作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 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/
我在 Android 中尝试过指纹 validator 。最初它是可以工作的,而且是在 Java 中。我现在正在将代码移植到 kotlin。这次有些东西对我不起作用。 try { mK
我是一名优秀的程序员,十分优秀!