Have to implement the openssl function - PKCS5_PBKDF2_HMAC_SHA1(keyData, 8, salt, 8, iterCount, KEY_SIZE, key)) in Java.
Below is my code in 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();
Is the above Java code correct?
This is usually how I implement 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();
}
Your implementation is going to work just fine
You can use the following function :
private static byte[] calculatePbeMac(
DERObjectIdentifier oid,
byte[] salt,
int itCount,
char[] password,
byte[] data,
String provider)
throws Exception
{
SecretKeyFactory keyFact = SecretKeyFactory.getInstance(oid.getId(), provider);
PBEParameterSpec defParams = new PBEParameterSpec(salt, itCount);
PBEKeySpec pbeSpec = new PBEKeySpec(password);
SecretKey key = keyFact.generateSecret(pbeSpec);
Mac mac = Mac.getInstance(oid.getId(), provider);
mac.init(key, defParams);
mac.update(data);
return mac.doFinal();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.