0

My code

    try {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); 
        KeyPair keyPair = keyGen.generateKeyPair(); 
        RSAKey privateKeyResult = (RSAKey) keyPair.getPrivate();
        if(keyStore!=null) {
            keyStore.setKeyEntry(alias, (Key) privateKeyResult, null, new Certificate[]{certificate});
        ...
    } catch (KeyStoreException e) {
        logger.error("Error in insertion to keystore", e);
    }

don't work when the user upgrade the Android operating system from Oreo to Pie (9.0).

get exception:

java.security.KeyStoreException: Failed to store private key
    at android.security.keystore.AndroidKeyStoreSpi.setPrivateKeyEntry(AndroidKeyStoreSpi.java:536)
    at android.security.keystore.AndroidKeyStoreSpi.engineSetKeyEntry(AndroidKeyStoreSpi.java:278)
    at java.security.KeyStore.setKeyEntry(KeyStore.java:1179)
...

Caused by: android.security.KeyStoreException: Memory allocation failed
    at android.security.KeyStore.getKeyStoreException(KeyStore.java:851)
    ... 19 more

please addvice

iftach barshem
  • 497
  • 5
  • 17
  • What type of key (RSA, DSA, ECDSA, ...) and bit length do you use? On which Android 9.0 devices did you test your code? – Robert Sep 08 '19 at 17:14
  • The key is RSA. KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); KeyPair keyPair = keyGen.generateKeyPair(); RSAKey privateKeyResult = (RSAKey) keyPair.getPrivate(); – iftach barshem Sep 09 '19 at 07:57
  • Is the key generated on device? If yes why don't you use the [AndroidKeyStore methods for generating](https://developer.android.com/training/articles/keystore#GeneratingANewPrivateKey) it? – Robert Sep 09 '19 at 08:04

0 Answers0