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?
Just rename RSACoreEngine to RSACoreEngine2 and now it works. Of course you need source code of Bouncy 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.