Example usage for org.bouncycastle.cms.bc BcCMSContentEncryptorBuilder BcCMSContentEncryptorBuilder

List of usage examples for org.bouncycastle.cms.bc BcCMSContentEncryptorBuilder BcCMSContentEncryptorBuilder

Introduction

In this page you can find the example usage for org.bouncycastle.cms.bc BcCMSContentEncryptorBuilder BcCMSContentEncryptorBuilder.

Prototype

public BcCMSContentEncryptorBuilder(ASN1ObjectIdentifier encryptionOID) 

Source Link

Usage

From source file:org.apache.kerby.pkix.EnvelopedDataEngine.java

License:Apache License

/**
 * Uses a certificate to encrypt data in a CMS EnvelopedData structure and
 * returns the encoded EnvelopedData as bytes.
 * <p/>//from   ww  w .ja v a  2 s.co m
 * 'encKeyPack' contains a CMS type ContentInfo encoded according to [RFC3852].
 * The contentType field of the type ContentInfo is id-envelopedData (1.2.840.113549.1.7.3).
 * The content field is an EnvelopedData. The contentType field for the type
 * EnvelopedData is id-signedData (1.2.840.113549.1.7.2).
 *
 * @param dataToEnvelope
 * @param certificate
 * @return The EnvelopedData bytes.
 * @throws IOException
 * @throws CMSException
 * @throws CertificateEncodingException
 */
public static byte[] getEnvelopedReplyKeyPack(byte[] dataToEnvelope, X509Certificate certificate)
        throws IOException, CMSException, CertificateEncodingException {
    CMSProcessableByteArray content = new CMSProcessableByteArray(dataToEnvelope);

    CMSEnvelopedDataGenerator envelopeGenerator = new CMSEnvelopedDataGenerator();
    envelopeGenerator.addRecipientInfoGenerator(
            new BcRSAKeyTransRecipientInfoGenerator(new JcaX509CertificateHolder(certificate)));
    CMSEnvelopedData envdata = envelopeGenerator.generate(content,
            new BcCMSContentEncryptorBuilder(CMSAlgorithm.DES_EDE3_CBC).build());

    return envdata.getEncoded();
}

From source file:org.ejbca.util.CMS.java

License:Open Source License

/**
 * @param is data to be encrypted// w  ww.j  a v a  2 s.  co m
 * @param os encrypted data
 * @param cert certificate with the public key to be used for the encryption
 * @param symmAlgOid the symmetric encryption algorithm to use, for example CMSEnvelopedGenerator.AES128_CBC
 * @throws Exception
 */
public static void encrypt(final InputStream is, final OutputStream os, final X509Certificate cert,
        final String symmAlgOid) throws Exception {
    final InputStream bis = new BufferedInputStream(is, bufferSize);
    final OutputStream bos = new BufferedOutputStream(os, bufferSize);
    final CMSEnvelopedDataStreamGenerator edGen = new CMSEnvelopedDataStreamGenerator();
    edGen.addRecipientInfoGenerator(
            new JceKeyTransRecipientInfoGenerator("hej".getBytes(), cert.getPublicKey()));
    BcCMSContentEncryptorBuilder bcCMSContentEncryptorBuilder = new BcCMSContentEncryptorBuilder(
            new ASN1ObjectIdentifier(symmAlgOid));
    final OutputStream out = edGen.open(bos, bcCMSContentEncryptorBuilder.build());
    fromInToOut(bis, out);
    bos.close();
    os.close();
}