1 import javax.crypto.*;
2 import javax.crypto.spec.SecretKeySpec;
3 import java.io.UnsupportedEncodingException;
4 import java.security.InvalidKeyException;
5 import java.security.MessageDigest;
6 import java.security.NoSuchAlgorithmException;
7 import java.security.SecureRandom;
8 import java.util.Base64;
9 import java.util.Random;
10
11 public class EncryptUtil {
12
13 public static String MD5Purity(String plainText) {
14 try {
15 MessageDigest md = MessageDigest.getInstance("MD5");
16 md.update(plainText.getBytes());
17 byte b[] = md.digest();
18 int i;
19 StringBuffer buf = new StringBuffer("");
20 for (int offset = 0; offset < b.length; offset++) {
21 i = b[offset];
22 if (i < 0) {
23 i += 256;
24 }
25 if (i < 16) {
26 buf.append("0");
27 }
28 buf.append(Integer.toHexString(i));
29 }
30 plainText = buf.toString();
31 } catch (NoSuchAlgorithmException e) {
32 e.printStackTrace();
33 }
34 return plainText.toLowerCase();
35 }
36
37 public static int GetRandom(int max, int min) {
38 Random random = new Random();
39 int s = random.nextInt(max) % (max - min + 1) + min;
40 return s;
41 }
42
43 public static String aesEncrypt(String str, String key) throws Exception {
44 if (str == null || key == null) {
45 return null;
46 }
47 str = ZipUtils.gzip(str);
48 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
49 cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));
50 byte[] bytes = cipher.doFinal(str.getBytes("utf-8"));
51 return Base64.getEncoder().encodeToString(bytes);
52 }
53
54
55 public static String aesDecrypt(String str, String key) throws Exception {
56 if (str == null || key == null) {
57 return null;
58 }
59 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
60 cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));
61 byte[] bytes = Base64.getDecoder().decode(str);
62 bytes = cipher.doFinal(bytes);
63 return ZipUtils.gunzip(new String(bytes, "utf-8"));
64 }
65
66
67 public static String MwEncrypt(String shopguid, String token, String seed, String data) throws Exception {
68 int start = GetRandom(9, 3);
69 int end = GetRandom(9, 3);
70 String keyseed = shopguid + token + seed.substring(start * end, start * end + end);
71 String key = EncryptUtil.MD5Purity(keyseed);
72 String encry = EncryptUtil.aesEncrypt(System.currentTimeMillis() + data, key);
73 if (encry.indexOf("==") == encry.length() - 2) {
74 encry = "2" + encry.substring(0, encry.indexOf("=="));
75 ;
76 } else if (encry.indexOf("=") == encry.length() - 1) {
77 encry = "1" + encry.substring(0, encry.indexOf("="));
78 } else {
79 encry = "a" + encry;
80 }
81 String autograph = EncryptUtil.MD5Purity(encry);
82 autograph = start + autograph + end;
83 encry = autograph.toUpperCase() + encry;
84 return encry;
85 }
86
87
88 public static String MwEncryptaut(String shopguid, String token, String seed, String data) throws Exception {
89 int start = GetRandom(9, 3);
90 int end = GetRandom(9, 3);
91 System.out.println("start = [" + start + "], end = [" + end + "]");
92 String keyseed = shopguid + token + seed.substring(start * end, start * end + end);
93 String key = EncryptUtil.MD5Purity(keyseed);
94 String encry = ZipUtils.gzip(System.currentTimeMillis() + data);
95 // EncryptUtil.aesEncrypt(System.currentTimeMillis()+data, key);
96 if (encry.indexOf("==") == encry.length() - 2) {
97 encry = "2" + encry.substring(0, encry.indexOf("=="));
98 ;
99 } else if (encry.indexOf("=") == encry.length() - 1) {
100 encry = "1" + encry.substring(0, encry.indexOf("="));
101 } else {
102 encry = "a" + encry;
103 }
104 String autograph = EncryptUtil.MD5Purity(encry + key);
105 autograph = start + autograph + end;
106 encry = autograph.toUpperCase() + encry;
107 return encry;
108 }
109 public static String MwDecryptaut(String shopguid, String token, String seed, String data) throws Exception {
110 //System.out.println("body_str:"+data);
111 if (data.length() < 35) {
112 throw new Exception("601");
113 }
114 String autograph = data.substring(0, 34);
115 int start = Integer.parseInt(autograph.substring(0, 1));
116 int end = Integer.parseInt(autograph.substring(33, 34));
117 //System.out.println("start:"+start+":::end:"+end);
118 String keyseed = shopguid + token + seed.substring(start * end, start * end + end);
119 String key = EncryptUtil.MD5Purity(keyseed);
120 autograph = autograph.substring(1, 33);
121 data = data.substring(34, data.length());
122 String autograph_ne = EncryptUtil.MD5Purity(data + key).toUpperCase();
123 if (!autograph_ne.equals(autograph)) {
124 throw new Exception("602");
125 }
126 String vl = data.substring(0, 1);
127 data = data.substring(1, data.length());
128 if (vl.equals("2")) {
129 data = data + "==";
130 } else if (vl.equals("1")) {
131 data = data + "=";
132 }
133 String Decryptstr = ZipUtils.gunzip(data);
134 long stl = Long.parseLong(Decryptstr.substring(0, 13));
135 long ltl = System.currentTimeMillis();
136 long lp = (ltl - stl) / (1000 * 60);
137
138 if (Math.abs(lp) > 10) {
139 throw new Exception("603");
140 }
141 Decryptstr = Decryptstr.substring(13, Decryptstr.length());
142 return Decryptstr;
143 }
144 public static void main(String[] args) throws Exception {
145 String str = MwEncryptaut("0001", "0c5f6f217aabb2ea742b0944cd5020f3", "yW2we6Vqf73Qd3pg4mE209949t968lB6957n981i3LtQezfF9z0LDi08d9Li2c52X8h7v5MY3l7000537foITmq49yie8a71lM40", "1");
146 System.out.println(str);
147 String destr= MwDecryptaut("0001", "0c5f6f217aabb2ea742b0944cd5020f3", "yW2we6Vqf73Qd3pg4mE209949t968lB6957n981i3LtQezfF9z0LDi08d9Li2c52X8h7v5MY3l7000537foITmq49yie8a71lM40",str );
148 System.out.println(destr);
149 }
150 */
151 public static String MwDecrypt(String shopguid, String token, String seed, String data) throws Exception {
152 if (data.length() < 35) {
153 throw new Exception("601");
154 }
155 String autograph = data.substring(0, 34);
156 int start = Integer.parseInt(autograph.substring(0, 1));
157 int end = Integer.parseInt(autograph.substring(33, 34));
158 //System.out.println("start:"+start+":::end:"+end);
159 autograph = autograph.substring(1, 33);
160 data = data.substring(34, data.length());
161 String autograph_ne = EncryptUtil.MD5Purity(data).toUpperCase();
162 if (!autograph_ne.equals(autograph)) {
163 throw new Exception("602");
164 }
165 String vl = data.substring(0, 1);
166 data = data.substring(1, data.length());
167 if (vl.equals("2")) {
168 data = data + "==";
169 } else if (vl.equals("1")) {
170 data = data + "=";
171 }
172 //System.out.println("data:"+data);
173 //System.out.println("seed:"+seed);
174 String keyseed = shopguid + token + seed.substring(start * end, start * end + end);
175 String key = EncryptUtil.MD5Purity(keyseed);
176 //System.out.println("key:"+key);
177 String Decryptstr = EncryptUtil.aesDecrypt(data, key);
178 long stl = Long.parseLong(Decryptstr.substring(0, 13));
179 long ltl = System.currentTimeMillis();
180 long lp = (ltl - stl) / (1000 * 60);
181 if (Math.abs(lp) > 10) {
182 throw new Exception("603");
183 }
184 Decryptstr = Decryptstr.substring(13, Decryptstr.length());
185 return Decryptstr;
186 }
187
188 public static byte[] encrypt(String content, String password) {
189 try {
190 KeyGenerator kgen = KeyGenerator.getInstance("AES");
191 kgen.init(128, new SecureRandom(password.getBytes()));
192 SecretKey secretKey = kgen.generateKey();
193 byte[] enCodeFormat = secretKey.getEncoded();
194 SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
195 Cipher cipher = Cipher.getInstance("AES");
196 byte[] byteContent = content.getBytes("utf-8");
197 cipher.init(Cipher.ENCRYPT_MODE, key);
198 byte[] result = cipher.doFinal(byteContent);
199 return result; // ����
200 } catch (NoSuchAlgorithmException e) {
201 e.printStackTrace();
202 } catch (NoSuchPaddingException e) {
203 e.printStackTrace();
204 } catch (InvalidKeyException e) {
205 e.printStackTrace();
206 } catch (UnsupportedEncodingException e) {
207 e.printStackTrace();
208 } catch (IllegalBlockSizeException e) {
209 e.printStackTrace();
210 } catch (BadPaddingException e) {
211 e.printStackTrace();
212 }
213 return null;
214 }
215
216
217 public static byte[] decrypt(byte[] content, String password) {
218 try {
219 KeyGenerator kgen = KeyGenerator.getInstance("AES");
220 kgen.init(128, new SecureRandom(password.getBytes()));
221 SecretKey secretKey = kgen.generateKey();
222 byte[] enCodeFormat = secretKey.getEncoded();
223 SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
224 Cipher cipher = Cipher.getInstance("AES");
225 cipher.init(Cipher.DECRYPT_MODE, key);
226 byte[] result = cipher.doFinal(content);
227 return result; // ����
228 } catch (NoSuchAlgorithmException e) {
229 e.printStackTrace();
230 } catch (NoSuchPaddingException e) {
231 e.printStackTrace();
232 } catch (InvalidKeyException e) {
233 e.printStackTrace();
234 } catch (IllegalBlockSizeException e) {
235 e.printStackTrace();
236 } catch (BadPaddingException e) {
237 e.printStackTrace();
238 }
239 return null;
240 }
241
242
243 public static String parseByte2HexStr(byte buf[]) {
244 StringBuffer sb = new StringBuffer();
245 for (int i = 0; i < buf.length; i++) {
246 String hex = Integer.toHexString(buf[i] & 0xFF);
247 if (hex.length() == 1) {
248 hex = '0' + hex;
249 }
250 sb.append(hex.toUpperCase());
251 }
252 return sb.toString();
253 }
254
255
256 public static byte[] parseHexStr2Byte(String hexStr) {
257 if (hexStr.length() < 1) {
258 return null;
259 }
260 byte[] result = new byte[hexStr.length() / 2];
261 for (int i = 0; i < hexStr.length() / 2; i++) {
262 int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
263 int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
264 result[i] = (byte) (high * 16 + low);
265 }
266 return result;
267 }
268
269 /*****************************************************************************************************************/
270
271 private static String encryptAes(String input, String key) {
272 byte[] crypted = null;
273 try {
274 SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
275 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
276 cipher.init(Cipher.ENCRYPT_MODE, skey);
277 crypted = cipher.doFinal(input.getBytes());
278 } catch (Exception e) {
279 System.out.println(e.toString());
280 }
281 return replaceSpecial(Base64.getEncoder().encodeToString(crypted));
282 }
283
284
285 public static String replaceSpecial(String s) {
286 s = s.replaceAll("=", "-Z");
287 s = s.replaceAll("\+", "Y-B");
288 s = s.replaceAll("/", "X-C");
289 return s;
290 }
291
292
293 public static String encrypt(String data) {
294 String key = "!@#$won9)6*^43^2";
295 return encryptAes(data, key);
296 }
297
298 public static String encryptPassword(String password) {
299 if (password.isEmpty()) {
300 return "";
301 }
302 String val = password + "cardmwee";
303 MessageDigest sh1 = null;
304 try {
305 sh1 = MessageDigest.getInstance("SHA-1");
306 } catch (NoSuchAlgorithmException e) {
307 e.printStackTrace();
308 }
309 sh1.update(val.getBytes());
310 byte[] m = sh1.digest();
311 return byte2hex(m);
312 }
313
314 public static String byte2hex(byte[] b) {
315 String hs = "";
316 String stmp = "";
317 for (int n = 0; n < b.length; n++) {
318 stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
319 if (stmp.length() == 1) {
320 hs = hs + "0" + stmp;
321 } else {
322 hs = hs + stmp;
323 }
324 }
325 return hs;
326 }
327
328 public static String sha1(String decript) {
329 try {
330 MessageDigest digest = java.security.MessageDigest
331 .getInstance("SHA-1");
332 digest.update(decript.getBytes());
333 byte messageDigest[] = digest.digest();
334 // Create Hex String
335 StringBuffer hexString = new StringBuffer();
336
337 for (int i = 0; i < messageDigest.length; i++) {
338 String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
339 if (shaHex.length() < 2) {
340 hexString.append(0);
341 }
342 hexString.append(shaHex);
343 }
344 return hexString.toString();
345
346 } catch (NoSuchAlgorithmException e) {
347 e.printStackTrace();
348 }
349 return "";
350 }
351 }