java实现MD5加密方法汇总

时间:2022-08-31 20:48:51

代码一:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
 * 实现MD5加密
 *
 */
public class MD5 {
   
  /**
   * 获取加密后的字符串
   * @param input
   * @return
   */
  public static String stringMD5(String pw) {
    try {
        
       // 拿到一个MD5转换器(如果想要SHA1参数换成”SHA1”)
       MessageDigest messageDigest =MessageDigest.getInstance("MD5");
       // 输入的字符串转换成字节数组
       byte[] inputByteArray = pw.getBytes();
       // inputByteArray是输入字符串转换得到的字节数组
       messageDigest.update(inputByteArray);
       // 转换并返回结果,也是字节数组,包含16个元素
       byte[] resultByteArray = messageDigest.digest();
       // 字符数组转换成字符串返回
       return byteArrayToHex(resultByteArray);
      } catch (NoSuchAlgorithmException e) {
       return null;
      }
  }
   
  public static String byteArrayToHex(byte[] byteArray) {
     
    // 首先初始化一个字符数组,用来存放每个16进制字符
    char[] hexDigits = {'0','1','2','3','4','5','6','7','8','9', 'A','B','C','D','E','F' };
    // new一个字符数组,这个就是用来组成结果字符串的(解释一下:一个byte是八位二进制,也就是2位十六进制字符(2的8次方等于16的2次方))
    char[] resultCharArray =new char[byteArray.length * 2];
    // 遍历字节数组,通过位运算(位运算效率高),转换成字符放到字符数组中去
    int index = 0;
    for (byte b : byteArray) {
      resultCharArray[index++] = hexDigits[b>>> 4 & 0xf];
      resultCharArray[index++] = hexDigits[b& 0xf];
    }
    // 字符数组组合成字符串返回
    return new String(resultCharArray);
  }
}

方法二:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package other;
 
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/*
 * MD5 算法
*/
public class MD5 {
  
  // 全局数组
  private final static String[] strDigits = { "0", "1", "2", "3", "4", "5",
      "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
 
  public MD5() {
  }
 
  // 返回形式为数字跟字符串
  private static String byteToArrayString(byte bByte) {
    int iRet = bByte;
    // System.out.println("iRet="+iRet);
    if (iRet < 0) {
      iRet += 256;
    }
    int iD1 = iRet / 16;
    int iD2 = iRet % 16;
    return strDigits[iD1] + strDigits[iD2];
  }
 
  // 返回形式只为数字
  private static String byteToNum(byte bByte) {
    int iRet = bByte;
    System.out.println("iRet1=" + iRet);
    if (iRet < 0) {
      iRet += 256;
    }
    return String.valueOf(iRet);
  }
 
  // 转换字节数组为16进制字串
  private static String byteToString(byte[] bByte) {
    StringBuffer sBuffer = new StringBuffer();
    for (int i = 0; i < bByte.length; i++) {
      sBuffer.append(byteToArrayString(bByte[i]));
    }
    return sBuffer.toString();
  }
 
  public static String GetMD5Code(String strObj) {
    String resultString = null;
    try {
      resultString = new String(strObj);
      MessageDigest md = MessageDigest.getInstance("MD5");
      // md.digest() 该函数返回值为存放哈希值结果的byte数组
      resultString = byteToString(md.digest(strObj.getBytes()));
    } catch (NoSuchAlgorithmException ex) {
      ex.printStackTrace();
    }
    return resultString;
  }
 
  public static void main(String[] args) {
    MD5 getMD5 = new MD5();
    System.out.println(getMD5.GetMD5Code("000000"));
  }
}