-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SecurityManager.java
127 lines (111 loc) · 4.62 KB
/
SecurityManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.getsixtyfour.openvpnmgmt.android.security;
import android.annotation.SuppressLint;
import android.content.Context;
import android.provider.Settings.Secure;
import android.util.Base64;
import android.util.Log;
import androidx.annotation.NonNull;
import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Arrays;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
@SuppressWarnings("Singleton")
public final class SecurityManager {
private static volatile SecurityManager sInstance = null;
private static final String AES_GCM_NO_PADDING = "AES/GCM/NoPadding";
private static final int IV_LENGTH = 16;
private static final String TAG = "SecurityManager";
private SecretKey mKey;
@SuppressWarnings("unused")
private SecurityManager() {
}
@SuppressLint("HardwareIds")
private SecurityManager(Context context) {
String androidId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
try {
byte[] key = androidId.getBytes(StandardCharsets.UTF_8);
MessageDigest sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key, IV_LENGTH);
mKey = new SecretKeySpec(key, "AES");
} catch (NoSuchAlgorithmException e) {
Log.wtf(TAG, e);
}
}
@SuppressWarnings({ "DoubleCheckedLocking", "SynchronizeOnThis" })
@NonNull
public static SecurityManager getInstance(@NonNull Context context) {
if (sInstance == null) {
synchronized (SecurityManager.class) {
if (sInstance == null) {
sInstance = new SecurityManager(context.getApplicationContext());
}
}
}
return sInstance;
}
@NonNull
@SuppressWarnings("WeakerAccess")
public String decryptString(@NonNull String stringToDecrypt) {
if (stringToDecrypt.isEmpty()) {
return stringToDecrypt;
}
String output = stringToDecrypt;
try {
byte[] encryptedBytes = Base64.decode(stringToDecrypt, Base64.DEFAULT);
Cipher cipher = Cipher.getInstance(AES_GCM_NO_PADDING);
AlgorithmParameterSpec ivSpec = new IvParameterSpec(encryptedBytes, 0, IV_LENGTH);
cipher.init(Cipher.DECRYPT_MODE, mKey, ivSpec);
byte[] cipherBytes = cipher.doFinal(encryptedBytes, IV_LENGTH, encryptedBytes.length - IV_LENGTH);
output = new String(cipherBytes, StandardCharsets.UTF_8);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e) {
Log.wtf(TAG, e);
}
return output;
}
@NonNull
@SuppressWarnings("WeakerAccess")
public String encryptString(@NonNull String stringToEncrypt) {
if (stringToEncrypt.isEmpty()) {
return stringToEncrypt;
}
String output = stringToEncrypt;
try {
byte[] clearText = stringToEncrypt.getBytes(StandardCharsets.UTF_8);
Cipher cipher = Cipher.getInstance(AES_GCM_NO_PADDING);
byte[] iv = new byte[IV_LENGTH];
new SecureRandom().nextBytes(iv);
AlgorithmParameterSpec ivSpec = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, mKey, ivSpec);
byte[] cipherBytes = cipher.doFinal(clearText);
output = new String(Base64.encode(concat(iv, cipherBytes), Base64.NO_WRAP), StandardCharsets.UTF_8);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e) {
Log.wtf(TAG, e);
}
return output;
}
private static byte[] concat(byte[]... arrays) {
int length = 0;
for (byte[] array : arrays) {
length += array.length;
}
byte[] result = new byte[length];
int pos = 0;
for (byte[] array : arrays) {
System.arraycopy(array, 0, result, pos, array.length);
pos += array.length;
}
return result;
}
}