AndroidKeystore NoSuchAlgorithm exception

I am trying to initialize a Cipher object for encryption and decryption.

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

I keep getting the following exception in 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)
+4
source share
1 answer

According to Google, there is a bug in BouncyCastle that prevented them from installing their provider in normal mode, so they used the provider name AndroidKeyStoreBCWorkaroundwhen installing the algorithms associated with the AndroidKeyStore keys. See here the source of AOSP.

The following code will run on KitKat via Marshmallow, but I do not recommend it, as Google says it will eliminate the crawl when it is no longer needed:

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

Cipher, Cipher Cipher:

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);
+7

All Articles