android N中的解密失败

时间:2016-11-23 08:27:28

标签: java android encryption cryptography android-7.0-nougat

以下函数用于解密存储在文件中的加密内容。相同的功能在Android操作系统版本中生成所需的输出,直至Nougat。在Android Nougat中,与Andorid M中生成的文件相比,解密文件的实际大小是实际大小的两倍。此外,我们发现用于将数据写入文件的while循环执行两次,与Android相比。 M.请建议一个有效的解决方案。

已经尝试过更改

byte[] keyBytes = key.getBytes();
byte[] ivBytes = ivString.getBytes(); 

byte[] keyBytes = key.getBytes("UTF-8");
byte[] ivBytes = ivString.getBytes("UTF-8");


public static  void  decryptPDFBook(String filePath, String key, String ivString, ShelfItem item) {

    InputStream  finStream;
    byte[] keyBytes = key.getBytes();
    byte[] ivBytes = ivString.getBytes();

    SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
    IvParameterSpec ivParamSpec = new IvParameterSpec(ivBytes);
    try {
        finStream = new FileInputStream(new File(filePath));
        Cipher cipherInstance = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipherInstance.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParamSpec);
        CipherInputStream cipherInputStream = new CipherInputStream(finStream, cipherInstance);
        ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();

        String newpath= Environment.getExternalStorageDirectory() +File.separator+ "ebooks"+File.separator+"ief"+File.separator+item.getId()+"_temp.pdf";

        File dir = new File(Environment.getExternalStorageDirectory() +File.separator+ "ebooks"+File.separator+"ief");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        FileOutputStream fos = new FileOutputStream(newpath);

        byte[] decodedByteChunk = new byte[1024];   
        int bytesAvailable = cipherInputStream.read(decodedByteChunk);
        while (bytesAvailable != -1) {
            fos.write(decodedByteChunk);
            bytesAvailable = cipherInputStream.read(decodedByteChunk);
        }
        byteArrayOS.close();
        cipherInputStream.close();
        fos.close();

       File encryptedFile = new File(filePath);
        if(encryptedFile.exists()){
           encryptedFile.delete();
           File file = new File (newpath);
           file.renameTo(encryptedFile);
       }


    } catch (NoSuchPaddingException nspe) {
        System.out.println("Inside NoSuchPaddingException");
        //Log.d("ELSAPAC", "BookExtractionUtil decryptPDFBook NoSuchPaddingException");
        nspe.printStackTrace();
    } catch (NoSuchAlgorithmException nsae) {
        System.out.println("Inside NoSuchAlgorithmException");
        //Log.d("ELSAPAC", "BookExtractionUtil decryptPDFBook NoSuchAlgorithmException");
        nsae.printStackTrace();
    } catch (InvalidKeyException ike) {
        System.out.println("Inside InvalidKeyException");
        //Log.d("ELSAPAC", "BookExtractionUtil decryptPDFBook InvalidKeyException");
        ike.printStackTrace();
    } catch (InvalidAlgorithmParameterException iape) {
        System.out.println("Inside InvalidAlgorithmParameterException");
        //Log.d("ELSAPAC", "BookExtractionUtil decryptPDFBook InvalidAlgorithmParameterException");
        iape.printStackTrace();
    } catch (IOException ioe) {     
        System.out.println("Inside IOException");
        //Log.d("ELSAPAC", "BookExtractionUtil decryptPDFBook IOException");
        ioe.printStackTrace();
        decryptPDFWithoutPadding( filePath,key,ivString,item);//pad block corrupted
        //return data;
    } catch (Exception e) {
       e.printStackTrace();
    }
     finally {        
    }

   // return null;

}

1 个答案:

答案 0 :(得分:3)

嗯,有一件事是我忽略了cipherInputStream.read()的回报值。您只需检查是否已读取任何数据,但无论实际读取多少字节,您都要写入整个decodedByteChunk。尝试修改你的while循环:

int bytesAvailable = cipherInputStream.read(decodedByteChunk);
while (bytesAvailable != -1) {
    fos.write(decodedByteChunk, 0, bytesAvailable);
    bytesAvailable = cipherInputStream.read(decodedByteChunk);
}

看看是否能解决您的问题。