title: Android 9 - KeyStore exception android.os.ServiceSpecificException
body: <p>If I run this code on Android 9, I receive the follow exception:</p>
  
  <pre><code>private static KeyStore.PrivateKeyEntry getPrivateKeyEntry(String alias) {
          try {
              KeyStore ks = KeyStore
                      .getInstance(SecurityConstants.KEYSTORE_PROVIDER_ANDROID_KEYSTORE);
              ks.load(null);
              KeyStore.Entry entry = ks.getEntry(alias, null);
  
              if (entry == null) {
                  Log.w(TAG, "No key found under alias: " + alias);
                  Log.w(TAG, "Exiting signData()...");
                  return null;
              }
  
              if (!(entry instanceof KeyStore.PrivateKeyEntry)) {
                  Log.w(TAG, "Not an instance of a PrivateKeyEntry");
                  Log.w(TAG, "Exiting signData()...");
                  return null;
              }
              return (KeyStore.PrivateKeyEntry) entry;
          } catch (Exception e) {
              Log.e(TAG, e.getMessage(), e);
              return null;
          }
      }
  </code></pre>
  
  <p>Exception:</p>
  
  <blockquote>
    <p>KeyStore exception
        android.os.ServiceSpecificException:  (code 7)
            at android.os.Parcel.createException(Parcel.java:1956)
            at android.os.Parcel.readException(Parcel.java:1910)
            at android.os.Parcel.readException(Parcel.java:1860)
            at android.security.IKeystoreService$Stub$Proxy.get(IKeystoreService.java:786)
            at android.security.KeyStore.get(KeyStore.java:195)
            at android.security.keystore.AndroidKeyStoreSpi.engineGetCertificateChain(AndroidKeyStoreSpi.java:118)
            at java.security.KeyStoreSpi.engineGetEntry(KeyStoreSpi.java:484)
            at java.security.KeyStore.getEntry(KeyStore.java:1560)
            at com.phenodev.testenc.KeyStoreHelper.getPrivateKeyEntry(KeyStoreHelper.java:151)
            at com.phenodev.testenc.KeyStoreHelper.encrypt(KeyStoreHelper.java:173)
            at com.phenodev.testenc.KeyStoreEncryptor.encrypt(KeyStoreEncryptor.java:19)</p>
  </blockquote>
  
  <p>Please help to fix it.</p>