AndroidKeystore NoSuchAlgorithm 异常

标签 android rsa keystore pkcs#1

我正在尝试初始化一个用于加密和解密的 Cipher 对象。

String cipher = privateKey.getAlgorithm() + "/ECB/PKCS1Padding";
        mCipher = Cipher.getInstance(cipher, "AndroidKeyStore");

我在 android 上不断收到以下异常:

    System.err: java.security.NoSuchAlgorithmException: Provider   AndroidKeyStore does not provide RSA/ECB/PKCS1Padding
   03-20 00:28:38.270 19817 21488 W System.err:     at javax.crypto.Cipher.getCipher(Cipher.java:357)
   03-20 00:28:38.270 19817 21488 W System.err:     at javax.crypto.Cipher.getInstance(Cipher.java:325)
   03-20 00:28:38.271 19817 21488 W System.err:     at javax.crypto.Cipher.getInstance(Cipher.java:297)

最佳答案

根据 Google 的说法,BouncyCaSTLe 中存在一个错误,导致他们无法正常安装其提供程序,因此他们在安装与 AndroidKeyStore key 相关的算法时使用了提供程序名称 AndroidKeyStoreBCWorkaroundSee AOSP source here.

以下代码将通过 Marshmallow 在 KitKat 上运行,但我不推荐它,因为 Google 表示他们将在不再需要时消除变通方法:

Cipher.getInstance("RSA/ECB/PKCS1Padding", "AndroidKeyStoreBCWorkaround");

代替使用私钥获取密码,密码类将找到正确的兼容密码:

KeyStore ks = KeyStore.getInstance("AndroidKeyStore");
ks.load(null);

KeyStore.PrivateKeyEntry keyEntry = 
        (KeyStore.PrivateKeyEntry) ks.getEntry("mykeypair", null);
PrivateKey privKey = keyEntry.getPrivateKey();

Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding");
c.init(Cipher.ENCRYPT_MODE, privKey);
byte[] cipherText = c.doFinal(clearText, 0, clearText.length);

关于AndroidKeystore NoSuchAlgorithm 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36111452/

相关文章:

security - JWT RS256:通过https获取公共(public) key 是否安全?

java - 使用生成的 keystore 文件的 tomcat https 配置

java - 如何将 AES 加密中使用的 key 共享给其他应用程序进行解密?

android - NoClassDefFoundError : com. google.firebase.FirebaseOptions(firebase 和 admob)

android - 表格不适合移动设备上的父容器大小

java - 在Android中,有没有办法设置标志/广播接收器来影响其他应用程序?

c - 使用 EVP 工具使用 RSA 加密 key 解密 AES 加密消息

c - PEM_write_PrivateKey() 函数未将 RSA 私钥存储在 private.pem 文件中

ssl - "unable to find valid certification path to requested target"添加新的 Keystore 到 ActiveMQ 之后

android - 如何让所有包只显示在应用程序屏幕中