Example usage for org.bouncycastle.asn1.pkcs PKCSObjectIdentifiers RC2_CBC

List of usage examples for org.bouncycastle.asn1.pkcs PKCSObjectIdentifiers RC2_CBC

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.pkcs PKCSObjectIdentifiers RC2_CBC.

Prototype

ASN1ObjectIdentifier RC2_CBC

To view the source code for org.bouncycastle.asn1.pkcs PKCSObjectIdentifiers RC2_CBC.

Click Source Link

Document

1.2.840.113549.3.2

Usage

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;/*from   w  w  w.  j  a  v  a2  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));
}

From source file:org.xwiki.crypto.password.internal.pbe.factory.BcPBES2Rc2CipherFactory.java

License:Open Source License

@Override
protected PasswordBasedCipher getPasswordBasedCipher(boolean forEncryption, KeyDerivationFunction kdf,
        SymmetricCipherParameters params) {
    return new AbstractBcPBES2Cipher(getCipherFactory().getInstance(forEncryption, params), kdf, params) {
        @Override//from  w w  w.jav a2  s  .  c om
        protected EncryptionScheme getScheme(SymmetricCipherParameters parameters) {
            return new EncryptionScheme(PKCSObjectIdentifiers.RC2_CBC,
                    new RC2CBCParameter(getRC2Version((KeyWithIVParameters) parameters),
                            ((KeyWithIVParameters) parameters).getIV()));
        }
    };
}