package com.cnse.module.security.md5Security; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; /** * @author Administrator md5加密工具類 雖然MD5和SHA已被王小云破解 * md5加密能夠把用戶名和密碼一塊兒加密層串 保證惟一性 提升安全 */ public class Md5Util { public static void main(String[] args) throws Exception { md5Encode("zhangsan", "123456"); System.out.println(md5Encode("張三", "123456")); } public static String md5Encode(String userName, String userPwd) throws Exception { String code = userName + userPwd; MessageDigest messageDigest = MessageDigest.getInstance("MD5"); messageDigest.update(code.getBytes("UTF-8")); byte[] bytes = messageDigest.digest(); String md5Str = ""; for (int i = 0; i < bytes.length; i++) { int temp = 0; // 當bytes<0 if (bytes[i] < 0) { temp = 256+bytes[i]; } else { temp = bytes[i]; } // 當位數不夠16位時 if (temp < 16) { md5Str = md5Str+"0"; } md5Str = md5Str + Integer.toString(temp,16); } return md5Str; } }