I'm using the code below to generate and store an AES key into the Android KeyStore:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { //redundant
try {
// generate some AES key for encryption
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
keyPairGenerator.initialize(new KeyGenParameterSpec.Builder(
"VideoEncryptionKey",
KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_GCM)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
.setRandomizedEncryptionRequired(false)
.build());
keyPairGenerator.generateKeyPair();
} catch (Exception e) {
e.printStackTrace();
}
}
When executed, the code fails with java.security.NoSuchAlgorithmException: KeyPairGenerator AES implementation not found
.
The code is built with targetSdkVersion 23
and compileSdkVersion 25
and is running on a Blackberry Priv with Android 6.0.1 so according to the docs, there shouldn't be any such exceptions as the AES algorithm requires API level 23 or higher.
Any help is appreciated.
The KeyPairGenerator
is for algorithms that uses key pairs (private and public keys) like RSA or DSA.
For symmetric keys like in AES, use the KeyGenerator
class.