MD5加密方法
项目中的一个方法,32位小写加密。
1 public static String encryptByMD5(String str) { 2 MessageDigest messageDigest = null; 3 4 try { 5 messageDigest = MessageDigest.getInstance("MD5"); 6 messageDigest.reset(); 7 messageDigest.update(str.getBytes("UTF-8")); 8 } catch (NoSuchAlgorithmException e) { 9 e.printStackTrace(); 10 } catch (UnsupportedEncodingException e) { 11 e.printStackTrace(); 12 } 13 14 byte[] byteArray = messageDigest.digest(); 15 StringBuffer md5StrBuff = new StringBuffer(); 16 17 for (int i = 0; i < byteArray.length; i++) { 18 if (Integer.toHexString(0xFF & byteArray[i]).length() == 1) { 19 md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i])); 20 } else { 21 md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i])); 22 } 23 } 24 25 return md5StrBuff.toString(); 26 }