1 package com.zyg.security.md5;
2
3 import java.io.UnsupportedEncodingException;
4 import java.security.MessageDigest;
5 import java.security.NoSuchAlgorithmException;
6 import java.security.SecureRandom;
7 import java.util.Arrays;
8
9 public class MyMD5Util {
10
11 private static final String HEX_NUMS_STR="0123456789ABCDEF";
12 private static final Integer SALT_LENGTH = 12;
13
14 /**
15 * 将16进制字符串转换成字节数组
16 * @param hex
17 * @return
18 */
19 public static byte[] hexStringToByte(String hex) {
20 int len = (hex.length() / 2);
21 byte[] result = new byte[len];
22 char[] hexChars = hex.toCharArray();
23 for (int i = 0; i < len; i++) {
24 int pos = i * 2;
25 result[i] = (byte) (HEX_NUMS_STR.indexOf(hexChars[pos]) << 4
26 | HEX_NUMS_STR.indexOf(hexChars[pos + 1]));
27 }
28 return result;
29 }
30
31
32 /**
33 * 将指定byte数组转换成16进制字符串
34 * @param b
35 * @return
36 */
37 public static String byteToHexString(byte[] b) {
38 StringBuffer hexString = new StringBuffer();
39 for (int i = 0; i < b.length; i++) {
40 String hex = Integer.toHexString(b[i] & 0xFF);
41 if (hex.length() == 1) {
42 hex = '0' + hex;
43 }
44 hexString.append(hex.toUpperCase());
45 }
46 return hexString.toString();
47 }
48
49 /**
50 * 验证口令是否合法
51 * @param password
52 * @param passwordInDb
53 * @return
54 * @throws NoSuchAlgorithmException
55 * @throws UnsupportedEncodingException
56 */
57 public static boolean validPassword(String password, String passwordInDb)
58 throws NoSuchAlgorithmException, UnsupportedEncodingException {
59 //将16进制字符串格式口令转换成字节数组
60 byte[] pwdInDb = hexStringToByte(passwordInDb);
61 //声明盐变量
62 byte[] salt = new byte[SALT_LENGTH];
63 //将盐从数据库中保存的口令字节数组中提取出来
64 System.arraycopy(pwdInDb, 0, salt, 0, SALT_LENGTH);
65 //创建消息摘要对象
66 MessageDigest md = MessageDigest.getInstance("MD5");
67 //将盐数据传入消息摘要对象
68 md.update(salt);
69 //将口令的数据传给消息摘要对象
70 md.update(password.getBytes("UTF-8"));
71 //生成输入口令的消息摘要
72 byte[] digest = md.digest();
73 //声明一个保存数据库中口令消息摘要的变量
74 byte[] digestInDb = new byte[pwdInDb.length - SALT_LENGTH];
75 //取得数据库中口令的消息摘要
76 System.arraycopy(pwdInDb, SALT_LENGTH, digestInDb, 0, digestInDb.length);
77 //比较根据输入口令生成的消息摘要和数据库中消息摘要是否相同
78 if (Arrays.equals(digest, digestInDb)) {
79 //口令正确返回口令匹配消息
80 return true;
81 } else {
82 //口令不正确返回口令不匹配消息
83 return false;
84 }
85 }
86
87
88 /**
89 * 获得加密后的16进制形式口令
90 * @param password
91 * @return
92 * @throws NoSuchAlgorithmException
93 * @throws UnsupportedEncodingException
94 */
95 public static String getEncryptedPwd(String password)
96 throws NoSuchAlgorithmException, UnsupportedEncodingException {
97 //声明加密后的口令数组变量
98 byte[] pwd = null;
99 //随机数生成器
100 SecureRandom random = new SecureRandom();
101 //声明盐数组变量
102 byte[] salt = new byte[SALT_LENGTH];
103 //将随机数放入盐变量中
104 random.nextBytes(salt);
105
106 //声明消息摘要对象
107 MessageDigest md = null;
108 //创建消息摘要
109 md = MessageDigest.getInstance("MD5");
110 //将盐数据传入消息摘要对象
111 md.update(salt);
112 //将口令的数据传给消息摘要对象
113 md.update(password.getBytes("UTF-8"));
114 //获得消息摘要的字节数组
115 byte[] digest = md.digest();
116
117 //因为要在口令的字节数组中存放盐,所以加上盐的字节长度
118 pwd = new byte[digest.length + SALT_LENGTH];
119 //将盐的字节拷贝到生成的加密口令字节数组的前12个字节,以便在验证口令时取出盐
120 System.arraycopy(salt, 0, pwd, 0, SALT_LENGTH);
121 //将消息摘要拷贝到加密口令字节数组从第13个字节开始的字节
122 System.arraycopy(digest, 0, pwd, SALT_LENGTH, digest.length);
123 //将字节数组格式加密后的口令转化为16进制字符串格式的口令
124 return byteToHexString(pwd);
125 }
126 }