Java自带MD5算法

 1 import java.security.MessageDigest;
 2 
 3 public class MD5_Test {
 4     public final static String MD5(String s) {
 5         char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 6                 'A', 'B', 'C', 'D', 'E', 'F' };
 7         try {
 8             byte[] btInput = s.getBytes();
 9             // 获得MD5摘要算法的 MessageDigest 对象
10             MessageDigest mdInst = MessageDigest.getInstance("MD5");
11             // 使用指定的字节更新摘要
12             mdInst.update(btInput);
13             // 获得密文
14             byte[] md = mdInst.digest();
15             // 把密文转换成十六进制的字符串形式
16             int j = md.length;
17             char str[] = new char[j * 2];
18             int k = 0;
19             for (int i = 0; i < j; i++) {
20                 byte byte0 = md[i];
21                 str[k++] = hexDigits[byte0 >>> 4 & 0xf];
22                 str[k++] = hexDigits[byte0 & 0xf];
23             }
24             return new String(str);
25         } catch (Exception e) {
26             e.printStackTrace();
27             return null;
28         }
29     }
30 
31     public static void main(String[] args) {
32         System.out.print(MD5_Test.MD5("password"));
33     }
34 }
原文地址:https://www.cnblogs.com/live365wang/p/1993699.html