将我的应用定位到Android 9预览时,我在尝试生成 KeyPair 时出错。我使用以下代码:
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);
}
}
我在 KeyPair keyPair = keyPairGenerator.generateKeyPair();
行中收到错误我将堆栈跟踪放在问题的最后。有人遇到过这个问题吗?
感谢您的帮助!
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.<init>(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
答案 0 :(得分:1)
在Google跟踪器中打开了一个错误。你可以在这里跟踪它:
[Developer Preview Android P] Unable to generate java.security.KeyPair