Example usage for org.bouncycastle.cms CMSAlgorithm AES256_CBC

List of usage examples for org.bouncycastle.cms CMSAlgorithm AES256_CBC

Introduction

In this page you can find the example usage for org.bouncycastle.cms CMSAlgorithm AES256_CBC.

Prototype

ASN1ObjectIdentifier AES256_CBC

To view the source code for org.bouncycastle.cms CMSAlgorithm AES256_CBC.

Click Source Link

Usage

From source file:de.mendelson.comm.as2.message.AS2MessageCreation.java

/**
 * Encrypts a byte array and returns it/*from   w ww  .  j  ava 2 s .  c  o  m*/
 */
private void encryptDataToMessage(AS2Message message, String receiverCryptAlias, int encryptionType,
        Partner receiver) throws Exception {
    AS2MessageInfo info = (AS2MessageInfo) message.getAS2Info();
    BCCryptoHelper cryptoHelper = new BCCryptoHelper();
    X509Certificate certificate = this.encryptionCertManager.getX509Certificate(receiverCryptAlias);
    CMSEnvelopedDataStreamGenerator dataGenerator = new CMSEnvelopedDataStreamGenerator();
    dataGenerator
            .addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(certificate).setProvider("BC"));
    DeferredFileOutputStream encryptedOutput = null;
    OutputStream out = null;
    try {
        //if the data is less then 3MB perform the operaion in memory else stream to disk
        encryptedOutput = new DeferredFileOutputStream(3 * 1024 * 1024, "as2encryptdata_", ".mem", null);
        if (encryptionType == AS2Message.ENCRYPTION_3DES) {
            out = dataGenerator.open(encryptedOutput,
                    new JceCMSContentEncryptorBuilder(CMSAlgorithm.DES_EDE3_CBC).setProvider("BC").build());
        } else if (encryptionType == AS2Message.ENCRYPTION_DES) {
            out = dataGenerator.open(encryptedOutput,
                    new JceCMSContentEncryptorBuilder(CMSAlgorithm.DES_EDE3_WRAP, 56).setProvider("BC")
                            .build());
        } else if (encryptionType == AS2Message.ENCRYPTION_RC2_40) {
            out = dataGenerator.open(encryptedOutput,
                    new JceCMSContentEncryptorBuilder(CMSAlgorithm.RC2_CBC, 40).setProvider("BC").build());
        } else if (encryptionType == AS2Message.ENCRYPTION_RC2_64) {
            out = dataGenerator.open(encryptedOutput,
                    new JceCMSContentEncryptorBuilder(CMSAlgorithm.RC2_CBC, 64).setProvider("BC").build());
        } else if (encryptionType == AS2Message.ENCRYPTION_RC2_128) {
            out = dataGenerator.open(encryptedOutput,
                    new JceCMSContentEncryptorBuilder(CMSAlgorithm.RC2_CBC, 128).setProvider("BC").build());
        } else if (encryptionType == AS2Message.ENCRYPTION_RC2_196) {
            out = dataGenerator.open(encryptedOutput,
                    new JceCMSContentEncryptorBuilder(CMSAlgorithm.RC2_CBC, 196).setProvider("BC").build());
        } else if (encryptionType == AS2Message.ENCRYPTION_AES_128) {
            out = dataGenerator.open(encryptedOutput,
                    new JceCMSContentEncryptorBuilder(CMSAlgorithm.AES128_CBC).setProvider("BC").build());
        } else if (encryptionType == AS2Message.ENCRYPTION_AES_192) {
            out = dataGenerator.open(encryptedOutput,
                    new JceCMSContentEncryptorBuilder(CMSAlgorithm.AES192_CBC).setProvider("BC").build());
        } else if (encryptionType == AS2Message.ENCRYPTION_AES_256) {
            out = dataGenerator.open(encryptedOutput,
                    new JceCMSContentEncryptorBuilder(CMSAlgorithm.AES256_CBC).setProvider("BC").build());
        } else if (encryptionType == AS2Message.ENCRYPTION_RC4_40) {
            out = dataGenerator.open(encryptedOutput,
                    new JceCMSContentEncryptorBuilder(new ASN1ObjectIdentifier(
                            cryptoHelper.convertAlgorithmNameToOID(BCCryptoHelper.ALGORITHM_RC4)), 40)
                                    .setProvider("BC").build());
        } else if (encryptionType == AS2Message.ENCRYPTION_RC4_56) {
            out = dataGenerator.open(encryptedOutput,
                    new JceCMSContentEncryptorBuilder(new ASN1ObjectIdentifier(
                            cryptoHelper.convertAlgorithmNameToOID(BCCryptoHelper.ALGORITHM_RC4)), 56)
                                    .setProvider("BC").build());
        } else if (encryptionType == AS2Message.ENCRYPTION_RC4_128) {
            out = dataGenerator.open(encryptedOutput,
                    new JceCMSContentEncryptorBuilder(new ASN1ObjectIdentifier(
                            cryptoHelper.convertAlgorithmNameToOID(BCCryptoHelper.ALGORITHM_RC4)), 128)
                                    .setProvider("BC").build());
        }
        if (out == null) {
            throw new Exception("Internal failure: unsupported encryption type " + encryptionType);
        }
        InputStream in = null;
        try {
            in = message.getDecryptedRawDataInputStream();
            this.copyStreams(in, out);
        } finally {
            if (in != null) {
                in.close();
            }
        }
    } finally {
        if (out != null) {
            out.close();
        }
        if (encryptedOutput != null) {
            encryptedOutput.close();
        }
    }
    //size of the data was < than the threshold
    if (encryptedOutput.isInMemory()) {
        message.setRawData(encryptedOutput.getData());
    } else {
        //data has been written to a temp file: reread and return
        ByteArrayOutputStream memOut = new ByteArrayOutputStream();
        encryptedOutput.writeTo(memOut);
        memOut.flush();
        memOut.close();
        //finally delete the temp file
        boolean deleted = encryptedOutput.getFile().delete();
        message.setRawData(memOut.toByteArray());
    }
    if (this.logger != null) {
        String cryptAlias = this.encryptionCertManager
                .getAliasByFingerprint(receiver.getCryptFingerprintSHA1());
        this.logger.log(Level.INFO, this.rb.getResourceString("message.encrypted",
                new Object[] { info.getMessageId(), cryptAlias,
                        this.rbMessage.getResourceString("encryption." + receiver.getEncryptionType()) }),
                info);
    }
}

From source file:eu.inn.biometric.signature.crypto.BCCryptoProvider.java

License:Open Source License

@Override
public byte[] encrypt(byte[] toEncrypt, List<X509Certificate> certs, Integer maxKeyLength) throws Exception {
    int keySize = Cipher.getMaxAllowedKeyLength("AES");
    if (maxKeyLength != null)
        if (keySize > maxKeyLength)
            keySize = maxKeyLength;/*from   w w  w. jav  a2 s . c  o  m*/
    String algIdentifier = CMSAlgorithm.AES128_CBC.getId();
    if (keySize >= 256)
        algIdentifier = CMSAlgorithm.AES256_CBC.getId();
    CMSEnvelopedDataGenerator gen = new CMSEnvelopedDataGenerator();
    for (X509Certificate cert : certs)
        gen.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(cert));
    CMSTypedData data = new CMSProcessableByteArray(toEncrypt);
    CMSEnvelopedData enveloped = gen.generate(data,
            new JceCMSContentEncryptorBuilder(new ASN1ObjectIdentifier(algIdentifier)).build());
    return enveloped.getEncoded();
}

From source file:no.difi.sdp.client.internal.CreateCMSDocument.java

License:Apache License

public CreateCMSDocument() {
    Security.addProvider(new BouncyCastleProvider());

    keyEncryptionScheme = rsaesOaepIdentifier();
    cmsEncryptionAlgorithm = CMSAlgorithm.AES256_CBC;
}

From source file:no.digipost.api.client.security.CryptoUtil.java

License:Apache License

public static void verifyJCE() {
    try {/*from ww w.j a  v a2 s.c o m*/
        if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
            Security.addProvider(new BouncyCastleProvider());
        }
        new JceCMSContentEncryptorBuilder(CMSAlgorithm.AES256_CBC)
                .setProvider(BouncyCastleProvider.PROVIDER_NAME).build();
    } catch (CMSException e) {
        throw new RuntimeException(
                "Feil under initialisering av algoritmer. Er Java Cryptographic Excetsions (JCE) installert?",
                e);
    }
}

From source file:no.digipost.api.client.util.Encrypter.java

License:Apache License

private Encrypter(DigipostPublicKey key) {
    this.key = key;
    this.encryptorBuilder = new JceCMSContentEncryptorBuilder(CMSAlgorithm.AES256_CBC)
            .setProvider(BouncyCastleProvider.PROVIDER_NAME);
}

From source file:no.digipost.api.useragreements.client.security.CryptoUtil.java

License:Apache License

public static void addBouncyCastleProviderAndVerify_AES256_CBC_Support() {
    try {//w w w  . j a v a  2  s. co m
        Security.addProvider(new BouncyCastleProvider());
        LOG.debug("Registered BouncyCastleProvider");
        new JceCMSContentEncryptorBuilder(CMSAlgorithm.AES256_CBC)
                .setProvider(BouncyCastleProvider.PROVIDER_NAME).build();
        LOG.debug("Support for AES256_CBC ok");
    } catch (CMSException e) {
        throw new RuntimeException(
                "Feil under initialisering av algoritmer. Er Java Cryptographic Excetsions (JCE) installert?",
                e);
    }
}