日期:2014-05-16 浏览次数:20610 次
//数据库MD5加密 package MD5CopePassWord; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; public class MD5Encrypt { //随机值(12位,由系统产生) 加盐 //原码(123) //密码 //随机值 + 密码 (保存到数据库) public static byte[] doEncrypt(String password) throws NoSuchAlgorithmException, UnsupportedEncodingException { SecureRandom random = new SecureRandom(); byte[] salt = new byte[12]; byte[] encryptPassword = null; random.nextBytes(salt); for(byte b : salt){ System.out.print(b + "\t"); } System.out.println(); MessageDigest messageDigest = MessageDigest.getInstance("MD5"); messageDigest.update(salt); //追加 messageDigest.update(password.getBytes()); byte[] digest = messageDigest.digest(); encryptPassword = new byte[digest.length + 12]; System.arraycopy(salt, 0, encryptPassword, 0, 12); System.arraycopy(digest, 0, encryptPassword, 12, digest.length); System.out.println(encryptPassword.length); for(byte b : encryptPassword){ System.out.print(b + "\t"); } return encryptPassword; } }