Example usage for org.bouncycastle.asn1.pkcs EncryptionScheme getParameters

List of usage examples for org.bouncycastle.asn1.pkcs EncryptionScheme getParameters

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.pkcs EncryptionScheme getParameters.

Prototype

public ASN1Encodable getParameters() 

Source Link

Usage

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

License:Open Source License

/**
 * Initializes the block cipher and sets up its initialization parameters.
 *
 * @param  generator  Derived key generator.
 * @param  scheme  PKCS#5 encryption scheme.
 *///from w  w w.  j a  v a  2s  .c o m
private void initCipher(final PKCS5S2ParametersGenerator generator,
        final org.bouncycastle.asn1.pkcs.EncryptionScheme scheme) {
    final PBES2Algorithm alg = PBES2Algorithm.fromOid(scheme.getAlgorithm().getId());
    if (keyLength == 0) {
        keyLength = alg.getKeySize();
    }

    byte[] iv = null;
    CipherParameters cipherParameters = generator.generateDerivedParameters(keyLength);
    switch (alg) {

    case RC2:
        setCipher(alg.getCipherSpec().newInstance());

        final ASN1Sequence rc2Params = ASN1Sequence.getInstance(scheme.getParameters());
        if (rc2Params.size() > 1) {
            cipherParameters = new RC2Parameters(((KeyParameter) cipherParameters).getKey(),
                    ASN1Integer.getInstance(rc2Params.getObjectAt(0)).getValue().intValue());
            iv = ASN1OctetString.getInstance(rc2Params.getObjectAt(0)).getOctets();
        }
        break;

    case RC5:

        final ASN1Sequence rc5Params = ASN1Sequence.getInstance(scheme.getParameters());
        final int rounds = ASN1Integer.getInstance(rc5Params.getObjectAt(1)).getValue().intValue();
        final int blockSize = ASN1Integer.getInstance(rc5Params.getObjectAt(2)).getValue().intValue();
        if (blockSize == 32) {
            setCipher(new PaddedBufferedBlockCipher(new CBCBlockCipher(new RC532Engine()), new PKCS7Padding()));
        }
        cipherParameters = new RC5Parameters(((KeyParameter) cipherParameters).getKey(), rounds);
        if (rc5Params.size() > 3) {
            iv = ASN1OctetString.getInstance(rc5Params.getObjectAt(3)).getOctets();
        }
        break;

    default:
        setCipher(alg.getCipherSpec().newInstance());
        iv = ASN1OctetString.getInstance(scheme.getParameters()).getOctets();
    }
    if (iv != null) {
        cipherParameters = new ParametersWithIV(cipherParameters, iv);
    }
    setCipherParameters(cipherParameters);
}

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

License:Open Source License

/**
 * @param forEncryption if true the cipher is initialised for encryption, if false for decryption.
 * @param password the password that will be used to derive the key.
 * @param kdfParams key derivation function parameters.
 * @param scheme encryption scheme.//from   ww w.j  ava2  s. c o m
 * @return a initialized key derivation function with a specific password bytes conversion mode.
 */
protected PasswordBasedCipher getInstance(boolean forEncryption, byte[] password, KeyDerivationFunc kdfParams,
        EncryptionScheme scheme) {
    KeyDerivationFunction kdf = getKeyDerivationFunction(kdfParams);

    // Fix key size if needed.
    if (kdf.getKeySize() < 0 || !isSupportedKeySize(kdf.getKeySize())) {
        kdf.overrideKeySize(getKeySize());
    }

    return getPasswordBasedCipher(forEncryption, kdf, new KeyWithIVParameters(kdf.derive(password).getKey(),
            ((ASN1OctetString) scheme.getParameters()).getOctets()));
}

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

License:Open Source License

@Override
protected PasswordBasedCipher getInstance(boolean forEncryption, byte[] password, KeyDerivationFunc kdfParams,
        EncryptionScheme scheme) {
    KeyDerivationFunction kdf = getKeyDerivationFunction(kdfParams);
    RC5CBCParameter rc5Params = RC5CBCParameter.getInstance(scheme.getParameters());

    return getPasswordBasedCipher(forEncryption, kdf, getRC5CipherParameters(password, rc5Params, kdf));
}

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

License:Open Source License

@Override
protected PasswordBasedCipher getInstance(boolean forEncryption, byte[] password, KeyDerivationFunc kdfParams,
        EncryptionScheme scheme) {
    KeyDerivationFunction kdf = getKeyDerivationFunction(kdfParams);

    // Set key size according to the encryption scheme algorithm used.
    kdf.overrideKeySize(getAESKeySize(scheme.getAlgorithm()));

    return getPasswordBasedCipher(forEncryption, kdf, new KeyWithIVParameters(kdf.derive(password).getKey(),
            ((ASN1OctetString) scheme.getParameters()).getOctets()));
}

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

License:Open Source License

private PasswordBasedCipherFactory getPBES2CipherFactory(EncryptionScheme scheme) {
    try {//from   ww w . j  av  a  2  s  .  c o m
        if (scheme.getAlgorithm().equals(PKCSObjectIdentifiers.encryptionAlgorithm.branch("9"))) {
            RC5CBCParameter rc5Param = RC5CBCParameter.getInstance(scheme.getParameters());
            if (rc5Param.getBlockSizeInBits().intValue() > 64) {
                // RC5-CBC-Pad with a 128bits block size
                return manager.getInstance(PasswordBasedCipherFactory.class, "PBES2-RC5-64-CBC-Pad");
            }
        }
        return manager.getInstance(PasswordBasedCipherFactory.class, scheme.getAlgorithm().getId());
    } catch (ComponentLookupException e) {
        throw new UnsupportedOperationException("Password based cipher factory not found.", e);
    }
}

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

License:Open Source License

@Override
protected PasswordBasedCipher getInstance(boolean forEncryption, byte[] password, KeyDerivationFunc kdfParams,
        EncryptionScheme scheme) {
    KeyDerivationFunction kdf = getKeyDerivationFunction(kdfParams);
    RC2CBCParameter rc2Params = RC2CBCParameter.getInstance(scheme.getParameters());

    return getPasswordBasedCipher(forEncryption, kdf, getRC2CipherParameters(password, rc2Params, kdf));
}