通過哈希算法進行加密。由於哈希算法是單向的,能夠將不論什么大小的數據轉化為定長的“指紋”,並且無法被反向計算。
另外,即使數據源僅僅修改了一丁點。哈希的結果也會全然不同。
這種特性使得它很適合用於保存password。由於我們須要加密后的password無法被解密,同一時候也能保證正確校驗每一個用戶的password。可是哈希加密能夠通過字典攻擊和暴力攻擊破解。
password加鹽。鹽是一個加入到用戶的password哈希過程中的一段隨機序列。
這個機制可以防止通過預先計算結果的彩虹表破解。每一個用戶都有自己的鹽,這種結果就是即使用戶的password同樣。通過加鹽后哈希值也將不同。
為了校驗password是否正確,我們須要儲存鹽值。通常和password哈希值一起存放在賬戶數據庫中。或者直接存為哈希字符串的一部分。
public class PasswordEncryption { public static final String PBKDF2_ALGORITHM = "PBKDF2WithHmacSHA1"; /** * 鹽的長度 */ public static final int SALT_BYTE_SIZE = 32 / 2; /** * 生成密文的長度 */ public static final int HASH_BIT_SIZE = 128 * 4; /** * 迭代次數 */ public static final int PBKDF2_ITERATIONS = 1000; /** * 對輸入的password進行驗證 * * @param attemptedPassword * 待驗證的password * @param encryptedPassword * 密文 * @param salt * 鹽值 * @return 是否驗證成功 * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException */ public static boolean authenticate(String attemptedPassword, String encryptedPassword, String salt) throws NoSuchAlgorithmException, InvalidKeySpecException { // 用同樣的鹽值對用戶輸入的password進行加密 String encryptedAttemptedPassword = getEncryptedPassword(attemptedPassword, salt); // 把加密后的密文和原密文進行比較,同樣則驗證成功。否則失敗 return encryptedAttemptedPassword.equals(encryptedPassword); } /** * 生成密文 * * @param password * 明文password * @param salt * 鹽值 * @return * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException */ public static String getEncryptedPassword(String password, String salt) throws NoSuchAlgorithmException, InvalidKeySpecException { KeySpec spec = new PBEKeySpec(password.toCharArray(), fromHex(salt), PBKDF2_ITERATIONS, HASH_BIT_SIZE); SecretKeyFactory f = SecretKeyFactory.getInstance(PBKDF2_ALGORITHM); return toHex(f.generateSecret(spec).getEncoded()); } /** * 通過提供加密的強隨機數生成器 生成鹽 * * @return * @throws NoSuchAlgorithmException */ public static String generateSalt() throws NoSuchAlgorithmException { SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); byte[] salt = new byte[SALT_BYTE_SIZE]; random.nextBytes(salt); return toHex(salt); } /** * 十六進制字符串轉二進制字符串 * * @param hex the hex string * @return the hex string decoded into a byte array */ private static byte[] fromHex(String hex) { byte[] binary = new byte[hex.length() / 2]; for (int i = 0; i < binary.length; i++) { binary[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16); } return binary; } /** * 二進制字符串轉十六進制字符串 * * @param array the byte array to convert * @return a length*2 character string encoding the byte array */ private static String toHex(byte[] array) { BigInteger bi = new BigInteger(1, array); String hex = bi.toString(16); int paddingLength = (array.length * 2) - hex.length(); if (paddingLength > 0) return String.format("%0" + paddingLength + "d", 0) + hex; else return hex; } }
首先要生成一個鹽值salt,再把原始password和salt加密得到密文。驗證的時候,把用戶輸入的password和同樣的鹽值salt使用同樣的加密算法得到一個密文,將這個密文和原密文相比較,同樣則驗證通過,反之則不通過。
public static void main(String[] args) { String password = "test"; String salt; String ciphertext; try { salt = PasswordEncryption.generateSalt(); ciphertext = PasswordEncryption.getEncryptedPassword(password, salt); boolean result = PasswordEncryption.authenticate(password, ciphertext, salt); System.out.println(password + " " + password.length()); System.out.println(salt + " " + salt.length()); System.out.println(ciphertext + " " + ciphertext.length()); if (result) { System.out.println("succeed"); } else { System.out.println("failed"); } } catch (NoSuchAlgorithmException e) { System.out.println("NoSuchAlgorithmException"); } catch (InvalidKeySpecException e) { System.out.println("InvalidKeySpecException"); } }
測試結果為:
test 4 3aca9ca3fa80158b765ece7d0a45f2e8 32 592cb30e95efc720c5accf425ed5f2fe46aa332d9980e6daa234797de49cda731c2c18e667b4dd71ba33797a3dcddd312ff9b03d802bf1cc09aacb2a176cf741 128 succeed
參考資料:
1、https://en.wikipedia.org/wiki/PBKDF2
2、http://blog.jobbole.com/61872/#toc1
3、http://www.oschina.net/question/82993_59611?sort=time&p=1