Skip to content

Instantly share code, notes, and snippets.

@flschweiger
Last active March 2, 2020 09:28
Show Gist options
  • Save flschweiger/d1917290fffa7ad75627dcc74b989d7a to your computer and use it in GitHub Desktop.
Save flschweiger/d1917290fffa7ad75627dcc74b989d7a to your computer and use it in GitHub Desktop.
Create a symmetric key inside the Android Keystore.
// Get the KeyGenerator instance for AES
KeyGenerator keyGenerator = KeyGenerator.getInstance(
KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
// Create a KeyGenParameterSpec builder and
// set the alias and different purposes of the key
KeyGenParameterSpec.Builder builder = new KeyGenParameterSpec.Builder(
"myAwesomeSecretKey01",
KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT);
// The KeyGenParameterSpec is how parameters for your key are passed to the
// generator. Choose wisely!
builder
.setKeySize(256)
.setBlockModes(KeyProperties.BLOCK_MODE_CBC)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7);
// Generate and store the key
keyGenerator.init(builder.build());
keyGenerator.generateKey();
@alex534488
Copy link

Where exactly is the key stored ? How can you get the key as a string or byte[] after all these steps ? Do you need to make all these steps everytime you open the app? or is there a condition you can set to only get the key insted of generating it again?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment