本文提供了基于MD5加密16位和32位的方法,具體內(nèi)容如下
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
|
import java.io.IOException; import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; /** * <p>標題:編碼工具類</p> * <p>功能:對數(shù)據(jù)進行編碼轉(zhuǎn)換</p> * 作者:趙力 */ public class EncodeUtil { public static void main(String[] args) throws Exception { System.out.println(md5Encrypt16( "需要進行MD5加密的字符串" )); } /** * MD5加密16位 * @param encryptStr 要加密數(shù)據(jù) * @return 返回16位加密結(jié)果 * ZhaoLi */ public static String md5Encrypt16(String encryptStr) { return md5Encrypt32(encryptStr).substring( 8 , 24 ); } /** * MD5加密32位 * @param encryptStr 要加密數(shù)據(jù) * @return 32位加密結(jié)果 * ZhaoLi */ public static String md5Encrypt32(String encryptStr) { MessageDigest md5; try { md5 = MessageDigest.getInstance( "MD5" ); byte [] md5Bytes = md5.digest(encryptStr.getBytes()); StringBuffer hexValue = new StringBuffer(); for ( int i = 0 ; i < md5Bytes.length; i++) { int val = (md5Bytes[i]) & 0xff ; if (val < 16 ) { hexValue.append( "0" ); } hexValue.append(Integer.toHexString(val)); } return hexValue.toString().toLowerCase(); } catch (Exception e) { throw new RuntimeException(e); } } /** * 結(jié)合base64實現(xiàn)md5加密 * @param msg 待加密字符串 * @return 獲取md5后轉(zhuǎn)為base64 * @throws Exception */ { return msg == null ? null : base64Encode(md5(msg)); } /** * 將byte[]轉(zhuǎn)為各種進制的字符串 * @param bytes byte[] * @param radix 可以轉(zhuǎn)換進制的范圍,從Character.MIN_RADIX到Character.MAX_RADIX,超出范圍后變?yōu)?0進制 * @return 轉(zhuǎn)換后的字符串 */ public static String binary( byte [] bytes, int radix) { return new BigInteger( 1 , bytes).toString(radix); // 這里的1代表正數(shù) } /** * base 64 encode * @param bytes 待編碼的byte[] * @return 編碼后的base 64 code */ public static String base64Encode( byte [] bytes) { return new BASE64Encoder().encode(bytes); } /** * base 64 decode * @param base64Code 待解碼的base 64 code * @return 解碼后的byte[] * @throws Exception */ public static byte [] base64Decode(String base64Code) { try { return base64Code == null ? null : new BASE64Decoder().decodeBuffer(base64Code); } catch (IOException e) { throw new RuntimeException( "報錯內(nèi)容" , e); } } /** * 獲取byte[]的md5值 * @param bytes byte[] * @return md5 * @throws Exception */ public static byte [] md5( byte [] bytes) { MessageDigest md; try { md = MessageDigest.getInstance( "MD5" ); } catch (NoSuchAlgorithmException e) { throw new RuntimeException( "報錯內(nèi)容" , e); } md.update(bytes); return md.digest(); } /** * 獲取字符串md5值 * @param msg * @return md5 * @throws Exception */ public static byte [] md5(String msg) { return msg == null ? null : md5(msg.getBytes()); } } |
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/ikcai/p/7509221.html