FileDocCategorySizeDatePackage
ReadLargeEncryptedMail.javaAPI DocBouncy Castle Crypto API 1.41 (Java 1.5)2419Wed Oct 01 10:55:28 BST 2008org.bouncycastle.mail.smime.examples

ReadLargeEncryptedMail

public class ReadLargeEncryptedMail extends Object
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.

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

        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]);