試圖讓PBKDF2的Java實現Java實現,我用這個作爲我的C#版本:https://github.com/shawnmclean/SimpleCrypto.netPBKDF2,從C#
我的代碼:
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
public class PBKDF2 {
public static void main(String[] args) {
try {
SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec ks = new PBEKeySpec("iamtwentycharacterss".toCharArray(),"50.eGIYr3ZpxpWw67utH17s/A==".getBytes(),50,64);
SecretKey s = f.generateSecret(ks);
Key k = new SecretKeySpec(s.getEncoded(),"HmacSHA1");
System.out.println(new String(k.getEncoded()));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
}
}
}
我試過在計算器不同的答案:
Java - PBKDF2 with HMACSHA256 as the PRF
Password Verification with PBKDF2 in Java
不幸的是,結果的不匹配,其結果應該是: mOs/Mw7ZRM99i/BTJ+xnmj5Pm6QlqP1vuPqrf/Qa3WwassxI1QJ447OqdoBzunbJjvrx7+bHAO1Dnj8ltS4TKA==
你的結果是base64編碼但在程序中我沒有看到任何Base 64編碼碼 – 2012-07-24 12:42:50
好,我已經添加使用鹼64編碼apache commons庫,但我仍然得到一個無效的結果。 – 2012-07-24 14:26:55