· 9 years ago · Oct 17, 2016, 11:46 AM
1public class AES_Helper {
2
3private Cipher cipherEncrypt, cipherDecrypt;
4
5
6public static String encode(String stringToEncode) throws NullPointerException {
7 if (stringToEncode == null) {
8 return "";
9 } else if (stringToEncode.equals("")) {
10 return "";
11 } else {
12 try {
13 SecretKeySpec skeySpec = getKey();
14 byte[] clearText = stringToEncode.getBytes("UTF8");
15
16 // IMPORTANT TO GET SAME RESULTS ON iOS and ANDROID
17 final byte[] iv = new byte[16];
18 Arrays.fill(iv, (byte) 0x00);
19 IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
20
21 // Cipher is not thread safe
22 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
23 cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);
24
25 String encrypedValue = Base64.encodeToString(cipher.doFinal(clearText), Base64.DEFAULT);
26 Log.d("jacek", "Encrypted: " + stringToEncode + " -> " + encrypedValue);
27 return encrypedValue.replace("n", "");
28 //return encrypedValue;
29 } catch (UnsupportedEncodingException |BadPaddingException | IllegalBlockSizeException | NoSuchAlgorithmException |
30 InvalidKeyException | NoSuchPaddingException | InvalidAlgorithmParameterException e) {
31 e.printStackTrace();
32 }
33 return "";
34 }
35}
36
37
38public static String decode(String text) throws NullPointerException {
39
40 if (text == null) {
41 return "";
42
43 } else if (text.equals("")) {
44 return "";
45
46 } else {
47
48
49 try
50
51 {
52 SecretKey key = getKey();
53
54 // IMPORTANT TO GET SAME RESULTS ON iOS and ANDROID
55 final byte[] iv = new byte[16];
56 Arrays.fill(iv, (byte) 0x00);
57 IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
58
59 byte[] encrypedPwdBytes = Base64.decode(text, Base64.DEFAULT);
60 // cipher is not thread safe
61 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
62 cipher.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);
63 byte[] decrypedValueBytes = (cipher.doFinal(encrypedPwdBytes));
64
65 return new String(decrypedValueBytes);
66
67 } catch (Exception e)
68
69 {
70 e.printStackTrace();
71 }
72 return "";
73 }
74}
75
76
77private static SecretKeySpec getKey() throws UnsupportedEncodingException {
78
79 // You can change it to 128 if you wish
80 int keyLength = 256;
81 byte[] keyBytes = new byte[keyLength / 8];
82 // explicitly fill with zeros
83 Arrays.fill(keyBytes, (byte) 0x0);
84
85 // if password is shorter then key length, it will be zero-padded
86 // to key length
87 byte[] passwordBytes = KEY_MESSAGE_ENCRYPT.getBytes("UTF-8");
88 int length = passwordBytes.length < keyBytes.length ? passwordBytes.length : keyBytes.length;
89 System.arraycopy(passwordBytes, 0, keyBytes, 0, length);
90 return new SecretKeySpec(keyBytes, "AES");
91}
92
93private static SecretKeySpec getKeyWithCustomKey(String securityKey) throws UnsupportedEncodingException {
94
95 // You can change it to 128 if you wish
96 int keyLength = 256;
97 byte[] keyBytes = new byte[keyLength / 8];
98 // explicitly fill with zeros
99 Arrays.fill(keyBytes, (byte) 0x0);
100
101 // if password is shorter then key length, it will be zero-padded
102 // to key length
103 byte[] passwordBytes = securityKey.getBytes("UTF-8");
104 int length = passwordBytes.length < keyBytes.length ? passwordBytes.length : keyBytes.length;
105 System.arraycopy(passwordBytes, 0, keyBytes, 0, length);
106 return new SecretKeySpec(keyBytes, "AES");
107}
108
109}