· 8 years ago · Jan 18, 2018, 10:04 AM
1Cipher cipher = null;
2 String transformation = KeyProperties.KEY_ALGORITHM_AES + "/"
3 + KeyProperties.BLOCK_MODE_CBC + "/"
4 + KeyProperties.ENCRYPTION_PADDING_PKCS7;
5 try {
6 cipher = Cipher.getInstance(transformation);
7
8 } catch (NoSuchAlgorithmException e) {
9 e.printStackTrace();
10 } catch (NoSuchPaddingException e) {
11 e.printStackTrace();
12 }
13
14 try {
15 String message = "This is the message i want send";
16 byte[] messageByte = message.getBytes();
17 //get the keystore instance, do we can get a key from it
18 keyStore.load(null);
19 SecretKey secretKey = (SecretKey) keyStore.getKey(KEY_STORE_NAME, null);
20 //get the cipher to use this secret key to encrypt the message
21 if (cipher != null) {
22 cipher.init(Cipher.ENCRYPT_MODE, secretKey);
23 cipher.doFinal(messageByte);
24 }
25
26
27 } catch (CertificateException e) {
28 e.printStackTrace();
29 } catch (NoSuchAlgorithmException e) {
30 e.printStackTrace();
31 } catch (IOException e) {
32 e.printStackTrace();
33 } catch (UnrecoverableKeyException e) {
34 e.printStackTrace();
35 } catch (KeyStoreException e) {
36 e.printStackTrace();
37 } catch (InvalidKeyException e) {
38 e.printStackTrace();
39 } catch (BadPaddingException e) {
40 e.printStackTrace();
41 } catch (IllegalBlockSizeException e) {
42 e.printStackTrace();
43 }
44 return cipher;