密码加密(MD5)

package com.sec.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
* 密码加密md5
*
* @author Administrator
*
*/
public class EncryptionUtil {
public static String getEncryptionPassword(String userPwd)
throws NoSuchAlgorithmException {

// 1、转变为二进制(byte[])
byte[] source = userPwd.getBytes();
MessageDigest md5 = MessageDigest.getInstance("md5");
// 2、将二进制更新
md5.update(source);
// 3、获取新摘要(byte[])
source = md5.digest(source);
// 4、重新转换为字符串
int value;
StringBuffer newPwd = new StringBuffer();
for (byte b : source) {
value = b & 0xff;// 保留低8位,其他位变零
// 将8位的字节转换为16进制数 0-9 a-f(10-15)
if (value <= 9) {
newPwd.append("0");// 8 --> 08
}
newPwd.append(Integer.toHexString(value));
}
return newPwd.toString();
}
}

原文地址:https://www.cnblogs.com/bsyx/p/4129434.html