title: Android P - GenerateKeyPair
body: <p>While targeting my app to Android 9 Preview I get an error when trying to generate a <strong>KeyPair</strong>. I'm using the following code:</p>
  
  <pre><code>public void createKeysM(String alias, boolean requireAuth) {
  
      try {
          KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(
                  KeyProperties.KEY_ALGORITHM_RSA, SecurityConstants.KEYSTORE_PROVIDER_ANDROID_KEYSTORE);
          keyPairGenerator.initialize(
                  new KeyGenParameterSpec.Builder(
                          alias,
                          KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
                          .setAlgorithmParameterSpec(new RSAKeyGenParameterSpec(1024, F4))
                          .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
                          .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1)
                          .setDigests(KeyProperties.DIGEST_SHA256,
                                  KeyProperties.DIGEST_SHA384,
                                  KeyProperties.DIGEST_SHA512)
                          .setUserAuthenticationRequired(requireAuth)
                          .build());
          KeyPair keyPair = keyPairGenerator.generateKeyPair();
          Log.d(TAG, "Public Key is: " + keyPair.getPublic().toString());
  
      } catch (NoSuchProviderException | NoSuchAlgorithmException | InvalidAlgorithmParameterException e) {
          throw new RuntimeException(e);
      }
  
  }
  </code></pre>
  
  <p>I'm getting an error in the line <strong>KeyPair keyPair = keyPairGenerator.generateKeyPair();</strong></p>
  
  <p>I'm putting the stack trace at the end of the question. Has anyone encountered this problem?</p>
  
  <p>Thank you for your help!</p>
  
  <pre><code>04-17 14:15:41.653 com.stackoverflow.mask E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.stackoverflow.mask, PID: 8644
      java.lang.RuntimeException: Unable to create application com.stackoverflow.mask.view.DummyBeaconApplication: java.security.ProviderException: Failed to load generated key pair from keystore
          at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5879)
          at android.app.ActivityThread.access$1100(ActivityThread.java:193)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1623)
          at android.os.Handler.dispatchMessage(Handler.java:106)
          at android.os.Looper.loop(Looper.java:164)
          at android.app.ActivityThread.main(ActivityThread.java:6656)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
       Caused by: java.security.ProviderException: Failed to load generated key pair from keystore
          at android.security.keystore.AndroidKeyStoreKeyPairGeneratorSpi.loadKeystoreKeyPair(AndroidKeyStoreKeyPairGeneratorSpi.java:530)
          at android.security.keystore.AndroidKeyStoreKeyPairGeneratorSpi.generateKeyPair(AndroidKeyStoreKeyPairGeneratorSpi.java:478)
          at java.security.KeyPairGenerator$Delegate.generateKeyPair(KeyPairGenerator.java:727)
          at com.stackoverflow.mask.domain.shared.KeyManagerImpl.createKeysM(KeyManagerImpl.java:109)
          at com.stackoverflow.mask.domain.shared.KeyManagerImpl.createKeys(KeyManagerImpl.java:53)
          at com.stackoverflow.mask.domain.shared.DataManager.&lt;init&gt;(DataManager.java:41)
          at com.stackoverflow.mask.dagger.modules.AppModule.provideDataManager(AppModule.java:78)
          at com.stackoverflow.mask.dagger.modules.AppModule_ProvideDataManagerFactory.get(AppModule_ProvideDataManagerFactory.java:40)
          at com.stackoverflow.mask.dagger.modules.AppModule_ProvideDataManagerFactory.get(AppModule_ProvideDataManagerFactory.java:13)
          at dagger.internal.DoubleCheck.get(DoubleCheck.java:47)
          at com.stackoverflow.mask.dagger.modules.interactors.UseCaseModuleBeacon_ProvideGetBeaconUseCaseFactory.get(UseCaseModuleBeacon_ProvideGetBeaconUseCaseFactory.java:37)
          at com.stackoverflow.mask.dagger.modules.interactors.UseCaseModuleBeacon_ProvideGetBeaconUseCaseFactory.get(UseCaseModuleBeacon_ProvideGetBeaconUseCaseFactory.java:12)
          at com.stackoverflow.mask.dagger.modules.BaseActivityModule_ProvideBeaconPresenterFactory.get(BaseActivityModule_ProvideBeaconPresenterFactory.java:37)
          at com.stackoverflow.mask.dagger.modules.BaseActivityModule_ProvideBeaconPresenterFactory.get(BaseActivityModule_ProvideBeaconPresenterFactory.java:12)
          at com.stackoverflow.mask.dagger.modules.AppModule_ProvideBeaconHelperFactory.get(AppModule_ProvideBeaconHelperFactory.java:50)
          at com.stackoverflow.mask.dagger.modules.AppModule_ProvideBeaconHelperFactory.get(AppModule_ProvideBeaconHelperFactory.java:14)
          at dagger.internal.DoubleCheck.get(DoubleCheck.java:47)
          at com.stackoverflow.mask.dagger.components.DaggerAppComponent.injectDummyApplication(DaggerAppComponent.java:1449)
          at com.stackoverflow.mask.dagger.components.DaggerAppComponent.inject(DaggerAppComponent.java:1440)
          at com.stackoverflow.mask.view.DummyApplication.onCreate(DummyApplication.java:54)
          at com.stackoverflow.mask.view.DummyBeaconApplication.onCreate(DummyBeaconApplication.kt:28)
          at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1154)
          at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5874)
          at android.app.ActivityThread.access$1100(ActivityThread.java:193) 
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1623) 
          at android.os.Handler.dispatchMessage(Handler.java:106) 
          at android.os.Looper.loop(Looper.java:164) 
          at android.app.ActivityThread.main(ActivityThread.java:6656) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823) 
       Caused by: java.security.UnrecoverableKeyException: Failed to obtain X.509 form of public key
          at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStorePublicKeyFromKeystore(AndroidKeyStoreProvider.java:239)
          at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStoreKeyPairFromKeystore(AndroidKeyStoreProvider.java:278)
          at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStoreKeyPairFromKeystore(AndroidKeyStoreProvider.java:289)
  04-17 14:15:41.657 com.stackoverflow.mask E/AndroidRuntime:     at android.security.keystore.AndroidKeyStoreKeyPairGeneratorSpi.loadKeystoreKeyPair(AndroidKeyStoreKeyPairGeneratorSpi.java:521)
              ... 30 more
       Caused by: android.security.KeyStoreException: Invalid key blob
          at android.security.KeyStore.getKeyStoreException(KeyStore.java:823)
          at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStorePublicKeyFromKeystore(AndroidKeyStoreProvider.java:241)
              ... 33 more
  </code></pre>