· 6 years ago · Mar 25, 2019, 07:30 AM
1public class PrefCryptor {
2 private Cipher _cx;
3 private byte[] _iv;
4 private static PrefCryptor instance;
5 private static Object mutext = new Object();
6 private static final String TAG = PrefCryptor.class.getName();
7 private SecretKey secretKey;
8
9 public static PrefCryptor getInstance() {
10 if (instance == null) {
11 synchronized (mutext) {
12 instance = new PrefCryptor();
13 }
14 }
15 return instance;
16 }
17
18
19
20 private PrefCryptor() {
21 init();
22 }
23
24 private void init() {
25 try {
26 _cx = Cipher.getInstance("AES/CBC/PKCS5Padding");
27 secretKey =KeyChainUtils.getInstance().getKey();
28 _iv = getIV16().getBytes("UTF-8");
29 } catch ( NoSuchAlgorithmException | NoSuchPaddingException | UnsupportedEncodingException e) {
30 e.printStackTrace();
31 }
32 }
33
34 private byte[] encryptDecrypt(String inputText, int mode) throws UnsupportedEncodingException,
35 InvalidKeyException, InvalidAlgorithmParameterException,
36 IllegalBlockSizeException, BadPaddingException {
37 // SecretKeySpec keySpec = new SecretKeySpec(secretKey, "AES");
38 IvParameterSpec ivSpec = new IvParameterSpec(_iv);
39 // encryption
40 if (mode == Cipher.ENCRYPT_MODE) {
41 _cx.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);// Initialize this cipher instance
42 return _cx.doFinal(inputText.getBytes("UTF-8")); // Finish multi-part transformation (encryption)
43 } else {
44 _cx.init(Cipher.DECRYPT_MODE, secretKey, ivSpec);// Initialize this cipher instance
45 byte[] decodedValue = Base64.decode(inputText.getBytes(), Base64.DEFAULT);
46 return _cx.doFinal(decodedValue);// Finish multi-part transformation (decryption)
47 }
48 }
49
50 public String encryptData(String plainText) {
51 if(_cx==null)
52 init();
53 if (_cx != null) {
54 try {
55 byte[] bytes = encryptDecrypt(plainText, Cipher.ENCRYPT_MODE);
56 return Base64.encodeToString(bytes, Base64.DEFAULT);
57 } catch (Exception e) {
58 e.printStackTrace();
59 }
60 } else {
61 Log.e(TAG, "Crypto not initialized yet");
62 }
63 return null;
64 }
65
66 public String decryptData(String cipherText) {
67 if(_cx==null)
68 init();
69 if (_cx != null) {
70 if (!TextUtils.isEmpty(cipherText)) {
71 try {
72 byte[] bytes = encryptDecrypt(cipherText, Cipher.DECRYPT_MODE);
73 return new String(bytes);
74 } catch (Exception e) {
75 e.printStackTrace();
76 }
77 }
78 }else {
79 Log.e(TAG, "Crypto not initialized yet");
80 }
81 return cipherText;
82 }
83
84 public String getIV16() {
85 SharedPreferences keyPreference = PreferenceManager.getDefaultSharedPreferences(MyApplication.getApplication());
86 String keyIV=keyPreference.getString("keyIV_EC","");
87 if(TextUtils.isEmpty(keyIV)){
88 SecureRandom ranGen = new SecureRandom();
89 byte[] aesKey = new byte[16];
90 ranGen.nextBytes(aesKey);
91 StringBuilder result = new StringBuilder();
92 for (byte b : aesKey) {
93 result.append(String.format("%02x", b)); //convert to hex
94 }
95 if (16 > result.toString().length()) {
96 keyIV= result.toString();
97 } else {
98 keyIV= result.toString().substring(0, 16);
99 }
100 keyPreference.edit().putString("keyIV_EC",keyIV).apply();
101 }
102 return keyIV;
103 }
104}