01: package org.bouncycastle.mail.smime.examples;
02:
03: import java.security.KeyStore;
04: import java.security.cert.X509Certificate;
05: import java.util.Properties;
06:
07: import javax.mail.Session;
08: import javax.mail.internet.MimeBodyPart;
09: import javax.mail.internet.MimeMessage;
10:
11: import org.bouncycastle.cms.RecipientId;
12: import org.bouncycastle.cms.RecipientInformation;
13: import org.bouncycastle.cms.RecipientInformationStore;
14: import org.bouncycastle.mail.smime.SMIMEEnvelopedParser;
15: import org.bouncycastle.mail.smime.SMIMEUtil;
16: import org.bouncycastle.mail.smime.util.SharedFileInputStream;
17:
18: /**
19: * a simple example that reads an encrypted email using the large file model.
20: * <p>
21: * The key store can be created using the class in
22: * org.bouncycastle.jce.examples.PKCS12Example - the program expects only one
23: * key to be present.
24: */
25: public class ReadLargeEncryptedMail {
26: public static void main(String args[]) throws Exception {
27: if (args.length != 3) {
28: System.err
29: .println("usage: ReadLargeEncryptedMail pkcs12Keystore password outputFile");
30: System.exit(0);
31: }
32:
33: //
34: // Open the key store
35: //
36: KeyStore ks = KeyStore.getInstance("PKCS12", "BC");
37: String keyAlias = ExampleUtils.findKeyAlias(ks, args[0],
38: args[1].toCharArray());
39:
40: //
41: // find the certificate for the private key and generate a
42: // suitable recipient identifier.
43: //
44: X509Certificate cert = (X509Certificate) ks
45: .getCertificate(keyAlias);
46: RecipientId recId = new RecipientId();
47:
48: recId.setSerialNumber(cert.getSerialNumber());
49: recId.setIssuer(cert.getIssuerX500Principal().getEncoded());
50:
51: //
52: // Get a Session object with the default properties.
53: //
54: Properties props = System.getProperties();
55:
56: Session session = Session.getDefaultInstance(props, null);
57:
58: MimeMessage msg = new MimeMessage(session,
59: new SharedFileInputStream("encrypted.message"));
60:
61: SMIMEEnvelopedParser m = new SMIMEEnvelopedParser(msg);
62:
63: RecipientInformationStore recipients = m.getRecipientInfos();
64: RecipientInformation recipient = recipients.get(recId);
65:
66: MimeBodyPart res = SMIMEUtil.toMimeBodyPart(recipient
67: .getContentStream(ks.getKey(keyAlias, null), "BC"));
68:
69: ExampleUtils.dumpContent(res, args[2]);
70: }
71: }
|