· 9 years ago · Oct 29, 2016, 07:28 PM
1// Get the AndroidKeyStore instance
2KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
3
4// Relict of the JCA API - you have to call load even
5// if you do not have an input stream you want to load or it'll crash
6keyStore.load(null);
7
8// Get the SecretKey from the KeyStore and instantiate a Cipher
9SecretKey secretKey =
10 (SecretKey) keyStore.getKey("myAwesomeSecretKey01", null);
11Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
12
13// Init the Cipher and encrypt the plaintext
14cipher.init(Cipher.ENCRYPT_MODE, secretKey);
15byte[] encryptedBytes =
16 cipher.doFinal("This is a super secret message".getBytes());