MD5加密Java工具類
這是一個非常好用的使用MD5+salt加密的工具類。使用這個工具類,非常簡單,從前臺拿到密碼passwd,直接HexUtil.getEncryptedPwd(passwd)就可以返回一個長度為56的字符串,可以用來保存到數據庫中,相反,登錄的時候,因為MD5加密是不可逆的運算,只能拿用戶輸入的密碼走一遍MD5+salt加密之后,跟數據庫中的passwd比較,看是否一致,一致時密碼相同,登錄成功,通過調用HexUtil.validPasswd(String passwd,String dbPasswd)方法,就可以了,不用再做其他事。
/**
- MD5加密解密及字符串對比工具類
- @author jacob_ye
http://www.blog.csdn.net/zranye */ public class HexUtil { private final static String HEX_NUMS_STR = "0123456789ABCDEF"; private final static Integer SALT_LENGTH = 12;
/**
- 將16進制字符串轉換成數組
- @return byte[]
- @author jacob
/ public static byte[] hexStringToByte(String hex) { / len為什么是hex.length() / 2 ?
- 首先,hex是一個字符串,里面的內容是像16進制那樣的char數組
- 用2個16進制數字可以表示1個byte,所以要求得這些char[]可以轉化成什么樣的byte[],首先可以確定的就是長度為這個char[]的一半
/
int len = (hex.length() / 2);
byte[] result = new byte[len];
char[] hexChars = hex.toCharArray();
for (int i = 0; i < len; i++) {
int pos = i 2;
result[i] = (byte) (HEX_NUMS_STR.indexOf(hexChars[pos]) << 4 | HEX_NUMS_STR
} return result; }.indexOf(hexChars[pos + 1]));
/**
- 將數組轉換成16進制字符串
- @return String
- @author jacob *
*/ public static String byteToHexString(byte[] salt){ StringBuffer hexString = new StringBuffer(); for (int i = 0; i < salt.length; i++) {
String hex = Integer.toHexString(salt[i] & 0xFF); if(hex.length() == 1){ hex = '0' + hex; } hexString.append(hex.toUpperCase());
} return hexString.toString(); }
/**
- 密碼驗證
- @param passwd 用戶輸入密碼
- @param dbPasswd 數據庫保存的密碼
- @return
- @throws NoSuchAlgorithmException
@throws UnsupportedEncodingException */ public static boolean validPasswd(String passwd, String dbPasswd)
throws NoSuchAlgorithmException, UnsupportedEncodingException{
byte[] pwIndb = hexStringToByte(dbPasswd); //定義salt byte[] salt = new byte[SALT_LENGTH]; System.arraycopy(pwIndb, 0, salt, 0, SALT_LENGTH); //創建消息摘要對象 MessageDigest md = MessageDigest.getInstance("MD5"); //將鹽數據傳入消息摘要對象 md.update(salt); md.update(passwd.getBytes("UTF-8")); byte[] digest = md.digest(); //聲明一個對象接收數據庫中的口令消息摘要 byte[] digestIndb = new byte[pwIndb.length - SALT_LENGTH]; //獲得數據庫中口令的摘要 System.arraycopy(pwIndb, SALT_LENGTH, digestIndb, 0,digestIndb.length); //比較根據輸入口令生成的消息摘要和數據庫中的口令摘要是否相同 if(Arrays.equals(digest, digestIndb)){
//口令匹配相同 return true;
}else{
return false;
} }
/**
- 獲得md5之后的16進制字符
- @param passwd 用戶輸入密碼字符
- @return String md5加密后密碼字符
- @throws NoSuchAlgorithmException
@throws UnsupportedEncodingException */ public static String getEncryptedPwd(String passwd)
throws NoSuchAlgorithmException, UnsupportedEncodingException{
//拿到一個隨機數組,作為鹽 byte[] pwd = null; SecureRandom sc= new SecureRandom(); byte[] salt = new byte[SALT_LENGTH]; sc.nextBytes(salt);
//聲明摘要對象,并生成 MessageDigest md = MessageDigest.getInstance("MD5"); md.update(salt); md.update(passwd.getBytes("UTF-8")); byte[] digest = md.digest();
pwd = new byte[salt.length + digest.length]; System.arraycopy(salt, 0, pwd, 0, SALT_LENGTH); System.arraycopy(digest, 0, pwd, SALT_LENGTH, digest.length); return byteToHexString(pwd); }</pre>來自:http://blog.csdn.net/zranye/article/details/8234480