升级到 1.45 时出现 BouncyCastle AES 错误

BouncyCastle AES error when upgrading to 1.45(升级到 1.45 时出现 BouncyCastle AES 错误)
本文介绍了升级到 1.45 时出现 BouncyCastle AES 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Reliable implementation of PBKDF2-HMAC-SHA256 for JAVA(PBKDF2-HMAC-SHA256 for JAVA 的可靠实现)
Correct way to sign and verify signature using bouncycastle(使用 bouncycastle 签名和验证签名的正确方法)
Creating RSA Public Key From String(从字符串创建 RSA 公钥)
Why java.security.NoSuchProviderException No such provider: BC?(为什么 java.security.NoSuchProviderException 没有这样的提供者:BC?)
Generating X509 Certificate using Bouncy Castle Java(使用 Bouncy Castle Java 生成 X509 证书)
How can I get a PublicKey object from EC public key bytes?(如何从 EC 公钥字节中获取 PublicKey 对象?)