0

使用 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 and BouncyCastle但他们决定放弃 bouncycastle 库,在我的情况下这不是一个选项。

有人有线索吗?

4

2 回答 2

2

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.

于 2011-04-28T10:40:06.337 回答
1

只需将 RSACoreEngine 重命名为 RSACoreEngine2 即可。当然你需要Bouncy Castle的源代码。

于 2012-02-21T16:56:23.260 回答