Skip to content

Instantly share code, notes, and snippets.

@d0nutptr
Created August 1, 2017 01:20
Show Gist options
  • Select an option

  • Save d0nutptr/7bd17d7811503eb48d3afa0c56ec54d7 to your computer and use it in GitHub Desktop.

Select an option

Save d0nutptr/7bd17d7811503eb48d3afa0c56ec54d7 to your computer and use it in GitHub Desktop.
public class KeyDerivationWithBC {
public SecretKey generateSecretKey(String password, byte[] salt) {
byte[] passwordBytes = PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(password.toCharArray());
PKCS5S2ParametersGenerator keyGenerator = new PKCS5S2ParametersGenerator(new SHA256Digest());
keyGenerator.init(passwordBytes, salt, 4096);
byte[] keySecret = ((KeyParameter) keyGenerator.generateDerivedParameters(32)).getKey();
return new SecretKeySpec(keySecret, 0, keySecret.length, "AES");
}
public byte[] generateSalt() {
byte[] salt = new byte[32];
(new SecureRandom()).getNextBytes(salt);
return salt;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment