这是一个MD5工具类
package org.fkjava.util;
import java.math.BigInteger;
import java.security.MessageDigest;
public class MD5 {
public static String getMD5(String str){
try{
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(str.getBytes());
return new BigInteger(1,md.digest()).toString(16).substring(0,16);
}catch(Exception e){
e.printStackTrace();
return null;
}
}
public static void main(String[] args){
String md5 = getMD5("123456");
System.out.println(md5);
byte[] password = md5.getBytes();
for(int i = 0;i<password.length;i++)
System.out.println(password[i]);
}
}
结果如下
e10adc3949ba59ab
101
49
48
97
100
99
51
57
52
57
98
97
53
57
97
98
现在取出的字符串能和数据库里的一致了,但为什么转换成的byte[]数组却不一样呢,数据库里同样是123456的MD5加密得到的hash值转换成的byte[]却是
-31
10
-36
57
73
-70
89
-85
-66
86
-32
87
-14
15
-120
62
未加密前的字符串都是“123456”,加密后取得的字符串都是“e10adc3949ba59ab”但为什么byte[]的数据不一样呢?
return new BigInteger(1,md.digest()).toString(16);
修改为
return new BigInteger(1,md.digest());
public static String getMD5(String str)
修改为
public static BigInteger getMD5(String str)
返回的就是数字。
把你数据库的值拿出来换成16位比较不是也行吗