java md5加密实现
程序员文章站
2022-03-01 18:42:50
...
package com.cjwdemo.tieba2017.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Util {
public static String getMD5(String str) {
String result = "";
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(str.getBytes());
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
result = buf.toString();
// System.out.println("MD5(" + str + ",32) = " + result);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
System.out.println("结果: " + MD5Util.getMD5("123"));
}
}
//结果: 202cb962ac59075b964b07152d234b70
校验网站 http://www.cmd5.com/