阅读Java中的encryption密码
我在javascript / nodejsencryption密码下面的代码。 我需要在Java中读取这些密码:
encryptPassword: function(password, salt) { var salt = new Buffer(salt, 'base64'); return crypto.pbkdf2Sync(password, salt, 10000, 64).toString('base64'); }
我使用这种方法来创buildencryption的密码进行testing。 这是结果(如javavariables):
static String password = "123456"; static String salt = "CPvFo+klD9Vh2iE07JEGXA=="; public final String encrypted = "LtStqkNQjrr+P4V8fGtnauNJNOIB7t35O5I4a4/I9lFUnMR3ckbZyT85g/wO0Da9318Wrql/y1bsY2XdpXqx+Q==";
我试图将上面的encryption代码转换为java,使用这个http://www.javacodegeeks.com/2012/05/secure-password-storage-donts-dos-and.html :
public static byte[] getEncryptedPassword(String password, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException { String algorithm = "PBKDF2WithHmacSHA1"; int derivedKeyLength = 64; int iterations = 10000; KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, iterations, derivedKeyLength); SecretKeyFactory f = SecretKeyFactory.getInstance(algorithm); return f.generateSecret(spec).getEncoded(); }
最后,我使用apache编解码器来设置64位string:
public static String getEncryptedPassword(String password, String salt) throws NoSuchAlgorithmException, InvalidKeySpecException { byte[] bytes = Base64.decodeBase64(salt); byte[] encryptedPassword = getEncryptedPassword(password, bytes); return Base64.encodeBase64String(encryptedPassword); }
testing人员是这样的:
public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException { System.out.println(encrypted); System.out.println(getEncryptedPassword(password, salt)); }
而输出是
LtStqkNQjrr + P4V8fGtnauNJNOIB7t35O5I4a4 / I9lFUnMR3ckbZyT85g / wO0Da9318Wrql / y1bsY2XdpXqx + Q ==
LtStqkNQjro =
正如你所看到的,java代码产生的东西几乎是javascript产生的encryption密码的前缀。 我testing了这个不同的复杂的密码,并得到了相同的结果:一个string是JavaScriptencryption密码的前缀。 不同的密码不是彼此的前缀。
所以我觉得我差不多 但我无法弄清楚缺less的是什么。