【问题标题】:Android 6.0 keystore ClassCast ExceptionAndroid 6.0 密钥库 ClassCastException
【发布时间】:2016-09-29 22:41:25
【问题描述】:

我正在使用Qlassified library,但在 android 6.0 和 6.0.1 上出现以下异常: java.lang.ClassCastException: android.security.keystore.AndroidKeyStoreECPublicKey cannot be cast to java.security.interfaces.RSAPublicKey

具体来说,当数据在此代码块内被加密时会发生这种情况:

final KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry) keyStoreInstance.getEntry(alias, null);

final RSAPublicKey publicKey = (RSAPublicKey) privateKeyEntry.getCertificate().getPublicKey(); // error

byte[] dataBytes = input.getBytes(CHARSET);
Cipher cipher = Cipher.getInstance(ALGORITHM, ANDROID_MODE);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return Base64.encodeToString(cipher.doFinal(dataBytes), BASE64_MODE);

有什么想法吗?

【问题讨论】:

  • 使用带有 RSA 公钥的证书?

标签: java android cryptography android-6.0-marshmallow java-security


【解决方案1】:

这来自存储在密钥库中的密钥的生成。您使用的是 EC 算法。您必须像这样指定 RSA 算法:

KeyPairGenerator generator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_RSA, "AndroidKeyStore");
                    generator.initialize(params);

我遇到了同样的问题,这解决了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-13
    • 2012-01-30
    • 1970-01-01
    • 2015-04-05
    • 2012-12-11
    • 2012-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多