MD5Util
/**
* @Author: CodeSleep
* @Date: 2021/7/20 10:56
* @Description: //TODO 自定义MD5算法实现加密
*/
public class MD5Util {
/**
* 加密算法
*
* @param str
* @return
*/
public static String getEncodeByMd5(String str) {
try {
int i;
// 得到MessageDigest的对象
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(str.getBytes());
byte b[] = md.digest();
// 创建StringBuffer对象
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));
}
// 返回结果为32位加密
return buf.toString();
// 返回结果为16位的加密
// return buf.toString().substring(8, 24);
} catch (NoSuchAlgorithmException e) {
// e.printStackTrace();
return null;
}
}
}
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
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
编辑 (opens new window)