首页 > 解决方案 > 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

标签: android

解决方案


推荐阅读