微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – Android KeyStore私有指数无法提取

我想在 Android密钥库中生成一个RSA密钥对.由于Android 4.3应该可以在Android系统密钥库中生成RSA密钥.

生成我的RSA密钥(工作正常)

Calendar notBefore = Calendar.getInstance();
        Calendar notAfter = Calendar.getInstance();
        notAfter.add(1,Calendar.YEAR);
        KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(ctx)
                .setAlias("key")
                .setSubject(
                        new X500Principal(String.format("CN=%s,OU=%s","key",ctx.getPackageName())))
                .setSerialNumber(BigInteger.ONE)
                .setStartDate(notBefore.getTime())
                .setEndDate(notAfter.getTime()).build();
            KeyPairGenerator kpg;
            kpg = KeyPairGenerator.getInstance("RSA","AndroidKeyStore");
            kpg.initialize(spec);
            KeyPair kp = kpg.genKeyPair();
            publicKey = kp.getPublic();
            privateKey = kp.getPrivate();

我的RSA加密看起来像(也是):

public static byte[] RSAEncrypt(final byte[] plain)
        throws NoSuchAlgorithmException,NoSuchPaddingException,InvalidKeyException,IllegalBlockSizeException,BadPaddingException {

    Cipher cipher = Cipher.getInstance("RSA");
    System.out.println("RSA Encryption key: " + publicKey.getAlgorithm());
    System.out.println("RSA Encryption key: " + publicKey.getEncoded());

    cipher.init(Cipher.ENCRYPT_MODE,publicKey);
    byte[] encryptedBytes = cipher.doFinal(plain);
    return encryptedBytes;
}

解密:

public static byte[] RSADecrypt(final byte[] encryptedBytes)
        throws NoSuchAlgorithmException,BadPaddingException {

    Cipher cipher1 = Cipher.getInstance("RSA");

    System.out.println("RSA Encryption key: " + privateKey.getAlgorithm());
    System.out.println("RSA Encryption key: " + privateKey.getEncoded());

    cipher1.init(Cipher.DECRYPT_MODE,privateKey);
    byte[] decryptedBytes = cipher1.doFinal(encryptedBytes);
    return decryptedBytes;
}

在解密函数中,我收到以下错误消息(当privateKey被编码时,在cipher1.init()中):

12-12 21:49:40.338: E/AndroidRuntime(20423): FATAL EXCEPTION: main
12-12 21:49:40.338: E/AndroidRuntime(20423): java.lang.UnsupportedOperationException: private    exponent cannot be extracted
12-12 21:49:40.338: E/AndroidRuntime(20423):    at org.apache.harmony.xnet.provider.jsse.OpenSSlrsAPrivateKey.getPrivateExponent(OpenSSlrsAPrivateKey.java:143)

我不明白是不是可以在Android KeyStore中生成RSA密钥?任何人都可以提供一个在Android KeyStore中生成RSA密钥并使用私钥解密的示例.

提前谢谢了!

解决方法

根据 the code,我认为OpenSSL提供程序防止在密钥生成到设备时导出私有指数.
@Override
public final BigInteger getPrivateExponent() {
    if (key.isEngineBased()) {
        throw new UnsupportedOperationException("private exponent cannot be extracted");
    }

    ensureReadParams();
    return privateExponent;
}

因此,您可能需要指定在检索密码实例时要使用相同的加密提供程序.该提供商supports these RSA ciphers

> RSA / ECB / nopadding
> RSA / ECB / PKCS1Padding

您应该以这种方式创建密码实例:

Cipher cipher1 = Cipher.getInstance("RSA/ECB/PKCS1Padding","AndroidOpenSSL");

原文地址:https://www.jb51.cc/java/121771.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐