mKeyStore?.getKey("default_key", null) 在 Kotlin 中变得空
mKeyStore?.getKey("default_key", null) is getting null in Kotlin
我在 Android 中尝试过指纹验证器。最初它正在工作,那是在 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 编写相同的代码时,键会得到一个空值。
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
为什么我会收到这个?如果有什么遗漏,请告诉我。
感谢您宝贵的时间。
初始化密钥生成器,然后提取密钥。
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()
我在 Android 中尝试过指纹验证器。最初它正在工作,那是在 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 编写相同的代码时,键会得到一个空值。
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
为什么我会收到这个?如果有什么遗漏,请告诉我。
感谢您宝贵的时间。
初始化密钥生成器,然后提取密钥。
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()