当我尝试获取密钥库密钥时,我的应用程序崩溃了。
java.security.UnrecoverableKeyException: Failed to obtain information about key
at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStoreSecretKeyFromKeystore(AndroidKeyStoreProvider.java:282)
at android.security.keystore.AndroidKeyStoreSpi.engineGetKey(AndroidKeyStoreSpi.java:98)
at java.security.KeyStore.getKey(KeyStore.java:1062)
at br.com.atlasquantum.extension.CryptographyExtensionKt.getSecretKey(CryptographyExtension.kt:71)
at br.com.atlasquantum.ui.splash.view.SplashActivity.isUserSaved(SplashActivity.kt:110)
at br.com.atlasquantum.ui.splash.view.SplashActivity$onCreate$1.invokeSuspend(SplashActivity.kt:48)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:233)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:742)
Caused by: android.security.KeyStoreException: -32
at android.security.KeyStore.getKeyStoreException(KeyStore.java:695)
at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStoreSecretKeyFromKeystore(AndroidKeyStoreProvider.java:283)
at android.security.keystore.AndroidKeyStoreSpi.engineGetKey(AndroidKeyStoreSpi.java:98)
at java.security.KeyStore.getKey(KeyStore.java:1062)
at br.com.atlasquantum.extension.CryptographyExtensionKt.getSecretKey(CryptographyExtension.kt:71)
at br.com.atlasquantum.ui.splash.view.SplashActivity.isUserSaved(SplashActivity.kt:110)
at br.com.atlasquantum.ui.splash.view.SplashActivity$onCreate$1.invokeSuspend(SplashActivity.kt:48)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:233)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:742)
第一次使用密钥库时它不会崩溃,一段时间后它会开始崩溃...当启动崩溃时,密钥库不会再停止崩溃了...我在Android Emulator(Android 8)上进行了测试和小米Mi 9 SE(Android 9)...这在我的小米上没有崩溃。
获取密钥的代码:(在keyStore.getKey()
中崩溃)
fun getSecretKey(keyAlias: String): Key? {
val keyStore = KeyStore.getInstance(AndroidKeyStore)
keyStore.load(null)
return if (keyStore.containsAlias(keyAlias)) {
keyStore.getKey(keyAlias, null)
} else {
null
}
}
创建密钥库代码:
fun createKeystoreKey(keyAlias: String): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val keyStore = KeyStore.getInstance(AndroidKeyStore)
keyStore.load(null)
if (keyStore.containsAlias(keyAlias)) {
return true
} else {
val keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, AndroidKeyStore)
keyGenerator.init(
KeyGenParameterSpec.Builder(
keyAlias,
KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT
)
.setBlockModes(KeyProperties.BLOCK_MODE_GCM)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
.setRandomizedEncryptionRequired(false)
.build()
)
keyGenerator.generateKey()
return true
}
} else {
return false
}
}
什么是-32?
Caused by: android.security.KeyStoreException: -32