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; }}