1 public static String encode(String str) { 2 String encodeString = ""; 3 4 try { 5 MessageDigest md = MessageDigest.getInstance("md5"); 6 // 用MD5加密后的字节数组 7 byte[] digest = md.digest(str.getBytes()); 8 for (byte b : digest) { 9 // 将前面的ffffff去掉 10 int temp = b & 0x000000ff; 11 // 得到16进制 12 String s = Integer.toHexString(temp); 13 // 使每一位上都有两个数 14 if (s.length() == 1) { 15 s = "0" + s; 16 } 17 18 encodeString += s; 19 } 20 } catch (NoSuchAlgorithmException e) { 21 // TODO Auto-generated catch block 22 e.printStackTrace(); 23 } 24 return encodeString; 25 }