Java 加密不会在 Android 上解密

标签 java android file encryption

好的,所以当我加密任何东西时,假设在这种情况下是一个文件,用我的电脑。它可以很好地加密和解密所述文件。但是,如果用我的计算机加密文件,然后将其发送到我的 android 并运行我编写的相同解密代码,它将无法正确解密文件。它没有显示任何错误,但是文件显然没有正确解密。

所以我了解到 Bouncy CaSTLe 与 JVM/JDK 标准加密库有很大不同,所以我不能 100% 确定这是否是问题所在。然而,这是有道理的。问题是,如果是的话,我不知道如何在我的 android 上使用 JVM/JDK 库进行加密。我不想在我的计算机上过多地使用 Bouncy CaSTLe 库。

public static void encrypt(File source, File dest, String password){
    try{
        FileInputStream inFile = new FileInputStream(source.getPath());
        FileOutputStream outFile = new FileOutputStream(dest.getPath());

        byte[] salt = new byte[8], iv = new byte[16];

        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, 65536, 256);
        SecretKey secretKey = factory.generateSecret(keySpec);
        SecretKey secret = new SecretKeySpec(secretKey.getEncoded(), "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secret, new IvParameterSpec(iv));

        byte[] input = new byte[64];
        int bytesRead;

        while((bytesRead = inFile.read(input)) != -1){
            byte[] output = cipher.update(input, 0, bytesRead);
            if(output != null){
                outFile.write(output);
            }
        }

        byte[] output = cipher.doFinal();
        if(output != null){
            outFile.write(output);
        }

        inFile.close();
        outFile.flush();
        outFile.close();
    }catch(Exception e){
        e.printStackTrace();
    }
}

public static void decrypt(File source, File dest, String password){
    try{
        byte[] salt = new byte[8], iv = new byte[16];

        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, 65536, 256);
        SecretKey tmp = factory.generateSecret(keySpec);
        SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));
        FileInputStream fis = new FileInputStream(source.getPath());
        FileOutputStream fos = new FileOutputStream(dest.getPath());
        byte[] in = new byte[64];
        int read;
        while((read = fis.read(in)) != -1){
            byte[] output = cipher.update(in, 0, read);
            if(output != null){
                fos.write(output);
            }
        }

        byte[] output = cipher.doFinal();
        if(output != null){
            fos.write(output);
        }
        fis.close();
        fos.flush();
        fos.close();
    }catch(Exception e){
        e.printStackTrace();
    }
}

结果将是能够使用标准 JVM/JDK 加密库在我的计算机上加密任何文件,并且能够在我的 android 上解密同一个文件。

但是,如果除了 Bouncy CaSTLe 之外还有其他库可以与 android 一起使用,我欢迎提出想法。

最佳答案

我遇到过类似的问题。首先检查您的加密和解密设置是否正确。

如果这些没问题,那么您应该将加密文本转换为 Base64 或类似的编码。

然后在手机上先解码Base64编码,再解密文件。

出现此问题是因为某些字符不适合在媒体之间保存或传输。它们还会受到底层操作系统的影响。即使更改了一点,您的整个解密文本也会更改,这里可能就是这种情况

安卓代码

public static void decrypt(File source, File dest, String password){
    try{
        Base64InputStream fis = new Base64InputStream(new DataInputStream(new FileInputStream(source.getPath())), Base64.DEFAULT);
        FileOutputStream fos = new FileOutputStream(dest.getPath());


        byte[] salt = new byte[8], iv = new byte[16];

        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, 65536, 256);
        SecretKey tmp = factory.generateSecret(keySpec);
        SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));

        byte[] in = new byte[64];
        int read;
        while((read = fis.read(in)) != -1){
            byte[] output = cipher.update(in, 0, read);
            if(output != null){
                fos.write(output);
            }
        }

        byte[] output = cipher.doFinal();
        if(output != null){
            fos.write(output);
        }
        fis.close();
        fos.flush();
        fos.close();
    }catch(Exception e){
        e.printStackTrace();
    }
}

常规 JVM

public static void encrypt(File source, File dest, String password){
    try{
        FileInputStream inFile = new FileInputStream(source.getPath());
        OutputStream outFile = Base64.getEncoder().wrap(new FileOutputStream(dest.getPath()));

        byte[] salt = new byte[8], iv = new byte[16];

        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, 65536, 256);
        SecretKey secretKey = factory.generateSecret(keySpec);
        SecretKey secret = new SecretKeySpec(secretKey.getEncoded(), "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secret, new IvParameterSpec(iv));

        byte[] input = new byte[64];
        int bytesRead;

        while((bytesRead = inFile.read(input)) != -1){
            byte[] output = cipher.update(input, 0, bytesRead);
            if(output != null){
                outFile.write(output);
            }
        }

        byte[] output = cipher.doFinal();
        if(output != null){
            outFile.write(output);
        }

        inFile.close();
        outFile.flush();
        outFile.close();
    }catch(Exception e){
        e.printStackTrace();
    }
}

public static void decrypt(File source, File dest, String password){
    try{
        InputStream fis = Base64.getDecoder().wrap(new FileInputStream(source.getPath()));
        //FileInputStream fis = new FileInputStream(source.getPath());
        FileOutputStream fos = new FileOutputStream(dest.getPath());

        byte[] salt = new byte[8], iv = new byte[16];

        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, 65536, 256);
        SecretKey tmp = factory.generateSecret(keySpec);
        SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));

        byte[] in = new byte[64];
        int read;
        while((read = fis.read(in)) != -1){
            byte[] output = cipher.update(in, 0, read);
            if(output != null){
                fos.write(output);
            }
        }

        byte[] output = cipher.doFinal();
        if(output != null){
            fos.write(output);
        }
        fis.close();
        fos.flush();
        fos.close();
    }catch(Exception e){
        e.printStackTrace();
    }
}

关于Java 加密不会在 Android 上解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58724102/

相关文章:

c - 循环中逻辑错误

Java 10 var 和捕获变量

Java 使用 array[] 分配给下一个索引

java - Jasper 报告 Excel 导出错误

android - 我可以在其中存储点以加快绘制速度的可绘制对象?

javascript - 如何在 React Native 中调用函数

java - 音乐停止时的 Android MediaPlayer 处理程序

java - 从较长的文件路径中获取文件路径的一部分

java - Spring Error Controller 响应 Not Acceptable

git - 如何使用 SourceTree 在我的 git 存储库中查找文件?