SecurityUtil.java
10.3 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package com.chinabr.jmetertool;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by Wang on 2016/9/23.
* 类描述:加密工具类
* 修改描述:
*/
public class SecurityUtil {
private static final String ALGO = "AES";
private static final String DEFAULT_ENCODING = "UTF-8";
private static final char[] DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
private static final String old_ENCRYPT_KEY = "UymLGWztn9eWhLIR";
private static final String old_APP_SIGN_KEY = "6sZUoGeHzIdmp5u8";
/**
* AES base64 接口请求参数加密
*
* @param key 加密秘钥
* @param params 参数字符串
* @return
*/
private static String paramEncrypt(String key, String params) {
try {
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(DEFAULT_ENCODING), ALGO);
Cipher cipher = Cipher.getInstance(ALGO);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
return replaceBlank(Base64.encodeToString(cipher.doFinal(params.getBytes(DEFAULT_ENCODING)), Base64.URL_SAFE));
} catch (Exception e) {
e.printStackTrace();
throw new IllegalStateException("failed to encrypt password.", e);
}
}
/**
* BizData字段解密
* @param params
* @return
*/
public static String paramDecrypt(String params, String type) {
byte[] bytes= Base64.decode(params,Base64.URL_SAFE);
if("old".equals(type)) {
return aesDecrypt(old_ENCRYPT_KEY,bytes);
} else if("new".equals(type)) {
return aesDecrypt(Constants.AES_KEY,bytes);
}
return null;
}
public static String getAppBizData(String params, String type) {
if("old".equals(type)) {
return paramEncrypt(old_ENCRYPT_KEY,params);
} else if("new".equals(type)) {
return paramEncrypt(Constants.AES_KEY,params);
}
return null;
}
public static String getAppSignKey(String app_id, String biz_data, String imei,
String method, String otaKey, String timestamp,
String token, String uid, String version, String type) {
String builder = "app_id" + "=" + app_id +
"&" + "biz_data" + "=" + biz_data +
"&" + "imei" + "=" + imei +
"&" + "method" + "=" + method +
"&" + "otaKey" + "=" + otaKey +
"&" + "timestamp" + "=" + timestamp +
"&" + "token" + "=" + token +
"&" + "uid" + "=" + uid +
"&" + "version" + "=" + version +
"&" + "key" + "=";
if("old".equals(type)) {
builder = builder + old_APP_SIGN_KEY;
} else if("new".equals(type)) {
builder = builder + Constants.SIGN_KEY;
} else {
return null;
}
return replaceBlank(Base64.encodeToString(MD5ToByte(builder), Base64.URL_SAFE));
}
private static String base64Encrypt(String key, String params) {
try {
return replaceBlank(Base64.encodeToString(params.getBytes(DEFAULT_ENCODING), Base64.URL_SAFE));
} catch (Exception e) {
e.printStackTrace();
throw new IllegalStateException("failed to encrypt password.", e);
}
}
/**
* 去掉空格字符串
*
* @param str
* @return
*/
private static String replaceBlank(String str) {
String dest = "";
if (str != null) {
Pattern BLANK_PATTERN = Pattern.compile("\\s*");
Matcher m = BLANK_PATTERN.matcher(str);
dest = m.replaceAll("");
}
return dest;
}
/**
* AES 加密 通用
*
* @param key 秘钥
* @param input String字符串
* @return
*/
private static byte[] aesEncrypt(String key, String input) {
try {
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(DEFAULT_ENCODING), ALGO);
Cipher cipher = Cipher.getInstance(ALGO);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
return cipher.doFinal(input.getBytes(DEFAULT_ENCODING));
} catch (Exception e) {
e.printStackTrace();
throw new IllegalStateException("failed to encrypt password.", e);
}
}
/**
* AES 解密 通用
*
* @param key 秘钥
* @param input byte数组
* @return
*/
private static String aesDecrypt(String key, byte[] input) {
try {
Cipher cipher = Cipher.getInstance(ALGO);
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(DEFAULT_ENCODING), ALGO);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
return new String(cipher.doFinal(input), DEFAULT_ENCODING);
} catch (Exception e) {
e.printStackTrace();
throw new IllegalStateException("failed to decrypt password.", e);
}
}
/**
* base64 decode
* @param input 输入
* @param flags Base64.URL_SAFE 等
* @return
*/
private static String base64Encode(byte[] input, int flags){
return Base64.encodeToString(input, flags);
}
/**
* base64 encode
* @param input 输入
* @param flags Base64.URL_SAFE 等
* @return
*/
private static byte[] base64Decode(String input, int flags){
return Base64.decode(input,flags);
}
private static char[] encodeHex(byte[] data) {
int l = data.length;
char[] out = new char[l << 1];
for (int i = 0, j = 0; i < l; i++) {
out[j++] = DIGITS[(0xF0 & data[i]) >>> 4];
out[j++] = DIGITS[0x0F & data[i]];
}
return out;
}
private static byte[] decodeHex(char[] data) {
int len = data.length;
if ((len & 0x01) != 0) {
throw new IllegalStateException("Odd number of characters.");
}
byte[] out = new byte[len >> 1];
for (int i = 0, j = 0; j < len; i++) {
int f = toDigit(data[j], j) << 4;
j++;
f = f | toDigit(data[j], j);
j++;
out[i] = (byte) (f & 0xFF);
}
return out;
}
private static int toDigit(char ch, int index) {
int digit = Character.digit(ch, 16);
if (digit == -1) {
throw new IllegalStateException("Illegal hexadecimal character " + ch + " at index " + index);
}
return digit;
}
private static String MD5(String sourceStr) {
String result = "";
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(sourceStr.getBytes());
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
result = buf.toString();
} catch (NoSuchAlgorithmException e) {
}
return result.toString().substring(8, 24);
}
private static String MD5ToHex(String sourceStr) {
String result = "";
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(sourceStr.getBytes());
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
result = buf.toString();
} catch (NoSuchAlgorithmException e) {
}
return result.toString();
}
private static byte[] MD5ToByte(String sourceStr) {
byte b[] = new byte[32];
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(sourceStr.getBytes());
b = md.digest();
return b;
} catch (NoSuchAlgorithmException e) {
}
return b;
}
public static void main(String[] args) {
String a = "wfIoA0F5ojfBm8PoKx4JoA==";
System.out.println(paramDecrypt(a,"new"));
String b = "{ \"id\": \"0\" }";
System.out.println(getAppBizData(b,"new"));
System.out.println(getAppBizData(b,"old"));
// String biz_data = "{\"id\":\"0\"}";
// String method = "com.lejane.handler.common.app.launch.advertise.query";
// String uid = "11406869";
// String token = "7HMuJHHSPOsEeuG1_0o9h3QiPursw8WTMejfVG6ba-jkArmocfW6tN4-dOo355_7GdwrCjA1TSD27jHAjh-wIFtFapzp1IiaZVyORdqGqSwmDvENOecheyWYzds50A2AT-FYxChOFlUSAJjcXig7WUjHTQZrJ_b5jU3l_L-VNik=";
// RequestParamsBean requestParamsBean = new RequestParamsBean();
// requestParamsBean.setApp_id("110");
// requestParamsBean.setImei("bc28204e3c3767af81791d485ce8946500a0178e");
// requestParamsBean.setPlatform("iOS");
// requestParamsBean.setVersion("1.21.0");
// requestParamsBean.setOtaKey("e86ce4752ba46a06035db951531caf2d");
// requestParamsBean.setToken(token);
// requestParamsBean.setUid(uid);
// requestParamsBean.setMethod(method);
// biz_data = SecurityUtil.getAppBizData(biz_data,"old");
// requestParamsBean.setBiz_data(biz_data);
// String sign = SecurityUtil.getAppSignKey(requestParamsBean.getApp_id(),requestParamsBean.getBiz_data(),requestParamsBean.getImei(),requestParamsBean.getMethod(),requestParamsBean.getOtaKey(),requestParamsBean.getTimestamp(),requestParamsBean.getToken(),requestParamsBean.getUid(),requestParamsBean.getVersion(),"old");
// requestParamsBean.setSign(sign);
// String request_params = JSONObject.toJSONString(requestParamsBean);
// System.out.println(biz_data);
// System.out.println(sign);
// System.out.println(requestParamsBean.getTimestamp());
}
}