Example usage for org.bouncycastle.asn1.pkcs PBKDF2Params getKeyLength

List of usage examples for org.bouncycastle.asn1.pkcs PBKDF2Params getKeyLength

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.pkcs PBKDF2Params getKeyLength.

Prototype

public BigInteger getKeyLength() 

Source Link

Document

Return the intended length in octets of the derived key.

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.//from ww  w  .j  a v  a 2 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;
    }/*from   ww  w. j av  a2 s. c o m*/
    int iterationCount = pbkdfParams.getIterationCount().intValue();
    byte[] salt = pbkdfParams.getSalt();
    PBEParametersGenerator generator = new PKCS5S2ParametersGenerator();
    generator.init(PBEParametersGenerator.PKCS5PasswordToBytes(password), salt, iterationCount);
    return generator.generateDerivedParameters(keySize);
}