問題描述
最近從 BC 1.34 升級到 1.45.我正在使用以下內容解碼一些先前編碼的數據:
Recently upgraded from BC 1.34 to 1.45. I'm decoding some previously-encoded data with the following:
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(encrypted);
使用 BC 1.45 時出現此異常:
When using BC 1.45 I get this exception:
javax.crypto.BadPaddingException: pad block corrupted
at org.bouncycastle.jce.provider.JCEBlockCipher.engineDoFinal(JCEBlockCipher.java:715)
at javax.crypto.Cipher.doFinal(Cipher.java:1090)
有關此問題的更多信息.我正在使用以下內容從密碼生成原始密鑰:
More about this issue. I am using the following to generate raw keys from a passphrase:
KeyGenerator kgen = KeyGenerator.getInstance("AES", "BC");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
sr.setSeed(seed);
kgen.init(128, sr);
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
我發現這會導致 BC 1.34 和 1.45 的兩個不同值.
What I have found is that this results in two different values for BC 1.34 vs 1.45.
它也可能與 BouncyCastle 無關(我正在 Android 2.3 上測試)
It might also not be BouncyCastle-related (I am testing on Android 2.3)
推薦答案
看起來問題是 SecureRandom 不能跨 Froyo-Gingerbread 邊界移植.這篇文章描述了一個類似的問題:
Looks like the problem is SecureRandom not being portable across the Froyo-Gingerbread boundary. This post describes a similar problem:
http://groups.google.com/group/android-security-discuss/browse_thread/thread/6ec015a33784b925
我不確定 SecureRandom 中到底發生了什么變化,但我發現修復它的唯一方法是使用便攜式方法生成的密鑰重新加密數據.
I am not sure what exactly changed in SecureRandom, but the only way I found to fix it was to reencrypt the data with keys generated using a portable method.
這篇關于升級到 1.45 時出現 BouncyCastle AES 錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!