最近学习加密算法的知识,利用MD5 加密,百度一下网上资料很多,不是很详细,这里就整理下如何实现用MD5加密和 哈希散列带秘钥加密算法,大家可以看下。
实现代码:
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
|
package com.ompa.common.utils; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import javax.crypto.Mac; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; /** * 采用MD5加密 * * @author zhangcd * @date 2016-4-29 */ public class EncryptUtil { private static final String MAC_NAME = "HmacSHA1" ; private static final String ENCODING = "UTF-8" ; private static final String key = "iloveyou" ; /*** * MD5加码 生成32位md5码 */ public static String string2MD5(String inStr){ MessageDigest md5 = null ; try { md5 = MessageDigest.getInstance( "MD5" ); } catch (Exception e){ System.out.println(e.toString()); e.printStackTrace(); return "" ; } char [] charArray = inStr.toCharArray(); byte [] byteArray = new byte [charArray.length]; for ( int i = 0 ; i < charArray.length; i++) byteArray[i] = ( byte ) charArray[i]; byte [] md5Bytes = md5.digest(byteArray); StringBuffer hexValue = new StringBuffer(); for ( int i = 0 ; i < md5Bytes.length; i++){ int val = (( int ) md5Bytes[i]) & 0xff ; if (val < 16 ) hexValue.append( "0" ); hexValue.append(Integer.toHexString(val)); } return hexValue.toString(); } /*** * MD5加密 生成32位md5码 */ public static String stringMD5(String inStr){ return string2MD5(string2MD5(inStr)); } /** * 加密解密算法 */ public static String convertMD5(String inStr){ char [] a = inStr.toCharArray(); for ( int i = 0 ; i < a.length; i++){ a[i] = ( char ) (a[i] ^ 't' ); } String s = new String(a); return s; } /** * HMAC-SHA1 * @param encryptText * @param encryptKey * @return * @throws Exception */ public static String HmacSHA1Encrypt(String encryptText, String encryptKey) throws Exception { byte [] data=encryptKey.getBytes(ENCODING); SecretKey secretKey = new SecretKeySpec(data, MAC_NAME); Mac mac = Mac.getInstance(MAC_NAME); mac.init(secretKey); byte [] text = encryptText.getBytes(ENCODING); byte [] str = mac.doFinal(text); // Create Hex String StringBuffer hexString = new StringBuffer(); // 字节数组转换为 十六进制 数 for ( int i = 0 ; i < str.length; i++) { String shaHex = Integer.toHexString(str[i] & 0xFF ); if (shaHex.length() < 2 ) { hexString.append( 0 ); } hexString.append(shaHex); } return hexString.toString(); } public static String convertSHA1(String instr){ try { return HmacSHA1Encrypt(instr,key); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return "" ; } } // 测试主函数 public static void main(String args[]) throws Exception { //哈希散列带秘钥加密 String tt = convertSHA1( "123456" ); System.out.println(tt); //MD5加密 String s = new String( "123456" ); System.out.println( "原始:" + s); System.out.println( "MD5后:" + string2MD5(s)); System.out.println( "MD5加密后:" + stringMD5(s)); } } |
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
原文链接:http://www.cnblogs.com/lingbing/p/6085747.html