【问题标题】:Android: CyanogenMod 7 (Android 2.3) + BouncyCastle encryption libraries: IllegalAccessErrorAndroid:CyanogenMod 7 (Android 2.3) + BouncyCastle 加密库:IllegalAccessError
【发布时间】:2011-04-28 09:14:58
【问题描述】:

我在使用 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 库,在我的情况下这不是一个选项。

有人知道吗?

【问题讨论】:

    标签: android encryption bouncycastle android-sdk-2.3 cyanogenmod


    【解决方案1】:

    充气城堡军团是 Android 固件的一部分,但不是 SDK 的一部分。您无法可靠地添加自己的 JAR 实现。要么通过javax.crypto API 使用 Castle,要么找到另一个可以使用的加密库。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2011-03-16
        • 1970-01-01
        • 2012-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-01
        • 2017-03-17
        • 1970-01-01
        相关资源
        最近更新 更多