2014-11-01 48 views
2

我知道這個問題已被問了好幾次,但它似乎沒有用我的代碼。BadPaddingException:在Android解密塊中損壞的Pad塊

解密時,我得到一個例外:

「javax.crypto.BadPaddingException:墊塊損壞」

我的代碼是:調用密碼時

private static byte[] appendIvToEncryptedData(byte[] eData, byte[] iv) throws Exception { 
     ByteArrayOutputStream os = new ByteArrayOutputStream(); 
     os.write(eData); 
     os.write(iv); 
     return os.toByteArray(); 
    } 

protected static byte[] dataEncryption(byte[] plainText) 
    throws Exception { 
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 
    SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "Crypto"); 
    byte [] iv = new byte[Constants.AES_BYTE_LENGTH]; 
    random.nextBytes(iv); 
    AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv); 
    SecretKeySpec secretKeySpec = new SecretKeySpec(mAESKey, "AES"); 
    cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, paramSpec); 
    return appendIvToEncryptedData(cipher.doFinal(plainText), cipher.getIV()); 
} 


protected static byte[] dataDecryption(byte[] encrypted) 
    throws Exception { 
    int ivIndex = encrypted.length - Constants.AES_BYTE_LENGTH; 
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 
    SecretKeySpec secretKeySpec = new SecretKeySpec(mAESKey, "AES"); 
    cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, 
      new IvParameterSpec(encrypted, ivIndex, Constants.AES_BYTE_LENGTH)); 

    return cipher.doFinal(encrypted); 
} 

拋出異常dataDecryption()函數中的.doFinal()。此外,調用SecureRandom得到這樣的警告:「Android 4.3及更高版本上可能存在不安全的隨機數字。請閱讀https://android-developers.blogspot.com/2013/08/some- securerandom-thoughts.html瞭解更多信息。」

我正在使用RandomAccessFile和FileOutputStream讀取和寫入文件,所以我直接使用字節數組。

我已經採取一看這另一個問題和modifyed我的代碼,因爲它說,但仍然不能正常工作:

Encryption error on Android 4.2

順便說一句,我在加密一個設備和解密在另一個不同。

這是我的堆棧跟蹤:

11-01 20:57:14.820: I/Exception(26336): javax.crypto.BadPaddingException: pad block corrupted 
11-01 20:57:14.820: I/Exception(26336):  at com.android.org.bouncycastle.jce.provider.JCEBlockCipher.engineDoFinal(JCEBlockCipher.java:701) 
11-01 20:57:14.820: I/Exception(26336):  at javax.crypto.Cipher.doFinal(Cipher.java:1106) 
11-01 20:57:14.820: I/Exception(26336):  at com.example.example.KeyManagement.dataDecryption(KeyManagement.java:132) 
11-01 20:57:14.820: I/Exception(26336):  at com.example.example.SecureReceiving$1.onEvent(SecureReceiving.java:86) 
11-01 20:57:14.820: I/Exception(26336):  at android.os.FileObserver$ObserverThread.onEvent(FileObserver.java:125) 
11-01 20:57:14.820: I/Exception(26336):  at android.os.FileObserver$ObserverThread.observe(Native Method) 
11-01 20:57:14.820: I/Exception(26336):  at android.os.FileObserver$ObserverThread.run(FileObserver.java:88) 

希望你能幫助我,在此先感謝。

+0

注意:我強烈推薦使用'new SecureRandom()'而不是'getInstance()',需要一個特定的算法和提供者。我通常會讓運行時系統挑選哪一個最好。此外,您可以使用'cipher.getBlockSize()'而不是常量。 – 2014-11-01 21:14:00

+0

我的意思是常量用於定義IV的大小。任何運氣與我的答案? – 2014-11-01 22:08:06

+0

這是我所有的不好,代碼工作(嗯,我不得不添加你在下面提到的修改),但問題(我認爲你提到它,但我找不到評論)是我掃描的關鍵( mAESKey)使用QR碼掃描器,但無法正確存儲。我有一個名爲KeyManagement.setKey()的函數,事實證明,由於我的「複製/粘貼」習慣,我正在調用兩次這個函數,最後關鍵是錯誤的。 非常感謝! – Fernando 2014-11-01 23:03:13

回答

2

您忘記了從密文中刪除IV。

嘗試:的

return cipher.doFinal(encrypted, 0, ivIndex); 

代替

return cipher.doFinal(encrypted); 

dataDecryption方法內。

相關問題