|
@@ -0,0 +1,133 @@
|
|
1
|
+package cn.lili.common.utils;
|
|
2
|
+
|
|
3
|
+import javax.crypto.*;
|
|
4
|
+import javax.crypto.spec.SecretKeySpec;
|
|
5
|
+import java.io.UnsupportedEncodingException;
|
|
6
|
+import java.nio.charset.StandardCharsets;
|
|
7
|
+import java.security.InvalidKeyException;
|
|
8
|
+import java.security.NoSuchAlgorithmException;
|
|
9
|
+import java.security.SecureRandom;
|
|
10
|
+
|
|
11
|
+public class AesEncrypt {
|
|
12
|
+ public AesEncrypt() {
|
|
13
|
+ }
|
|
14
|
+
|
|
15
|
+ public static String encrypt(String content, String password) {
|
|
16
|
+ try {
|
|
17
|
+ KeyGenerator kgen = KeyGenerator.getInstance("AES");
|
|
18
|
+ // 防止Linux下随机生成Key
|
|
19
|
+ SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
|
|
20
|
+ secureRandom.setSeed(password.getBytes());
|
|
21
|
+ kgen.init(128, secureRandom);
|
|
22
|
+ // kgen.init(128, new SecureRandom(password.getBytes()));
|
|
23
|
+ SecretKey secretKey = kgen.generateKey();
|
|
24
|
+ byte[] enCodeFormat = secretKey.getEncoded();
|
|
25
|
+ SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
|
|
26
|
+ Cipher cipher = Cipher.getInstance("AES");
|
|
27
|
+ byte[] byteContent = content.getBytes("utf-8");
|
|
28
|
+ cipher.init(1, key);
|
|
29
|
+ byte[] result = cipher.doFinal(byteContent);
|
|
30
|
+ return parseByte2HexStr(result);
|
|
31
|
+ } catch (NoSuchAlgorithmException var9) {
|
|
32
|
+ var9.printStackTrace();
|
|
33
|
+ } catch (NoSuchPaddingException var10) {
|
|
34
|
+ var10.printStackTrace();
|
|
35
|
+ } catch (InvalidKeyException var11) {
|
|
36
|
+ var11.printStackTrace();
|
|
37
|
+ } catch (UnsupportedEncodingException var12) {
|
|
38
|
+ var12.printStackTrace();
|
|
39
|
+ } catch (IllegalBlockSizeException var13) {
|
|
40
|
+ var13.printStackTrace();
|
|
41
|
+ } catch (BadPaddingException var14) {
|
|
42
|
+ var14.printStackTrace();
|
|
43
|
+ }
|
|
44
|
+
|
|
45
|
+ return null;
|
|
46
|
+ }
|
|
47
|
+
|
|
48
|
+ public static String decrypt(String encrypt, String password) {
|
|
49
|
+ try {
|
|
50
|
+ byte[] decode = parseHexStr2Byte(encrypt);
|
|
51
|
+ KeyGenerator kgen = KeyGenerator.getInstance("AES");
|
|
52
|
+ // 防止Linux下随机生成Key
|
|
53
|
+ SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
|
|
54
|
+ secureRandom.setSeed(password.getBytes());
|
|
55
|
+ kgen.init(128, secureRandom);
|
|
56
|
+ SecretKey secretKey = kgen.generateKey();
|
|
57
|
+ byte[] enCodeFormat = secretKey.getEncoded();
|
|
58
|
+ SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
|
|
59
|
+ Cipher cipher = Cipher.getInstance("AES");
|
|
60
|
+ cipher.init(2, key);
|
|
61
|
+ byte[] result = cipher.doFinal(decode);
|
|
62
|
+ return new String(result, StandardCharsets.UTF_8);
|
|
63
|
+ } catch (NoSuchAlgorithmException var9) {
|
|
64
|
+ var9.printStackTrace();
|
|
65
|
+ } catch (NoSuchPaddingException var10) {
|
|
66
|
+ var10.printStackTrace();
|
|
67
|
+ } catch (InvalidKeyException var11) {
|
|
68
|
+ var11.printStackTrace();
|
|
69
|
+ } catch (IllegalBlockSizeException var12) {
|
|
70
|
+ var12.printStackTrace();
|
|
71
|
+ } catch (BadPaddingException var13) {
|
|
72
|
+ var13.printStackTrace();
|
|
73
|
+ }
|
|
74
|
+
|
|
75
|
+ return null;
|
|
76
|
+ }
|
|
77
|
+
|
|
78
|
+ private static String parseByte2HexStr(byte[] buf) {
|
|
79
|
+ StringBuffer sb = new StringBuffer();
|
|
80
|
+
|
|
81
|
+ for(int i = 0; i < buf.length; ++i) {
|
|
82
|
+ String hex = Integer.toHexString(buf[i] & 255);
|
|
83
|
+ if (hex.length() == 1) {
|
|
84
|
+ hex = '0' + hex;
|
|
85
|
+ }
|
|
86
|
+
|
|
87
|
+ sb.append(hex.toUpperCase());
|
|
88
|
+ }
|
|
89
|
+
|
|
90
|
+ return sb.toString();
|
|
91
|
+ }
|
|
92
|
+
|
|
93
|
+ private static byte[] parseHexStr2Byte(String hexStr) {
|
|
94
|
+ if (hexStr.length() < 1) {
|
|
95
|
+ return null;
|
|
96
|
+ } else {
|
|
97
|
+ byte[] result = new byte[hexStr.length() / 2];
|
|
98
|
+
|
|
99
|
+ for(int i = 0; i < hexStr.length() / 2; ++i) {
|
|
100
|
+ int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
|
|
101
|
+ int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
|
|
102
|
+ result[i] = (byte)(high * 16 + low);
|
|
103
|
+ }
|
|
104
|
+
|
|
105
|
+ return result;
|
|
106
|
+ }
|
|
107
|
+ }
|
|
108
|
+
|
|
109
|
+ public static byte[] encrypt2(String content, String password) {
|
|
110
|
+ try {
|
|
111
|
+ SecretKeySpec key = new SecretKeySpec(password.getBytes(), "AES");
|
|
112
|
+ Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
|
|
113
|
+ byte[] byteContent = content.getBytes("utf-8");
|
|
114
|
+ cipher.init(1, key);
|
|
115
|
+ byte[] result = cipher.doFinal(byteContent);
|
|
116
|
+ return result;
|
|
117
|
+ } catch (NoSuchAlgorithmException var6) {
|
|
118
|
+ var6.printStackTrace();
|
|
119
|
+ } catch (NoSuchPaddingException var7) {
|
|
120
|
+ var7.printStackTrace();
|
|
121
|
+ } catch (InvalidKeyException var8) {
|
|
122
|
+ var8.printStackTrace();
|
|
123
|
+ } catch (UnsupportedEncodingException var9) {
|
|
124
|
+ var9.printStackTrace();
|
|
125
|
+ } catch (IllegalBlockSizeException var10) {
|
|
126
|
+ var10.printStackTrace();
|
|
127
|
+ } catch (BadPaddingException var11) {
|
|
128
|
+ var11.printStackTrace();
|
|
129
|
+ }
|
|
130
|
+
|
|
131
|
+ return null;
|
|
132
|
+ }
|
|
133
|
+}
|