Android: CyanogenMod 7 (Android 2.3) + BouncyCastle encryption libraries: IllegalAccessError

丶灬走出姿态 提交于 2019-12-23 04:46:02

问题


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?


回答1:


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.




回答2:


Just rename RSACoreEngine to RSACoreEngine2 and now it works. Of course you need source code of Bouncy Castle.



来源:https://stackoverflow.com/questions/5816310/android-cyanogenmod-7-android-2-3-bouncycastle-encryption-libraries-illega

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!