简体   繁体   English

java.security.InvalidKeyException

[英]java.security.InvalidKeyException

I'm having trouble that only happens when I'm going to generate signed apk, but when I'm running directly from my Android Studio, everything works normally. 我遇到麻烦只会在我要生成签名的apk时发生,但是当我直接从我的Android Studio运行时,一切正常。

The error happens here: 错误发生在这里:

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

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

    @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)
        }
    }

Erro: 埃罗:

Caused by java.security.InvalidKeyException Only SecretKey is supported 由java.security.InvalidKeyException引起仅支持SecretKey

You should test the key generation when it's done like so: 您应该像这样测试密钥生成:

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

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.security.InvalidKeyException:密钥库操作失败 - java.security.InvalidKeyException: Keystore operation failed java.security.InvalidKeyException:密钥大小错误 - java.security.InvalidKeyException: Wrong key size java.security.InvalidKeyException:缺少参数 - java.security.InvalidKeyException: Parameters missing 验证抛出 java.security.InvalidKeyException: null - Verify throws java.security.InvalidKeyException: null InvalidKeyException java.security.InvalidKeyException:没有安装的提供程序支持此键:(null) - InvalidKeyException java.security.InvalidKeyException: No installed provider supports this key: (null) BouncyCastle 签名:java.security.InvalidKeyException:密钥格式无效 - BouncyCastle Signing: java.security.InvalidKeyException: invalid key format JAVA中的加密私钥:java.security.InvalidKeyException - Encrypted Private key in JAVA: java.security.InvalidKeyException java.security.InvalidKeyException:无效的密钥长度:8个字节 - java.security.InvalidKeyException: Invalid key length: 8 bytes java.security.InvalidKeyException:解密期间密钥大小错误 - java.security.InvalidKeyException: Wrong key size during decryption java.security.InvalidKeyException:DES-EDE3的密钥长度无效 - java.security.InvalidKeyException: invalid key length for DES-EDE3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM