-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEncryptUtil.java
More file actions
95 lines (82 loc) · 2.61 KB
/
EncryptUtil.java
File metadata and controls
95 lines (82 loc) · 2.61 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
package cn.sinobest.ypgj.util;
import java.math.BigInteger;
import java.security.MessageDigest;
/**
* Author : lihaoquan
* Description : 加密解密工具类
*/
public class EncryptUtil {
public static String getMD5(String str) {
return encode(str, "MD5");
}
public static String getSHA1(String str) {
return encode(str, "SHA-1");
}
public static String getLittleMD5(String str) {
String estr = encode(str, "MD5");
return estr.substring(0, 20);
}
public static String getLittleSHA1(String str) {
String estr = encode(str, "SHA-1");
return estr.substring(0, 20);
}
private static String encode(String str, String type) {
try {
MessageDigest alga = java.security.MessageDigest.getInstance(type);
alga.update(str.getBytes("UTF-8"));
byte[] digesta = alga.digest();
return byte2hex(digesta);
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
public static String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp;
}
return hs.toUpperCase();
}
public static byte[] hex2byte(byte[] b) {
if ((b.length % 2) != 0)
throw new IllegalArgumentException("长度不是偶数");
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n < b.length; n += 2) {
String item = new String(b, n, 2);
b2[n / 2] = (byte) Integer.parseInt(item, 16);
}
return b2;
}
/**
* 异或加密
*
* @param str
* @param key
* @return
*/
public static String xorEncrypt(String str, String key) {
BigInteger strbi = new BigInteger(str.getBytes());
BigInteger keybi = new BigInteger(key.getBytes());
BigInteger encryptbi = strbi.xor(keybi);
return new String(encryptbi.toByteArray());
}
/**
* 异或解密
*
* @param encryptStr
* @param key
* @return
*/
public static String xorDecrypt(String encryptStr, String key) {
BigInteger encryptbi = new BigInteger(encryptStr.getBytes());
BigInteger keybi = new BigInteger(key.getBytes());
BigInteger decryptbi = encryptbi.xor(keybi);
return new String(decryptbi.toByteArray());
}
}