public class Encrypt_MD5 {
public String fillMD5(String md5){//补全32位
return md5.length()==32?md5:fillMD5("0"+md5);
}
public String[] md5(String param){
String sign_md5_32 = "";
try {
// 生成一个MD5加密计算摘要
MessageDigest md = MessageDigest.getInstance("MD5");
// 计算md5函数
md.update(param.getBytes());
// digest()最后确定返回md5 hash值,返回值为8为字符串。因为md5 hash值是16位的hex值,实际上就是8位的字符
// BigInteger函数则将8位的字符串转换成16位hex值,用字符串来表示;得到字符串形式的hash值
String sign_md5_16 =new BigInteger(1, md.digest()).toString(16);
//BigInteger会把0省略掉,需补全至32位
sign_md5_32 = sign_md5_16.length()==32?sign_md5_16:fillMD5("0"+sign_md5_16);
//System.out.println(sign_md5_32);
} catch (Exception e) {
throw new RuntimeException("MD5加密错误:"+e.getMessage(),e);
}
return sign_md5_32;
}
}