I’ve tried to encrypt data with SecretKey like below:
KeyGenParameterSpec keyGenParameterSpec = new KeyGenParameterSpec.Builder(
"alias", KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
.setKeySize(256)
.setBlockModes(KeyProperties.BLOCK_MODE_GCM)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
.setUserAuthenticationRequired(true)
.setUserAuthenticationValidityDurationSeconds(30)
.build();
KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
keyGenerator.init(keyGenParameterSpec);
SecretKey secretKey = keyGenerator.generateKey();
Cipher cipher = Cipher.getInstance("AES_256/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(iv));
byte[] doFinal = cipher.doFinal(inputBytes);
But fails with the following error: No provider offers [AES_256, GCM, NoPadding] for AES key of class android.security.keystore2.AndroidKeyStoreSecretKey and export format null
Any advice?
New contributor
mqtqbenc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.