Skip to content

Instantly share code, notes, and snippets.

@flschweiger
Created October 28, 2016 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flschweiger/476fedc0df1ec3b371f7a47fe5799e89 to your computer and use it in GitHub Desktop.
Save flschweiger/476fedc0df1ec3b371f7a47fe5799e89 to your computer and use it in GitHub Desktop.
Encrypt a super secret message.
// Get the AndroidKeyStore instance
KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
// Relict of the JCA API - you have to call load even
// if you do not have an input stream you want to load or it'll crash
keyStore.load(null);
// Get the SecretKey from the KeyStore and instantiate a Cipher
SecretKey secretKey =
(SecretKey) keyStore.getKey("myAwesomeSecretKey01", null);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
// Init the Cipher and encrypt the plaintext
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedBytes =
cipher.doFinal("This is a super secret message".getBytes());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment