Android:CyanogenMod 7 (Android 2.3) + BouncyCastle 加密库:IllegalAccessError
使用 bouncycastle 库时出现奇怪的错误:
ERROR/AndroidRuntime(1226): FATAL EXCEPTION: Thread-10
ERROR/AndroidRuntime(1226): java.lang.IllegalAccessError: tried to access class org.bouncycastle.crypto.engines.RSACoreEngine from class org.bouncycastle.crypto.engines.RSAEngine
ERROR/AndroidRuntime(1226): at org.bouncycastle.crypto.engines.RSAEngine.init(Unknown Source)
ERROR/AndroidRuntime(1226): at org.bouncycastle.crypto.encodings.PKCS1Encoding.init(PKCS1Encoding.java:90)
我已将 bouncycastle jar 文件 (bcprov145.jar) 添加到 eclipse 项目中。
生成此异常的代码是:
public int encrypt(byte[] source, int sourceLength, byte[] destination,
int destinationLength) throws CryptoError
{
int offset = 0;
byte[] encrypted;
org.bouncycastle.crypto.AsymmetricBlockCipher engine =
new org.bouncycastle.crypto.engines.RSAEngine();
engine = new org.bouncycastle.crypto.encodings.PKCS1Encoding(engine);
BigInteger mod = publicKey.getModulus();
BigInteger exp = publicKey.getPublicExponent();
org.bouncycastle.crypto.params.RSAKeyParameters keyParams =
new org.bouncycastle.crypto.params.RSAKeyParameters(false, mod, exp);
//When running the following line, the sh*t hits the fan....
engine.init(true, keyParams);
try
{
encrypted = engine.processBlock(source, offset, source.length);
}
catch (org.bouncycastle.crypto.InvalidCipherTextException e)
{
throw new CryptoError(e);
}
int length = Math.min(encrypted.length, destinationLength);
BufferTools.copyByteArray(encrypted, destination, length);
return length;
}
有趣的是:它在未修改的 Android 2.2 手机上完美运行,但我在使用 CyanogenMod 7.0.2.1(Android 2.3?)修改的手机上收到此错误。改装和未改装的手机均为 HTC Desire。
该项目是针对 Android 2.2 库构建的。这是问题所在吗?如果是,我应该创建不同的构建项目来区分这些版本吗?这将是非常不愉快的......
我已经在这里检查过类似的问题: IllegalAccessError with Android 和 BouncyCastle 但他们决定放弃 bouncycastle 库,在我的情况下这不是一个选择。
有人知道吗?
I'm getting a strange error when using bouncycastle libraries:
ERROR/AndroidRuntime(1226): FATAL EXCEPTION: Thread-10
ERROR/AndroidRuntime(1226): java.lang.IllegalAccessError: tried to access class org.bouncycastle.crypto.engines.RSACoreEngine from class org.bouncycastle.crypto.engines.RSAEngine
ERROR/AndroidRuntime(1226): at org.bouncycastle.crypto.engines.RSAEngine.init(Unknown Source)
ERROR/AndroidRuntime(1226): at org.bouncycastle.crypto.encodings.PKCS1Encoding.init(PKCS1Encoding.java:90)
I've added the bouncycastle jar file (bcprov145.jar) to the eclipse project.
The code that generated this exception is:
public int encrypt(byte[] source, int sourceLength, byte[] destination,
int destinationLength) throws CryptoError
{
int offset = 0;
byte[] encrypted;
org.bouncycastle.crypto.AsymmetricBlockCipher engine =
new org.bouncycastle.crypto.engines.RSAEngine();
engine = new org.bouncycastle.crypto.encodings.PKCS1Encoding(engine);
BigInteger mod = publicKey.getModulus();
BigInteger exp = publicKey.getPublicExponent();
org.bouncycastle.crypto.params.RSAKeyParameters keyParams =
new org.bouncycastle.crypto.params.RSAKeyParameters(false, mod, exp);
//When running the following line, the sh*t hits the fan....
engine.init(true, keyParams);
try
{
encrypted = engine.processBlock(source, offset, source.length);
}
catch (org.bouncycastle.crypto.InvalidCipherTextException e)
{
throw new CryptoError(e);
}
int length = Math.min(encrypted.length, destinationLength);
BufferTools.copyByteArray(encrypted, destination, length);
return length;
}
The funny thing is: It works perfectly on an unmodded Android 2.2 phone, but I get this error on my phone, modded with CyanogenMod 7.0.2.1 (Android 2.3?). Both the modded and the unmodded phone are HTC Desire.
The project is builded against Android 2.2 libraries. Is that the problem? If it is, should I create different build-projects to differentiate on these versions? That would be very unpleasant....
I've already checked out a similar issue here: IllegalAccessError with Android and BouncyCastle but they decided to abandon the bouncycastle libs, which in my case is not an option.
Does anyone have a clue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Legion of the Bouncy Castle 是 Android 固件的一部分,但不是 SDK 的一部分。您无法可靠地添加您自己的 JAR 实现。通过
javax.crypto
API 使用 Castle,或者找到另一个可以使用的加密库。The Legion of the Bouncy Castle is part of the Android firmware, but not part of the SDK. You cannot reliably add your own implementation of the JAR. Either use the Castle through the
javax.crypto
APIs, or find another crypto library that you can use.只需将 RSACoreEngine 重命名为 RSACoreEngine2,现在它就可以工作了。
当然,您需要 Bouncy Castle 的源代码。
Just rename RSACoreEngine to RSACoreEngine2 and now it works.
Of course you need source code of Bouncy Castle.