MD5加密
package test;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Utils {
public static String getMd5(String plainText) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(plainText.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));
}
return buf.toString().substring(8, 24);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
}
MD5测试
package test;
public class MD5Test {
public static void main(String[] args) {
String userName = "zhangsan";
String passWord = "123456";
String MDPassWord = MD5Utils.getMd5(passWord);
System.out.println("加密前密码:"+passWord);
System.out.println("加密后密码:"+MDPassWord);
}
}
返回的内容
加密前密码:123456
加密后密码:49ba59abbe56e057