【问题标题】:Android jvm doesn't support keyFactory generatePublic() ?Android jvm 不支持 keyFactory generatePublic() ?
【发布时间】:2012-01-30 07:32:29
【问题描述】:

各位,我最近在 PC 和 Android 上实现了 RSA 加密。有些在 PC (jdk1.6) 上运行良好,但是当我在 Android 上尝试时(Android 2.1 with jdk1.5 strong>),莫名其妙出现错误。

代码如下:

BigInteger m = m; BigInteger e = e; RSAPublicKeySpec keySpec = new RSAPublicKeySpec(m, e); KeyFactory fact = KeyFactory.getInstance("RSA"); try{ PublicKey pubKey = fact.generatePublic(keySpec); return pubKey; } catch (Exception e) { throw new RuntimeException("Spurious serialisation error", e); } return null;

问题是 PublicKey pubKey = fact.generatePublic(keySpec); 好像引起了异常,但是抓不到,甚至修改为Throwable e .它只是跳转到 return null; 但实际上返回值不是 null,它是一个非法的 Pubkey,具有正常的指数(65537)和一个坏的模数(用大量字母填充,'ae3432a** *'。

谁能帮我测试一下?

【问题讨论】:

    标签: android key rsa


    【解决方案1】:

    我们在 Android 2.3.6 应用中使用 RSA-Publickey。我们使用以下代码生成密钥:

    // data[] is pre-filled with modulus and publicExponent
    String ENCRYPTION_ALGORITHM = "RSA";
    
    BigInteger modulus = (BigInteger) data[0];
    BigInteger publicExponent = (BigInteger) data[1];
    
    PublicKey publicKey = getKeyFactory().generatePublic(new RSAPublicKeySpec(modulus, publicExponent));
    return publicKey;
    
    private static KeyFactory getKeyFactory() {
        if (keyFactory == null) {
            try {
                keyFactory = KeyFactory.getInstance(ENCRYPTION_ALGORITHM);
            } catch (NoSuchAlgorithmException e) {
                // Algorithm is part of every Android installation. Since we do not get here under realistic
                // circumstances it is OK to crash here.
                throw new HellFrozeOverException();
            }
        }
        return keyFactory;
    }
    

    如果我没看错,你也会这样做。对不起,我不能帮助你更多......

    【讨论】:

    • 谢谢Tobias,我调试了这些代码,发现provider可能有问题,jdk1.5会使用Provider: SunRsaSign ,而android 2.1使用bouncycastle 1.43,但我还是解决不了,
    • 你这么说,你在Android 2.3.6上测试这些代码,效果好吗?
    • 是的,我们测试了这段代码,它运行良好。由于 NFC 问题,我们决定改用 Android 4。
    猜你喜欢
    • 2014-04-12
    • 2017-01-15
    • 2019-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    相关资源
    最近更新 更多