一个JAVA MD5加密
主要容易出错的地方是byte[] passByte 向字符串的转化,不能直接用passByte.toString();
public void testaaa() {
try {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
messageDigest.reset();
String ss="hello";
messageDigest.update(ss.trim().getBytes()); //pass为用户注册时输入的代码
byte[] passByte = messageDigest.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < passByte.length; offset++) {
i = passByte[offset];
if(i<0) i+= 256;
if(i<16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
ss = buf.toString(); //将加密后的密文转成字符串赋给pass,最后传到数据库的是pass
System.out.println(ss);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}