title: java.security.UnrecoverableKeyException: Failed to obtain information about private key
body: <p>I have the following lines to get the private key from key store on Android</p>
  
  <pre><code>KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
  keyStore.load(null);
  
  // generating key pair code omitted
  
  KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry) this.keyStore.getEntry("alias", null);
  </code></pre>
  
  <p>Everything works fine except that when the OS upgrades from Android 5.1.1 to Android 6.0.1, the 3rd line will throw <code>java.security.UnrecoverableKeyException: Failed to obtain information about private key</code> for very first execution. But it will work fine again afterward. Now my workaround is to execute the line for 2 times. At the same time, I am also wondering if there is better way to avoid the exception.</p>
  
  <h2>Update</h2>
  
  <p>The exception trace</p>
  
  <pre><code>W/System.err﹕ java.security.UnrecoverableKeyException: Failed to obtain information about private key
  W/System.err﹕ at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStorePublicKeyFromKeystore(AndroidKeyStoreProvider.java:217)
  W/System.err﹕ at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStoreKeyPairFromKeystore(AndroidKeyStoreProvider.java:253)
  W/System.err﹕ at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStorePrivateKeyFromKeystore(AndroidKeyStoreProvider.java:263)
  W/System.err﹕ at android.security.keystore.AndroidKeyStoreSpi.engineGetKey(AndroidKeyStoreSpi.java:93)
  W/System.err﹕ at java.security.KeyStoreSpi.engineGetEntry(KeyStoreSpi.java:372)
  W/System.err﹕ at java.security.KeyStore.getEntry(KeyStore.java:645)
  W/System.err﹕ at com.example.keystoretest.MainActivity.onCreate(MainActivity.java:113)
  W/System.err﹕ at android.app.Activity.performCreate(Activity.java:6251)
  W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
  W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
  W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
  W/System.err﹕ at android.app.ActivityThread.-wrap11(ActivityThread.java)
  W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
  W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
  W/System.err﹕ at android.os.Looper.loop(Looper.java:148)
  W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5417)
  W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
  W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
  W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  W/System.err﹕ Caused by: android.security.KeyStoreException: Invalid key blob
  W/System.err﹕ at android.security.KeyStore.getKeyStoreException(KeyStore.java:632)
  W/System.err﹕ at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStorePublicKeyFromKeystore(AndroidKeyStoreProvider.java:218)
  W/System.err﹕ ... 18 more
  </code></pre>