1 import java.security.MessageDigest;
2 public class MD5_Test {
3 public final static String MD5(String s) {
4 char hexDigits[] = { '0', '1', '2', '3', '4',
5 '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 }
26 catch (Exception e) {
27 e.printStackTrace();
28 return null;
29 }
30 }
31 public static void main(String[] args) {
32 System.out.print(MD5_Test.MD5("password"));
33 }
34 }
35