1 public class MD5Util { 2 public static String getMD5(String s) { 3 char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 4 'A', 'B', 'C', 'D', 'E', 'F'}; 5 byte[] btInput = s.getBytes(); 6 // 获得MD5摘要算法的 MessageDigest 对象 7 MessageDigest mdInst = null; 8 try { 9 mdInst = MessageDigest.getInstance("MD5");10 } catch (NoSuchAlgorithmException e) {11 // TODO Auto-generated catch block12 e.printStackTrace();13 }14 // 使用指定的字节更新摘要15 mdInst.update(btInput);16 // 获得密文17 byte[] md = mdInst.digest();18 // 把密文转换成十六进制的字符串形式19 int j = md.length;20 char str[] = new char[j * 2];21 int k = 0;22 for (int i = 0; i < j; i++) {23 byte byte0 = md[i];24 str[k++] = hexDigits[byte0 >>> 4 & 0xf];25 str[k++] = hexDigits[byte0 & 0xf];26 }27 return new String(str);28 }29 }