问题描述
最近从 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 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!