PKCS5_PBKDF2_HMAC_SHA1在爪哇中



必须在Java中实现openssl 函数 - PKCS5_PBKDF2_HMAC_SHA1(keyData, 8, salt, 8, iterCount, KEY_SIZE, key((。

下面是我的 Java 代码

SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(customerId.toCharArray(), RefundUtil.getSalt(), iterationCount, bitLength);
SecretKey tmp = factory.generateSecret(spec);
byte[] pbkdf2Key = tmp.getEncoded();

上面的 Java 代码是否正确?

这通常是我实现PBKDF2_HMAC_SHA1的方式:

private static byte[] getSalt(int saltSize){
SecureRandom secureRandom = new SecureRandom();
byte[] salt = new byte[saltSize];
secureRandom.nextBytes(salt);
this.MC = salt;
return salt;
}
public static byte[] hashPassword(char[] password, int saltSize) throws NoSuchAlgorithmException, InvalidKeySpecException{
byte[] salt = getSalt(saltSize);
PBEKeySpec pbeKeySpec = new PBEKeySpec(password,salt,iterations,hashBites);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
return keyFactory.generateSecret(pbeKeySpec).getEncoded();
}  

您的实现将正常工作

相关内容

  • 没有找到相关文章

最新更新