Example usage for org.bouncycastle.cms.jcajce JceCMSContentEncryptorBuilder JceCMSContentEncryptorBuilder

List of usage examples for org.bouncycastle.cms.jcajce JceCMSContentEncryptorBuilder JceCMSContentEncryptorBuilder

Introduction

In this page you can find the example usage for org.bouncycastle.cms.jcajce JceCMSContentEncryptorBuilder JceCMSContentEncryptorBuilder.

Prototype

public JceCMSContentEncryptorBuilder(ASN1ObjectIdentifier encryptionOID, int keySize) 

Source Link

Usage

From source file:mitm.common.security.smime.SMIMEBuilderImpl.java

License:Open Source License

@Override
public void encrypt(SMIMEEncryptionAlgorithm algorithm, int keySize) throws SMIMEBuilderException {
    try {// w ww  . j  av  a  2 s.c  o m
        JceCMSContentEncryptorBuilder encryptorBuilder = new JceCMSContentEncryptorBuilder(algorithm.getOID(),
                keySize);

        encryptorBuilder.setProvider(nonSensitiveProvider);

        bodyPart = envelopedGenerator.generate(bodyPart, encryptorBuilder.build());

        /*
         * We will use the deprecated content-type if required. We do this by changing the content-type
         * header. I wish I could specify the content-type for the envelopedGenerator but that's not 
         * possible without completely reimplemening envelopedGenerator. This is a workaround until
         * BC allows me to set the content-type
         */
        if (useDeprecatedContentTypes) {
            bodyPart.setHeader("Content-Type", SMIMEHeader.DEPRECATED_ENCRYPTED_CONTENT_TYPE);
        }

        envelopedGenerator = new PrivateSMIMEEnvelopedGenerator();
    } catch (SMIMEException e) {
        throw new SMIMEBuilderException(e);
    } catch (MessagingException e) {
        throw new SMIMEBuilderException(e);
    } catch (CMSException e) {
        throw new SMIMEBuilderException(e);
    }
}

From source file:mitm.common.security.smime.SMIMEBuilderImplTest.java

License:Open Source License

@Test
public void testEncryptSignedQuotedPrintableSoftBreaksDirectBC() throws Exception {
    MimeMessage message = loadMessage("qp-soft-breaks-signed.eml");

    SMIMEEnvelopedGenerator envelopedGenerator = new SMIMEEnvelopedGenerator();

    JceKeyTransRecipientInfoGenerator infoGenerator = new JceKeyTransRecipientInfoGenerator(
            encryptionCertificate);/*ww  w .  j a v a  2 s . co  m*/

    envelopedGenerator.addRecipientInfoGenerator(infoGenerator);

    JceCMSContentEncryptorBuilder encryptorBuilder = new JceCMSContentEncryptorBuilder(
            new ASN1ObjectIdentifier("1.2.840.113549.3.7"), 0).setProvider("BC");

    MimeBodyPart bodyPart = envelopedGenerator.generate(message, encryptorBuilder.build());

    MimeMessage newMessage = new MimeMessage(MailSession.getDefaultSession());

    newMessage.setContent(bodyPart.getContent(), bodyPart.getContentType());

    newMessage.saveChanges();

    File file = new File(tempDir, "testEncryptSignedQuotedPrintableSoftBreaksDirectBC.eml");

    FileOutputStream output = new FileOutputStream(file);

    MailUtils.writeMessage(newMessage, output);

    newMessage = MailUtils.loadMessage(file);

    assertEquals(SMIMEHeader.Type.ENCRYPTED, SMIMEHeader.getSMIMEContentType(newMessage));

    File opensslOutputFileSigned = new File(tempDir,
            "testEncryptSignedQuotedPrintableSoftBreaksDirectBC-openssl-signed.eml");

    decryptMessage(file, privateKeyEntry.getPrivateKey(), opensslOutputFileSigned);

    newMessage = MailUtils.loadMessage(opensslOutputFileSigned);

    assertTrue(newMessage.isMimeType("multipart/signed"));

    File opensslOutputFile = new File(tempDir,
            "testEncryptSignedQuotedPrintableSoftBreaksDirectBC-openssl.eml");

    verifyMessage(opensslOutputFileSigned, rootCertificate, opensslOutputFile);

    newMessage = MailUtils.loadMessage(opensslOutputFile);

    assertTrue(newMessage.isMimeType("text/plain"));

    assertEquals(SMIMEHeader.Type.NO_SMIME, SMIMEHeader.getSMIMEContentType(newMessage));
}