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

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

Introduction

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

Prototype

public EncryptionScheme getEncryptionScheme() 

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.//from  w  w  w .  j a  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 PrivateKey derivePrivateKeyPBES2(EncryptedPrivateKeyInfo eIn, AlgorithmIdentifier algId,
        char[] password) throws GeneralSecurityException, InvalidCipherTextException {
    PBES2Parameters pbeParams = PBES2Parameters.getInstance((ASN1Sequence) algId.getParameters());
    CipherParameters cipherParams = extractPBES2CipherParams(password, pbeParams);

    EncryptionScheme scheme = pbeParams.getEncryptionScheme();
    BufferedBlockCipher cipher;/* w  w w  .  j  av  a 2 s  .c  om*/
    if (scheme.getAlgorithm().equals(PKCSObjectIdentifiers.RC2_CBC)) {
        RC2CBCParameter rc2Params = RC2CBCParameter.getInstance(scheme);
        byte[] iv = rc2Params.getIV();
        CipherParameters param = new ParametersWithIV(cipherParams, iv);
        cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new RC2Engine()));
        cipher.init(false, param);
    } else {
        byte[] iv = ((ASN1OctetString) scheme.getObject()).getOctets();
        // this version, done for BC 1.49 compat, caused #1238.
        //            byte[] iv = ASN1OctetString.getInstance(scheme).getOctets();
        CipherParameters param = new ParametersWithIV(cipherParams, iv);
        cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESedeEngine()));
        cipher.init(false, param);
    }

    byte[] data = eIn.getEncryptedData();
    byte[] out = new byte[cipher.getOutputSize(data.length)];
    int len = cipher.processBytes(data, 0, data.length, out, 0);
    len += cipher.doFinal(out, len);
    byte[] pkcs8 = new byte[len];
    System.arraycopy(out, 0, pkcs8, 0, len);
    KeyFactory fact = KeyFactory.getInstance("RSA"); // It seems to work for both RSA and DSA.
    return fact.generatePrivate(new PKCS8EncodedKeySpec(pkcs8));
}