Android InvalidKeyException:仅支持SecretKey
我最近开始在设备中看到这个错误Android InvalidKeyException:仅支持SecretKey,android,security,ssl,encryption,boringssl,Android,Security,Ssl,Encryption,Boringssl,我最近开始在设备中看到这个错误 java.security.InvalidKeyException: Only SecretKey is supported at com.android.org.conscrypt.OpenSSLCipher.checkAndSetEncodedKey(OpenSSLCipher.java:436) at com.android.org.conscrypt.OpenSSLCipher.engineInit(OpenSSLCiphe
java.security.InvalidKeyException: Only SecretKey is supported
at com.android.org.conscrypt.OpenSSLCipher.checkAndSetEncodedKey(OpenSSLCipher.java:436)
at com.android.org.conscrypt.OpenSSLCipher.engineInit(OpenSSLCipher.java:273)
at javax.crypto.Cipher.tryTransformWithProvider(Cipher.java:2664)
at javax.crypto.Cipher.tryCombinations(Cipher.java:2575)
at javax.crypto.Cipher$SpiAndProviderUpdater.updateAndGetSpiAndProvider(Cipher.java:2480)
at javax.crypto.Cipher.chooseProvider(Cipher.java:567)
at javax.crypto.Cipher.init(Cipher.java:975)
at javax.crypto.Cipher.init(Cipher.java:910)
从
在以下情况下引发异常:
if (!(key instanceof SecretKey)) {
throw new InvalidKeyException("Only SecretKey is supported");
}
我总是这样从商店拿到秘钥:
SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME, null);
知道发生了什么吗?使用密钥生成器对象生成密钥 例如: 将SecretKeyObject初始化为全局
SecretKey secretKeyObject;
通过以下方式初始化密钥生成器对象:
KeyGenerator keyGeneratorObject = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES,"AndroidKeyStore");
keyStoreObject.load(null);
keyGeneratorObject.init(new KeyGenParameterSpec.Builder(key_name,KeyProperties.PURPOSE_ENCRYPT|KeyProperties.PURPOSE_DECRYPT).setBlockModes(KeyProperties.BLOCK_MODE_CBC).setUserAuthenticationRequired(true)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7).build());
secretKeyObject = keyGeneratorObject.generateKey();
然后
cipherObject.init(Cipher.ENCRYPT_MODE, secretKeyObject);
这对我很有效。如果密钥为空,则会发生这种情况
我有一个类似的问题,只是我的代码中有一个错误,它阻止了正确读取密钥。因此,在Cipher.init()中传递了null并导致了此消息。如果
key
为null
,则会发生这种情况T的空实例总是返回false
。