android - 无法解密android Lollipop 中的加密文件

标签 android encryption android-5.0-lollipop

我的应用程序中有一个用于下载文件的加密/解密机制。

此机制适用于 android 5.0-lollipop 之前的所有 android 设备和版本。

解密过程如下:

cipher.init(Cipher.DECRYPT_MODE, key);
fileInputStream = new FileInputStream(file);
cipherInputStream = new CipherInputStream(fileInputStream, cipher);
byte[] fileByte = new byte[(int) file.length()];
int j = cipherInputStream.read(fileByte);
return fileByte;

密码和 key 之前生成并在整个应用程序中使用:

 key = new SecretKeySpec(keyValue, "AES");
 try {
     cipher = Cipher.getInstance("AES");
 } catch (Exception e) {
     e.printStackTrace();
 }

当我在 android 5.0 中解密一个大约 200,000 字节的文件时,j(返回前的变量)约为 8000,远低于 200000,而在旧的 android 版本中它恰好等于解密的文件长度。

我发现问题出在解密上。因为我可以在 android 5.0 中加密文件并在旧的 android 版本中解密它,但反之亦然。但是我正在发布加密过程:

cipher.init(Cipher.ENCRYPT_MODE, AESutil.key);
cipherOutputStream = new CipherOutputStream(output, cipher);
byte data[] = new byte[1024];
int count;
while ((count = input.read(data)) != -1) {
    cipherOutputStream.write(data, 0, count);
}

提前致谢

最佳答案

我的密码示例(L):

APPPATH 是我在 SD 卡上的应用程序目录的字符串

static void encrypt(File file, String pass) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {


        FileInputStream fis = new FileInputStream(file);


        FileOutputStream fos = new FileOutputStream(APPPATH+"/E_"+file.getName());


        SecretKeySpec sks = new SecretKeySpec(pass.getBytes(), "AES");

        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, sks);

        CipherOutputStream cos = new CipherOutputStream(fos, cipher);

        int b;
        byte[] d = new byte[8];
        while((b = fis.read(d)) != -1) {
            cos.write(d, 0, b);
        }

        cos.flush();
        cos.close();
        fis.close();


    }



     static void decrypt(File file, String pass) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {

            FileInputStream fis = new FileInputStream(file);

            FileOutputStream fos = new FileOutputStream(APPPATH+"/D_"+file.getName());
            SecretKeySpec sks = new SecretKeySpec(pass.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, sks);
            CipherInputStream cis = new CipherInputStream(fis, cipher);
            int b;
            byte[] d = new byte[8];
            while((b = cis.read(d)) != -1) {
                fos.write(d, 0, b);
            }
            fos.flush();
            fos.close();
            cis.close();
        }

关于android - 无法解密android Lollipop 中的加密文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27820538/

相关文章:

java - 当 android lollipop 或更高版本时设置 windowTranslucentStatus=true

android - 无法解析或不是 SUPPORTED_ABIS、FLAVOR & VERSION_CODES.LOLLIPOP 的字段

java - Android Lollipop 设计问题

android - 以编程方式设置相对布局中 EditText 和 TextView 的位置

android - 使用 Node js 向 google ccs 发送下游消息

java - XMLEncryptionFactory 在哪里?

java - 解密错误: caused by java. lang.noclassdeffounderror com/android/org/constcrypt/OpenSSLRSAPublicKey android

android - 如何在 MPAndroidChart 中设置 xAxis 的字符串值?

android - 如何在 android 中使用 onclick 更改按钮?

algorithm - bcrypt 真的比其他 key 派生算法更能缓解 GPU 攻击吗?