PBKDF2(Password-Based Key Derivation Function)。

通过哈希算法进行加密。由于哈希算法是单向的,能够将不论什么大小的数据转化为定长的“指纹”,并且无法被反向计算。

另外,即使数据源仅仅修改了一丁点。哈希的结果也会全然不同。

这种特性使得它很适合用于保存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

最新文章

  1. OpenGL的简单研究-开端
  2. WebApi:自定义筛选器
  3. CG
  4. JDE开发端安装笔记
  5. PHP强制清除缓存
  6. [linux basic]--线程
  7. 简单且线程安全的两个单例模式java程序
  8. Lambda表达式 =&gt;(msdn)
  9. 模仿ios下的coverflow
  10. Apache Tomcat8必备知识
  11. ref参数的用途
  12. UVa1368/ZOJ3132 DNA Consensus String
  13. PHP. 03 .ajax传输XML、 ajax传输json、封装
  14. return 的返回值与结束功能
  15. jQuery遮罩层插件
  16. (Java)微信之个人公众账号开发(一)——进入开发者模式
  17. UNIX网络编程——信号驱动式I/O
  18. Prim算法、Kruskal算法和最小生成树 | Minimum Spanning Tree
  19. 附录B——MySQL样例表
  20. ZooKeeper是按照CP原则构建的,不适合做Service服务发现

热门文章

  1. shp系列(六)——利用C++进行Dbf文件的写(创建)
  2. mysqls,为node.js而编写的sql语句生成插件 (crud for mysql).
  3. Spring《八-一》CGLIB代理和自动代理
  4. Monad 系列
  5. JS中数组的一些笔记
  6. 4) 十分钟学会android--建立第一个APP,启动另一个Activity
  7. 6 Python+Selenium的元素定位方法(CSS)
  8. 02--读书笔记之:C++ Primer (第4版)及习题
  9. python tips:类与实例的属性问题
  10. 如何设置root登录(滴滴云)