All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bouncycastle.mail.smime.examples.ReadLargeEncryptedMail Maven / Gradle / Ivy

Go to download

The Bouncy Castle Java CMS and S/MIME APIs for handling the CMS and S/MIME protocols. This jar contains CMS and S/MIME APIs for JDK 1.5. The APIs can be used in conjunction with a JCE/JCA provider such as the one provided with the Bouncy Castle Cryptography APIs. If the S/MIME API is used, the JavaMail API and the Java activation framework will also be needed.

There is a newer version: 1.46
Show newest version
package org.bouncycastle.mail.smime.examples;

import java.security.KeyStore;
import java.security.cert.X509Certificate;
import java.util.Properties;

import javax.mail.Session;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;

import org.bouncycastle.cms.RecipientId;
import org.bouncycastle.cms.RecipientInformation;
import org.bouncycastle.cms.RecipientInformationStore;
import org.bouncycastle.mail.smime.SMIMEEnvelopedParser;
import org.bouncycastle.mail.smime.SMIMEUtil;
import org.bouncycastle.mail.smime.util.SharedFileInputStream;

/**
 * a simple example that reads an encrypted email using the large file model.
 * 

* The key store can be created using the class in * org.bouncycastle.jce.examples.PKCS12Example - the program expects only one * key to be present. */ public class ReadLargeEncryptedMail { public static void main( String args[]) throws Exception { if (args.length != 3) { System.err.println("usage: ReadLargeEncryptedMail pkcs12Keystore password outputFile"); System.exit(0); } // // Open the key store // KeyStore ks = KeyStore.getInstance("PKCS12", "BC"); String keyAlias = ExampleUtils.findKeyAlias(ks, args[0], args[1].toCharArray()); // // find the certificate for the private key and generate a // suitable recipient identifier. // X509Certificate cert = (X509Certificate)ks.getCertificate(keyAlias); RecipientId recId = new RecipientId(); recId.setSerialNumber(cert.getSerialNumber()); recId.setIssuer(cert.getIssuerX500Principal().getEncoded()); // // Get a Session object with the default properties. // Properties props = System.getProperties(); Session session = Session.getDefaultInstance(props, null); MimeMessage msg = new MimeMessage(session, new SharedFileInputStream("encrypted.message")); SMIMEEnvelopedParser m = new SMIMEEnvelopedParser(msg); RecipientInformationStore recipients = m.getRecipientInfos(); RecipientInformation recipient = recipients.get(recId); MimeBodyPart res = SMIMEUtil.toMimeBodyPart(recipient.getContentStream(ks.getKey(keyAlias, null), "BC")); ExampleUtils.dumpContent(res, args[2]); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy