这是一个非常好用的使用MD5+salt加密的工具类。使用这个工具类,非常简单,从前台拿到密码passwd,直接HexUtil.getEncryptedPwd(passwd)就可以返回一个长度为56的字符串,可以用来保存到数据库中,相反,登录的时候,因为MD5加密是不可逆的运算,只能拿用户输入的密码走一遍MD5+salt加密之后,跟数据库中的passwd比较,看是否一致,一致时密码相同,登录成功,通过调用HexUtil.validPasswd(String passwd,String dbPasswd)方法,就可以了,不用再做其他事。
001 | /** |
002 | * MD5加密解密及字符串对比工具类 |
003 | * @author jacob_ye |
004 | * http://www.blog.csdn.net/zranye |
005 | */ |
006 | public class HexUtil { |
007 | private final static String HEX_NUMS_STR = "0123456789ABCDEF" ; |
008 | private final static Integer SALT_LENGTH = 12 ; |
009 |
010 | /** |
011 | * 将16进制字符串转换成数组 |
012 | * |
013 | * @return byte[] |
014 | * @author jacob |
015 | * */ |
016 | public static byte [] hexStringToByte(String hex) { |
017 | /* len为什么是hex.length() / 2 ? |
018 | * 首先,hex是一个字符串,里面的内容是像16进制那样的char数组 |
019 | * 用2个16进制数字可以表示1个byte,所以要求得这些char[]可以转化成什么样的byte[],首先可以确定的就是长度为这个char[]的一半 |
020 | */ |
021 | int len = (hex.length() / 2); |
022 | byte[] result = new byte[len]; |
023 | char[] hexChars = hex.toCharArray(); |
024 | for (int i = 0; i < len; i++) { |
025 | int pos = i * 2; |
026 | result[i] = (byte) (HEX_NUMS_STR.indexOf(hexChars[pos]) << 4 | HEX_NUMS_STR |
027 | .indexOf(hexChars[pos + 1])); |
028 | } |
029 | return result; |
030 | } |
031 | |
032 | /** |
033 | * 将数组转换成16进制字符串 |
034 | * |
035 | * @return String |
036 | * @author jacob |
037 | * |
038 | * */ |
039 | public static String byteToHexString(byte[] salt){ |
040 | StringBuffer hexString = new StringBuffer(); |
041 | for (int i = 0; i < salt.length; i++) { |
042 | String hex = Integer.toHexString(salt[i] & 0xFF); |
043 | if(hex.length() == 1){ |
044 | hex = '0' + hex; |
045 | } |
046 | hexString.append(hex.toUpperCase()); |
047 | } |
048 | return hexString.toString(); |
049 | } |
050 | |
051 | /** |
052 | * 密码验证 |
053 | * @param passwd 用户输入密码 |
054 | * @param dbPasswd 数据库保存的密码 |
055 | * @return |
056 | * @throws NoSuchAlgorithmException |
057 | * @throws UnsupportedEncodingException |
058 | */ |
059 | public static boolean validPasswd(String passwd, String dbPasswd) |
060 | throws NoSuchAlgorithmException, UnsupportedEncodingException{ |
061 | byte[] pwIndb = hexStringToByte(dbPasswd); |
062 | //定义salt |
063 | byte[] salt = new byte[SALT_LENGTH]; |
064 | System.arraycopy(pwIndb, 0, salt, 0, SALT_LENGTH); |
065 | //创建消息摘要对象 |
066 | MessageDigest md = MessageDigest.getInstance("MD5"); |
067 | //将盐数据传入消息摘要对象 |
068 | md.update(salt); |
069 | md.update(passwd.getBytes("UTF-8")); |
070 | byte[] digest = md.digest(); |
071 | //声明一个对象接收数据库中的口令消息摘要 |
072 | byte[] digestIndb = new byte[pwIndb.length - SALT_LENGTH]; |
073 | //获得数据库中口令的摘要 |
074 | System.arraycopy(pwIndb, SALT_LENGTH, digestIndb, 0,digestIndb.length); |
075 | //比较根据输入口令生成的消息摘要和数据库中的口令摘要是否相同 |
076 | if(Arrays.equals(digest, digestIndb)){ |
077 | //口令匹配相同 |
078 | return true; |
079 | }else{ |
080 | return false; |
081 | } |
082 | } |
083 | |
084 | /** |
085 | * 获得md5之后的16进制字符 |
086 | * @param passwd 用户输入密码字符 |
087 | * @return String md5加密后密码字符 |
088 | * @throws NoSuchAlgorithmException |
089 | * @throws UnsupportedEncodingException |
090 | */ |
091 | public static String getEncryptedPwd(String passwd) |
092 | throws NoSuchAlgorithmException, UnsupportedEncodingException{ |
093 | //拿到一个随机数组,作为盐 |
094 | byte [] pwd = null ; |
095 | SecureRandom sc= new SecureRandom(); |
096 | byte [] salt = new byte [SALT_LENGTH]; |
097 | sc.nextBytes(salt); |
098 | |
099 | //声明摘要对象,并生成 |
100 | MessageDigest md = MessageDigest.getInstance( "MD5" ); |
101 | md.update(salt); |
102 | md.update(passwd.getBytes( "UTF-8" )); |
103 | byte [] digest = md.digest(); |
104 | |
105 | pwd = new byte [salt.length + digest.length]; |
106 | System.arraycopy(salt, 0 , pwd, 0 , SALT_LENGTH); |
107 | System.arraycopy(digest, 0 , pwd, SALT_LENGTH, digest.length); |
108 | return byteToHexString(pwd); |
109 | } |