1 public class MD5Util {
2 public static String md5_32(String str) {
3 byte[] hash;
4 try {
5 //字符串的报文摘要
6 hash = MessageDigest.getInstance("MD5").digest(str.getBytes("UTF-8"));
7 } catch (NoSuchAlgorithmException e) {
8 e.printStackTrace();
9 } catch (UnsupportedEncodingException e) {
10 e.printStackTrace();
11 }
12
13 StringBuffer hex = new StringBuffer();
14 for (byte b : hash) {
15 if ((b & 0xFF) < 0x10) hex.append("0");
16 hex.append(Integer.toHexString(b & 0xFF));
17 }
18 return hex.toString();
19 }
20
21 //采用16位加密
22 public static String md5_16(String str){
23 return md5(str).substring(8,24);
24 }
25 }