-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncrypt.java
More file actions
144 lines (135 loc) · 3.15 KB
/
Encrypt.java
File metadata and controls
144 lines (135 loc) · 3.15 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
package cn.encrypt;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
/**
* @Project:
* @Title: Encrypt.java
* @Description: 对字符串进行加密、解密操作
* @Company:
* @Author:
* @Date:
* @version 1.0
*/
public class Encrypt {
static byte[] key = "192.168.".getBytes();
/**
* 将字符串加密 不可恢复
*
* @param msg
* 需要加密的字符串
* @return 返回加密后的字符串
*/
public static String computeDigest(String msg) {
try {
java.security.MessageDigest alg = java.security.MessageDigest.getInstance("SHA-1");
alg.reset();
alg.update(msg.getBytes());
byte[] hash = alg.digest();
String digest = "";
for (int i = 0; i < hash.length; i++) {
int v = hash[i] & 0xFF;
v = v & 0xAB;
if (v < 16)
digest += "0";
digest += Integer.toString(v, 16).toUpperCase();
}
return digest;
} catch (Exception e) {
e.printStackTrace();
return msg;
}
}
/**
* 可逆的加密算法
*
* @param str
* @return
* @throws Exception
*/
public static String encode(String str) {
String strTemp = "";
try {
byte[] input = str.getBytes();
SecretKey deskey = new javax.crypto.spec.SecretKeySpec(key, "DES");
Cipher c1 = Cipher.getInstance("DES");
c1.init(Cipher.ENCRYPT_MODE, deskey);
byte[] cipherByte = c1.doFinal(input);
strTemp = byte2hex(cipherByte);
} catch (Exception e) {
e.printStackTrace();
}
return strTemp;
}
/**
* 针对encode方法的解密
*
* @param str
* @return
* @throws Exception
*/
public static String decode(String str) {
if (str == null)
return "";
String strTemp = "";
try {
byte[] input = hex2byte(str);
SecretKey deskey = new javax.crypto.spec.SecretKeySpec(key, "DES");
Cipher c1 = Cipher.getInstance("DES");
c1.init(Cipher.DECRYPT_MODE, deskey);
byte[] clearByte = c1.doFinal(input);
strTemp = new String(clearByte);
} catch (Exception e) {
e.printStackTrace();
}
return strTemp;
}
/**
* 字节码转换成16进制字符串
*
* @param b
* @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();
}
/**
* 16进制字符串转换成字节码
*
* @param h
* @return
*/
public static byte[] hex2byte(String h) {
byte[] ret = new byte[h.length() / 2];
for (int i = 0; i < ret.length; i++) {
ret[i] = Integer.decode("#" + h.substring(2 * i, 2 * i + 2)).byteValue();
}
return ret;
}
/**
* 解密token返回user_id
*
* @param token
* @return 2016-12-19 下午02:06:42
*/
public static String getUserId(String token) {
return decode(token).split("_")[0];
}
/**
* 解密token返回device_token
*
* @param token
* @return 2016-12-19 下午02:06:59
*/
public static String getDeviceToken(String token) {
return decode(token).split("_")[2];
}
}