· 7 years ago · Jan 08, 2019, 09:56 AM
1case DialogInterface.BUTTON_POSITIVE:
2
3 EnCryptor encryptor = new EnCryptor();
4 try {
5 passwordInByte = encryptor.encryptText(Global.ENCRYPTED_PASSWORD, login.loginPassword);
6
7 } catch ( NoSuchAlgorithmException
8 | NoSuchProviderException
9 | InvalidAlgorithmParameterException
10 | InvalidKeyException
11 | NoSuchPaddingException
12 | BadPaddingException
13 | IllegalBlockSizeException
14 | IOException exc) {
15 exc.printStackTrace();
16 }
17 SharedPreferences settings = getSharedPreferences(Global.data, Context.MODE_PRIVATE);
18 settings.edit().putBoolean("use_fingerprint_authentication", true).commit();
19 settings.edit().putString("userName", login.loginUser).commit();
20 settings.edit().putString("userPassword", Arrays.toString(passwordInByte)).commit();
21
22public class EnCryptor {
23
24 private static final String TRANSFORMATION = "AES/GCM/NoPadding";
25 private static final String ANDROID_KEY_STORE = "AndroidKeyStore";
26
27 private byte[] encryption;
28 private byte[] iv;
29
30 public EnCryptor() {}
31
32 public byte[] encryptText(final String alias, final String textToEncrypt)
33 throws NoSuchAlgorithmException,
34 NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, IOException,
35 InvalidAlgorithmParameterException, BadPaddingException,
36 IllegalBlockSizeException {
37
38 final Cipher cipher = Cipher.getInstance(TRANSFORMATION);
39 cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(alias));
40
41 iv = cipher.getIV();
42
43 return (encryption = cipher.doFinal(textToEncrypt.getBytes("UTF-8")));
44 }
45
46 @TargetApi(Build.VERSION_CODES.M)
47 @NonNull
48 private SecretKey getSecretKey(final String alias) throws NoSuchAlgorithmException,
49 NoSuchProviderException, InvalidAlgorithmParameterException {
50
51 final KeyGenerator keyGenerator = KeyGenerator
52 .getInstance(KeyProperties.KEY_ALGORITHM_AES, ANDROID_KEY_STORE);
53
54 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
55 keyGenerator.init(new KeyGenParameterSpec.Builder(alias,
56 KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
57 .setBlockModes(KeyProperties.BLOCK_MODE_GCM)
58 .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
59 .build());
60 }
61
62 return keyGenerator.generateKey();
63 }
64
65 public byte[] getEncryption() {
66 return encryption;
67 }
68
69 public byte[] getIv(String alias) throws NoSuchPaddingException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, InvalidKeyException {
70
71 final Cipher cipher = Cipher.getInstance(TRANSFORMATION);
72 try {
73 cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(alias));
74 } catch (InvalidKeyException e) {
75 e.printStackTrace();
76 } catch (NoSuchAlgorithmException e) {
77 e.printStackTrace();
78 } catch (NoSuchProviderException e) {
79 e.printStackTrace();
80 } catch (InvalidAlgorithmParameterException e) {
81 e.printStackTrace();
82 }
83
84 iv = cipher.getIV();
85 return iv;
86 }
87}
88
89SharedPreferences preferences = context.getSharedPreferences(Global.data,
90Context.MODE_PRIVATE);
91 Login.Response res;
92 byte[] array = null;
93 try {
94 DeCryptor decryptor = new DeCryptor();
95 EnCryptor enCryptor = new EnCryptor();
96 //byte[] encryptedBytes = Base64.decode(preferences.getString("userPassword", ""), Base64.DEFAULT);
97 //byte[] passByte = preferences.getString("userPassword", "").getBytes(Charset.forName("UTF-8"));
98
99 String stringArray = preferences.getString("userPassword", null);
100
101 if (stringArray != null) {
102 String[] split = stringArray.substring(1, stringArray.length()-1).split(", ");
103 array = new byte[split.length];
104 for (int i = 0; i < split.length; i++) {
105 array[i] = Byte.parseByte(split[i]);
106 }
107 }
108 String parsedPassword = decryptor.decryptData(Global.ENCRYPTED_PASSWORD, array, enCryptor.getIv(Global.ENCRYPTED_PASSWORD));
109
110public class DeCryptor {
111
112 private static final String TRANSFORMATION = "AES/GCM/NoPadding";
113 private static final String ANDROID_KEY_STORE = "AndroidKeyStore";
114
115 private KeyStore keyStore;
116
117 public DeCryptor() throws CertificateException, NoSuchAlgorithmException, KeyStoreException,
118 IOException {
119 initKeyStore();
120 }
121
122 private void initKeyStore() throws KeyStoreException, CertificateException,
123 NoSuchAlgorithmException, IOException {
124 keyStore = KeyStore.getInstance(ANDROID_KEY_STORE);
125 keyStore.load(null);
126 }
127
128 @TargetApi(Build.VERSION_CODES.KITKAT)
129 public String decryptData(final String alias, final byte[] encryptedData, final byte[] encryptionIv)
130 throws UnrecoverableEntryException, NoSuchAlgorithmException, KeyStoreException,
131 NoSuchPaddingException, InvalidKeyException, IOException,
132 BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException {
133
134 final Cipher cipher = Cipher.getInstance(TRANSFORMATION);
135 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
136 final GCMParameterSpec spec = new GCMParameterSpec(128, encryptionIv);
137
138 cipher.init(Cipher.DECRYPT_MODE, getSecretKey(alias), spec);
139 }
140 return new String(cipher.doFinal(encryptedData), "UTF-8");
141 }
142
143 private SecretKey getSecretKey(final String alias) throws NoSuchAlgorithmException,
144 UnrecoverableEntryException, KeyStoreException {
145 return ((KeyStore.SecretKeyEntry) keyStore.getEntry(alias, null)).getSecretKey();
146 }
147}