Example usage for org.bouncycastle.cms RecipientInformation getContentStream

List of usage examples for org.bouncycastle.cms RecipientInformation getContentStream

Introduction

In this page you can find the example usage for org.bouncycastle.cms RecipientInformation getContentStream.

Prototype

public CMSTypedStream getContentStream(Recipient recipient) throws CMSException, IOException 

Source Link

Document

Return a CMSTypedStream representing the content in the EnvelopedData after recovering the content encryption/MAC key using the passed in Recipient.

Usage

From source file:de.mendelson.util.security.BCCryptoHelper.java

/**
 * Used for mendelson rosettaNet/*  w w w. java2s  . co m*/
 */
public MimeBodyPart decrypt(MimeBodyPart part, Certificate cert, Key key)
        throws GeneralSecurityException, MessagingException, CMSException, IOException, SMIMEException {
    if (!this.isEncrypted(part)) {
        throw new GeneralSecurityException(
                "decrypt: Unable to decrypt - Content-Type indicates data isn't encrypted");
    }
    X509Certificate x509Cert = castCertificate(cert);
    SMIMEEnveloped envelope = new SMIMEEnveloped(part);
    RecipientId recipientId = new JceKeyTransRecipientId(x509Cert);
    RecipientInformation recipient = envelope.getRecipientInfos().get(recipientId);
    if (recipient == null) {
        throw new GeneralSecurityException(
                "decrypt: Unable to decrypt data - wrong key used to decrypt the data.");
    } else {
        MimeBodyPart bodyPart = SMIMEUtil.toMimeBodyPart(recipient.getContentStream(
                new JceKeyTransEnvelopedRecipient(this.getPrivateKey(key)).setProvider("BC")));
        return (bodyPart);
    }
}

From source file:de.mendelson.util.security.BCCryptoHelper.java

/**
 * Decrypts a formerly encrypted stream. An exception will be thrown if
 * decryption is not possible//from w  w  w  .j  av a 2s  . co m
 */
public void decryptCMS(InputStream encrypted, OutputStream decrypted, Certificate cert, Key key)
        throws Exception {
    BufferedInputStream bufferedEncrypted = new BufferedInputStream(encrypted);
    BufferedOutputStream bufferedDecrypted = new BufferedOutputStream(decrypted);
    X509Certificate x509Cert = this.castCertificate(cert);
    CMSEnvelopedDataParser parser = new CMSEnvelopedDataParser(bufferedEncrypted);
    RecipientId recipientId = new JceKeyTransRecipientId(x509Cert);
    RecipientInformation recipient = parser.getRecipientInfos().get(recipientId);
    if (recipient != null) {
        CMSTypedStream cmsEncrypted = recipient
                .getContentStream(new JceKeyTransEnvelopedRecipient(this.getPrivateKey(key)).setProvider("BC"));
        InputStream encryptedContent = cmsEncrypted.getContentStream();
        this.copyStreams(encryptedContent, bufferedDecrypted);
        bufferedDecrypted.flush();
    } else {
        throw new GeneralSecurityException("Wrong key used to decrypt the data.");
    }
}

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

License:Open Source License

/**
 * @param is data to be decrypted//www  .j  a  v  a  2 s  .com
 * @param os decrypted data
 * @param key to be used for the decryption
 * @param providerName the provider that should do the decryption
 * @throws Exception
 */
public static void decrypt(final InputStream is, OutputStream os, PrivateKey key, String providerName)
        throws Exception {
    final InputStream bis = new BufferedInputStream(is, bufferSize);
    final OutputStream bos = new BufferedOutputStream(os, bufferSize);
    @SuppressWarnings("unchecked")
    final Iterator<RecipientInformation> it = new CMSEnvelopedDataParser(bis).getRecipientInfos()
            .getRecipients().iterator();
    if (it.hasNext()) {
        final RecipientInformation recipientInformation = it.next();
        JceKeyTransEnvelopedRecipient rec = new JceKeyTransEnvelopedRecipient(key);
        rec.setProvider(providerName);
        rec.setContentProvider(BouncyCastleProvider.PROVIDER_NAME);
        final CMSTypedStream recData = recipientInformation.getContentStream(rec);
        final InputStream ris = recData.getContentStream();
        fromInToOut(ris, bos);
    }
    os.close();
}

From source file:org.votingsystem.signature.util.Encryptor.java

License:Open Source License

public byte[] decryptCMS(byte[] base64EncryptedData) throws Exception {
    byte[] cmsEncryptedData = Base64.getDecoder().decode(base64EncryptedData);
    CMSEnvelopedDataParser ep = new CMSEnvelopedDataParser(cmsEncryptedData);
    RecipientInformationStore recipients = ep.getRecipientInfos();
    Collection c = recipients.getRecipients();
    Iterator it = c.iterator();/* w ww.  ja v  a  2  s.com*/
    byte[] result = null;
    if (it.hasNext()) {
        RecipientInformation recipient = (RecipientInformation) it.next();
        CMSTypedStream recData = recipient.getContentStream(
                new JceKeyTransEnvelopedRecipient(privateKey).setProvider(ContextVS.PROVIDER));
        return FileUtils.getBytesFromStream(recData.getContentStream());
    }
    return result;
}

From source file:org.votingsystem.signature.util.Encryptor.java

License:Open Source License

public static byte[] decryptCMS(byte[] base64EncryptedData, PrivateKey privateKey)
        throws CMSException, IOException {
    //byte[] cmsEncryptedData = Base64.getDecoder().decode(base64EncryptedData);
    byte[] cmsEncryptedData = org.bouncycastle.util.encoders.Base64.decode(base64EncryptedData);
    CMSEnvelopedDataParser ep = new CMSEnvelopedDataParser(cmsEncryptedData);
    RecipientInformationStore recipients = ep.getRecipientInfos();
    Collection c = recipients.getRecipients();
    Iterator it = c.iterator();/*from ww  w.j  a v a  2s. com*/
    byte[] result = null;
    if (it.hasNext()) {
        RecipientInformation recipient = (RecipientInformation) it.next();
        //assertEquals(recipient.getKeyEncryptionAlgOID(), PKCSObjectIdentifiers.rsaEncryption.getId());
        CMSTypedStream recData = recipient.getContentStream(
                new JceKeyTransEnvelopedRecipient(privateKey).setProvider(ContextVS.PROVIDER));
        return FileUtils.getBytesFromStream(recData.getContentStream());
    }
    return result;
}

From source file:org.votingsystem.signature.util.Encryptor.java

License:Open Source License

public static byte[] decryptCMSStream(PrivateKey privateKey, byte[] cmsEncryptedData) throws Exception {
    CMSEnvelopedDataParser ep = new CMSEnvelopedDataParser(cmsEncryptedData);
    RecipientInformationStore recipients = ep.getRecipientInfos();
    Collection c = recipients.getRecipients();
    Iterator it = c.iterator();//from w  w  w. j a  v a  2 s.  com

    byte[] result = null;
    if (it.hasNext()) {
        RecipientInformation recipient = (RecipientInformation) it.next();
        //assertEquals(recipient.getKeyEncryptionAlgOID(), PKCSObjectIdentifiers.rsaEncryption.getId());
        CMSTypedStream recData = recipient.getContentStream(
                new JceKeyTransEnvelopedRecipient(privateKey).setProvider(ContextVS.PROVIDER));
        InputStream dataStream = recData.getContentStream();
        ByteArrayOutputStream dataOut = new ByteArrayOutputStream();
        byte[] buf = new byte[4096];
        int len = 0;
        while ((len = dataStream.read(buf)) >= 0) {
            dataOut.write(buf, 0, len);
        }
        dataOut.close();
        result = dataOut.toByteArray();
        //assertEquals(true, Arrays.equals(data, dataOut.toByteArray()));
    }
    return result;
}