gpt4 book ai didi

java.security.InvalidKeyException

转载 作者:行者123 更新时间:2023-12-02 05:56:30 26 4
gpt4 key购买 nike

我遇到的问题只有在我要生成签名的 apk 时才会发生,但是当我直接从 Android Studio 运行时,一切正常。

错误发生在这里:

val key = keyStore? .getKey(KEY_NAME,空)

密码? .init(Cipher.ENCRYPT_MODE, key )

    @TargetApi(Build.VERSION_CODES.M)
private fun generateKey() {
try {
keyStore = KeyStore.getInstance("AndroidKeyStore")

keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore")

keyStore?.load(null)

keyGenerator?.init(KeyGenParameterSpec.Builder(KEY_NAME,
KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT)
.setBlocenter code herekModes(KeyProperties.BLOCK_MODE_CBC)
.setUserAuthenticationRequired(true)
.setEncryptionPaddings(
KeyProperties.ENCRYPTION_PADDING_PKCS7)

.build())
keyGenerator?.generateKey()
} catch (e: Exception) {
e.printStackTrace()
} catch (e: NoSuchAlgorithmException) {
throw RuntimeException("Failed to get KeyGenerator instance", e)
} catch (e: NoSuchProviderException) {
throw RuntimeException("Failed to get KeyGenerator instance", e)
} catch (e: NoSuchAlgorithmException) {
throw RuntimeException(e)
} catch (e: InvalidAlgorithmParameterException) {
throw RuntimeException(e)
} catch (e: CertificateException) {
throw RuntimeException(e)
} catch (e: IOException) {
throw RuntimeException(e)
}

}



@TargetApi(Build.VERSION_CODES.M)
private fun cipherInit(): Boolean {
try {
cipher = Cipher.getInstance(
KeyProperties.KEY_ALGORITHM_AES + "/"
+ KeyProperties.BLOCK_MODE_CBC + "/"
+ KeyProperties.ENCRYPTION_PADDING_PKCS7)
} catch (e: NoSuchAlgorithmException) {
throw RuntimeException("Failed to get Cipher", e)
} catch (e: NoSuchPaddingException) {
throw RuntimeException("Failed to get Cipher", e)
}

try {
keyStore?.load(null)
val key = keyStore?.getKey(KEY_NAME, null)
cipher?.init(Cipher.ENCRYPT_MODE, key)
return true
} catch (e: KeyPermanentlyInvalidatedException) {
return false
} catch (e: KeyStoreException) {
throw RuntimeException("Failed to init Cipher", e)
} catch (e: CertificateException) {
throw RuntimeException("Failed to init Cipher", e)
} catch (e: UnrecoverableKeyException) {
throw RuntimeException("Failed to init Cipher", e)
} catch (e: IOException) {
throw RuntimeException("Failed to init Cipher", e)
} catch (e: NoSuchAlgorithmException) {
throw RuntimeException("Failed to init Cipher", e)
} catch (e: InvalidKeyException) {
throw RuntimeException("Failed to init Cipher", e)
}
}

错误:

由java.security.InvalidKeyException引起仅支持 SecretKey

最佳答案

完成后,您应该测试 key 生成:

val keyGen = KeyGenerator.getInstance("AES")
keyGen.init(128)
val secretKey = keyGen.generateKey()

希望这有帮助

关于java.security.InvalidKeyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56009098/

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