Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
executable file
·
176 lines (146 loc) · 3.61 KB

File metadata and controls

executable file
·
176 lines (146 loc) · 3.61 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package array;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class AESUtil {
private static final String AES = "AES";
private static final String CHARSET_NAME = "utf-8";
/**
* 获取密钥
*
* @param password
* 加密密码
* @return
* @throws NoSuchAlgorithmException
*/
private static SecretKeySpec getKey(String password)
throws NoSuchAlgorithmException {
// 密钥加密器生成器
KeyGenerator kgen = KeyGenerator.getInstance(AES);
kgen.init(128, new SecureRandom(password.getBytes()));
// 创建加密器
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, AES);
return key;
}
/**
* 加密
*
* @param str
* 原文
* @param password
* 加密密码
* @return
*/
public static String encode(String str, String password) {
byte[] arr = encodeToArr(str, password);
return byteArrToString(arr);
}
/**
*
* 加密
*
* @author wuhongbo
* @param str
* 原文
* @param password
* 加密密码
* @return
*/
private static byte[] encodeToArr(String str, String password) {
try {
Cipher cipher = Cipher.getInstance(AES);// 创建密码器
byte[] byteContent = str.getBytes(CHARSET_NAME);
cipher.init(Cipher.ENCRYPT_MODE, getKey(password));// 初始化
byte[] result = cipher.doFinal(byteContent);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 解密
*
* @param hexStr
* 密文
* @param password
* 加密密码
* @return
*/
public static String decode(String hexStr, String password) {
byte[] arr = string2ByteArr(hexStr);
return decode(arr, password);
}
/**
* 解密
*
* @author wuhongbo
* @param arr
* 密文数组
* @param password
* 加密密码
* @return
*/
private static String decode(byte[] arr, String password) {
try {
// 创建密码器
Cipher cipher = Cipher.getInstance(AES);
cipher.init(Cipher.DECRYPT_MODE, getKey(password));// 初始化
byte[] result = cipher.doFinal(arr);
return new String(result, CHARSET_NAME);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* byte数组转成16进制字符串
*
* @param arr
* @return
*/
private static String byteArrToString(byte[] arr) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < arr.length; i++) {
String s = Integer.toString(arr[i] + 128, 16);
if (s.length() == 1) {
s = "0" + s;
}
sb.append(s);
}
return sb.toString().toUpperCase();
}
/**
* 16进制字符串转成byte数组
*
* @param arr
* @return
*/
private static byte[] string2ByteArr(String s) {
s = s.toUpperCase();
String str = "0123456789ABCDEF";
byte[] arr = new byte[s.length() / 2];
for (int i = 0; i < arr.length; i++) {
char s1 = s.charAt(i * 2);
char s2 = s.charAt(i * 2 + 1);
int tmp1 = str.indexOf(s1) * 16;
int tmp2 = str.indexOf(s2);
arr[i] = (byte) (tmp1 + tmp2 - 128);
}
return arr;
}
public static void main(String[] args) {
String password = "yahier";
String content = "123456";
System.out.println("原文:" + content);
String hexStr = encode(content, password);
System.out.println("密文长度:" + hexStr.length());
System.out.println("密文:" + hexStr);
System.out.println("解密:" + decode(hexStr, password));
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.