1 package com.mall.common;
2
3
4 import java.security.MessageDigest;
5 /**
6 * MD5算法
7 */
8 public class MD5 {
9
10 public final static String getMD5(String s) {
11 char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
12 'a', 'b', 'c', 'd', 'e', 'f' };
13 try {
14 byte[] strTemp = s.getBytes();
15 MessageDigest mdTemp = MessageDigest.getInstance("MD5");
16
17 mdTemp.update(strTemp);
18
19 byte[] md = mdTemp.digest();
20 int j = md.length;
21 char str[] = new char[j * 2];
22 int k = 0;
23 for (int i = 0; i < j; i++) {
24 byte byte0 = md[i];
25 str[k++] = hexDigits[byte0 >>> 4 & 0xf];
26 str[k++] = hexDigits[byte0 & 0xf];
27 }
28 return new String(str);
29 } catch (Exception e) {
30 return null;
31 }
32 }
33
34 public static void main(String[] args) {
35 String pwd="123456";
36 System.out.println("明文:"+pwd+"密文:"+MD5.getMD5(pwd));
37 }
38
39 }