Last active
March 2, 2020 09:28
-
-
Save flschweiger/d1917290fffa7ad75627dcc74b989d7a to your computer and use it in GitHub Desktop.
Create a symmetric key inside the Android Keystore.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?