Created
October 28, 2016 21:32
-
-
Save flschweiger/476fedc0df1ec3b371f7a47fe5799e89 to your computer and use it in GitHub Desktop.
Encrypt a super secret message.
This file contains hidden or 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 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