gpt4 book ai didi

java - mKeyStore?.getKey ("default_key", null) 在 Kotlin 中获取 null

转载 作者:行者123 更新时间:2023-12-02 09:38:28 24 4
gpt4 key购买 nike

我在 Android 中尝试过指纹 validator 。最初它是可以工作的,而且是在 Java 中。我现在正在将代码移植到 kotlin。这次有些东西对我不起作用。

try {
mKeyStore = KeyStore.getInstance("AndroidKeyStore");
} catch (KeyStoreException e) {
throw new RuntimeException("Failed to get an instance of KeyStore", e);
}
try {
mKeyGenerator = KeyGenerator
.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
} catch (NoSuchAlgorithmException | NoSuchProviderException e) {
throw new RuntimeException("Failed to get an instance of KeyGenerator", e);
}

mKeyStore.load(null);
SecretKey key = (SecretKey) mKeyStore.getKey("default_key", null);

上面写的代码是用Java编写的,我得到的键是非空值。

但是当用 Kotlin 编写相同的代码时,关键是获取 null 值。

try {
mKeyStore = KeyStore.getInstance("AndroidKeyStore")
} catch (e: KeyStoreException) {
throw RuntimeException("Failed to get an instance of KeyStore", e)
}

try {
mKeyGenerator = KeyGenerator
.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore")
} catch (e: NoSuchAlgorithmException) {
throw RuntimeException("Failed to get an instance of KeyGenerator", e)
} catch (e: NoSuchProviderException) {
throw RuntimeException("Failed to get an instance of KeyGenerator", e)
}

mKeyStore?.load(null)
val key = mKeyStore?.getKey("default_key", null) as? SecretKey

为什么我会收到这个?如果有什么遗漏请澄清。

感谢您抽出宝贵的时间。

最佳答案

初始化 key 生成器,然后提取 key 。

 mKeyGenerator.init(
KeyGenParameterSpec.Builder("default_key", KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_CBC)
.setUserAuthenticationRequired(true)
.setEncryptionPaddings(
KeyProperties.ENCRYPTION_PADDING_PKCS7
)
.build()
)
mKeyGenerator.generateKey()

关于java - mKeyStore?.getKey ("default_key", null) 在 Kotlin 中获取 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57303640/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com