Java MD5加密
对字符串进行加密,密文为32位字符串
1 package cn.com.cxsw.utils; 2 3 import java.security.MessageDigest; 4 5 6 public class MD5 { 7 8 /** 9 * MD5加密 10 * 11 * @param message 12 * 要进行MD5加密的字符串 13 * @return 加密结果为32位字符串 14 */ 15 public static String getMD5(String message) { 16 MessageDigest messageDigest = null; 17 StringBuffer md5StrBuff = new StringBuffer(); 18 try { 19 messageDigest = MessageDigest.getInstance("MD5"); 20 messageDigest.reset(); 21 messageDigest.update(message.getBytes("UTF-8")); 22 23 byte[] byteArray = messageDigest.digest(); 24 for (int i = 0; i < byteArray.length; i++) { 25 if (Integer.toHexString(0xFF & byteArray[i]).length() == 1) 26 md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i])); 27 else 28 md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i])); 29 } 30 } catch (Exception e) { 31 throw new RuntimeException(); 32 } 33 return md5StrBuff.toString().toUpperCase();// 字母大写 34 } 35 36 37 public static void main(String[] args) { 38 String psw = "admin"; 39 System.out.println(getMD5(psw)); 40 } 41 42 }