Example usage for org.bouncycastle.asn1.pkcs PBES2Parameters getKeyDerivationFunc

List of usage examples for org.bouncycastle.asn1.pkcs PBES2Parameters getKeyDerivationFunc

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.pkcs PBES2Parameters getKeyDerivationFunc.

Prototype

public KeyDerivationFunc getKeyDerivationFunc() 

Source Link

Usage

From source file:org.cryptacular.pbe.PBES2EncryptionScheme.java

License:Open Source License

/**
 * Creates a new instance with the given parameters.
 *
 * @param  params  PBES2 parameters describing the key derivation function and
 *                 encryption scheme./*  ww w. ja v a2  s .c o  m*/
 * @param  password  Password used to derive key.
 */
public PBES2EncryptionScheme(final PBES2Parameters params, final char[] password) {
    final PBKDF2Params kdfParams = PBKDF2Params.getInstance(params.getKeyDerivationFunc().getParameters());
    final byte[] salt = kdfParams.getSalt();
    final int iterations = kdfParams.getIterationCount().intValue();
    if (kdfParams.getKeyLength() != null) {
        keyLength = kdfParams.getKeyLength().intValue() * 8;
    }

    final PKCS5S2ParametersGenerator generator = new PKCS5S2ParametersGenerator();
    generator.init(PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(password), salt, iterations);
    initCipher(generator, params.getEncryptionScheme());
}

From source file:org.jruby.ext.openssl.x509store.PEMInputOutput.java

License:LGPL

private static CipherParameters extractPBES2CipherParams(char[] password, PBES2Parameters pbeParams) {
    PBKDF2Params pbkdfParams = PBKDF2Params.getInstance(pbeParams.getKeyDerivationFunc().getParameters());
    int keySize = 192;
    if (pbkdfParams.getKeyLength() != null) {
        keySize = pbkdfParams.getKeyLength().intValue() * 8;
    }/*www.j av  a 2  s .c om*/
    int iterationCount = pbkdfParams.getIterationCount().intValue();
    byte[] salt = pbkdfParams.getSalt();
    PBEParametersGenerator generator = new PKCS5S2ParametersGenerator();
    generator.init(PBEParametersGenerator.PKCS5PasswordToBytes(password), salt, iterationCount);
    return generator.generateDerivedParameters(keySize);
}