android - Android 模拟器上的密钥库崩溃
问题描述
当我尝试获取密钥库密钥时,我的应用程序崩溃了。
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)和小米米上测试过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
解决方案
推荐阅读
- ansible - 如何从dict中设置多个事实?
- java - 为什么在我的方法 onActivityResult 中它没有达到 else if 条件。我想使用 UCrop 将裁剪后的图像保存到 Firebase
- c - 我应该如何制作一个在元素周期表中添加新元素的函数?
- algorithm - 找到包含无向图中给定边的欧拉路径的最小成本(算法)
- python - 使用 python 找到 N^(1/17)
- python - 从充满文件的目录中提取字符串
- azure - 无效或未知密钥:network_security_group_id
- javascript - 如何切换侧边栏?
- node.js - 如何从 Google Maps Direction API 响应中渲染地图?
- json - 在 Google BigQuery 中读取多级重复 JSON 结构数据