1 import java.security.MessageDigest;
2 import java.security.*;
3 import java.security.spec.*;
4
5 /*
6 * To change this template, choose Tools | Templates
7 * and open the template in the editor.
8 */
9 /**
10 *
11 * @author Administrator
12 */
13 public class MD5 {
14 public final static String MD5(String s) {
15 char hexDigits[] = {
16 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
17 'e', 'f'
18 };
19 try {
20 byte[] strTemp = s.getBytes();
21 MessageDigest mdTemp = MessageDigest.getInstance("MD5");
22 mdTemp.update(strTemp);
23 byte[] md = mdTemp.digest();
24 int j = md.length;
25 char str[] = new char[j * 2];
26 int k = 0;
27 for (int i = 0; i < j; i++) {
28 byte byte0 = md[i];
29 str[k++] = hexDigits[byte0 >>> 4 & 0xf];
30 str[k++] = hexDigits[byte0 & 0xf];
31 }
32 return new String(str);
33 } catch (Exception e) {
34 return null;
35 }
36 }
37 }
38