-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAESUtil.java
More file actions
54 lines (47 loc) · 1.72 KB
/
Copy pathAESUtil.java
File metadata and controls
54 lines (47 loc) · 1.72 KB
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
package mo_phong_zalo2;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class AESUtil {
private static final String KEY = "1234567890123456";
public static byte[] encryptBytes(byte[] data) {
try {
SecretKeySpec key = new SecretKeySpec(KEY.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(data);
} catch (Exception e) {
return data;
}
}
public static byte[] decryptBytes(byte[] encrypted) {
try {
SecretKeySpec key = new SecretKeySpec(KEY.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(encrypted);
} catch (Exception e) {
return encrypted;
}
}
public static String encrypt(String text) {
try {
SecretKeySpec key = new SecretKeySpec(KEY.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
return Base64.getEncoder().encodeToString(cipher.doFinal(text.getBytes()));
} catch (Exception e) {
return text;
}
}
public static String decrypt(String encrypted) {
try {
SecretKeySpec key = new SecretKeySpec(KEY.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
return new String(cipher.doFinal(Base64.getDecoder().decode(encrypted)));
} catch (Exception e) {
return encrypted;
}
}
}