两种方式实现java Md5加密

发布于 2019-01-17  824 次阅读


一、加盐方式

/**

  • 生成32位md5码
  • @param password
  • @return
    */
    public static String md5Password(String password) {

try {
// 得到一个信息摘要器
MessageDigest digest = MessageDigest.getInstance("md5");
byte[] result = digest.digest(password.getBytes());
StringBuffer buffer = new StringBuffer();
// 把每一个byte 做一个与运算 0xff;
for (byte b : result) {
// 与运算
int number = b & 0xff;// 加盐
String str = Integer.toHexString(number);
if (str.length() == 1) {
buffer.append("0");
}
buffer.append(str);
}

// 标准的md5加密后的结果
return buffer.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return "";
}

}

二、普通方式

public static String MD5(String key) {
char hexDigits[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
try {
byte[] btInput = key.getBytes();
// 获得MD5摘要算法的 MessageDigest 对象
MessageDigest mdInst = MessageDigest.getInstance("MD5");
// 使用指定的字节更新摘要
mdInst.update(btInput);
// 获得密文
byte[] md = mdInst.digest();
// 把密文转换成十六进制的字符串形式
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
return null;
}
}
工具类如下:

import java.security.MessageDigest;

/**

  • @Author:Starry
  • @Description:
  • @Date:Created in 9:46 2018/4/13
  • Modified By:
    */
    public class MD5Utils {

private static final String hexDigIts[] = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};

/**

  • MD5加密
  • @param origin 字符
  • @param charsetname 编码
  • @return
    */
    public static String MD5Encode(String origin, String charsetname){
    String resultString = null;
    try{
    resultString = new String(origin);
    MessageDigest md = MessageDigest.getInstance("MD5");
    if(null == charsetname || "".equals(charsetname)){
    resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
    }else{
    resultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname)));
    }
    }catch (Exception e){
    }
    return resultString;
    }

public static String byteArrayToHexString(byte b[]){
StringBuffer resultSb = new StringBuffer();
for(int i = 0; i < b.length; i++){
resultSb.append(byteToHexString(b[i]));
}
return resultSb.toString();
}

public static String byteToHexString(byte b){
int n = b;
if(n < 0){
n += 256;
}
int d1 = n / 16;
int d2 = n % 16;
return hexDigIts[d1] + hexDigIts[d2];
}

}

结果如下:


公交车司机终于在众人的指责中将座位让给了老太太