无法解密 android lollipop 中的加密文件

Cannot decrypt encrypted file in android lollipop

我的应用程序中有一个 encrypt/decrypt 机制来下载文件。

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

解密过程如下:

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 = 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();
        }