001: package org.bouncycastle.mail.smime.examples;
002:
003: import java.io.FileInputStream;
004: import java.io.FileOutputStream;
005: import java.security.KeyStore;
006: import java.security.cert.Certificate;
007: import java.security.cert.X509Certificate;
008: import java.util.Enumeration;
009: import java.util.Properties;
010:
011: import javax.mail.Address;
012: import javax.mail.Message;
013: import javax.mail.Session;
014: import javax.mail.internet.InternetAddress;
015: import javax.mail.internet.MimeBodyPart;
016: import javax.mail.internet.MimeMessage;
017:
018: import org.bouncycastle.mail.smime.SMIMEEnvelopedGenerator;
019:
020: /**
021: * a simple example that creates a single encrypted mail message.
022: * <p>
023: * The key store can be created using the class in
024: * org.bouncycastle.jce.examples.PKCS12Example - the program expects only one
025: * key to be present in the key file.
026: * <p>
027: * Note: while this means that both the private key is available to
028: * the program, the private key is retrieved from the keystore only for
029: * the purposes of locating the corresponding public key, in normal circumstances
030: * you would only be doing this with a certificate available.
031: */
032: public class CreateEncryptedMail {
033: public static void main(String args[]) throws Exception {
034: if (args.length != 2) {
035: System.err
036: .println("usage: CreateEncryptedMail pkcs12Keystore password");
037: System.exit(0);
038: }
039:
040: //
041: // Open the key store
042: //
043: KeyStore ks = KeyStore.getInstance("PKCS12", "BC");
044:
045: ks.load(new FileInputStream(args[0]), args[1].toCharArray());
046:
047: Enumeration e = ks.aliases();
048: String keyAlias = null;
049:
050: while (e.hasMoreElements()) {
051: String alias = (String) e.nextElement();
052:
053: if (ks.isKeyEntry(alias)) {
054: keyAlias = alias;
055: }
056: }
057:
058: if (keyAlias == null) {
059: System.err.println("can't find a private key!");
060: System.exit(0);
061: }
062:
063: Certificate[] chain = ks.getCertificateChain(keyAlias);
064:
065: //
066: // create the generator for creating an smime/encrypted message
067: //
068: SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator();
069:
070: gen.addKeyTransRecipient((X509Certificate) chain[0]);
071:
072: //
073: // create a subject key id - this has to be done the same way as
074: // it is done in the certificate associated with the private key
075: // version 3 only.
076: //
077: /*
078: MessageDigest dig = MessageDigest.getInstance("SHA1", "BC");
079:
080: dig.update(cert.getPublicKey().getEncoded());
081:
082: gen.addKeyTransRecipient(cert.getPublicKey(), dig.digest());
083: */
084:
085: //
086: // create the base for our message
087: //
088: MimeBodyPart msg = new MimeBodyPart();
089:
090: msg.setText("Hello world!");
091:
092: MimeBodyPart mp = gen.generate(msg,
093: SMIMEEnvelopedGenerator.RC2_CBC, "BC");
094: //
095: // Get a Session object and create the mail message
096: //
097: Properties props = System.getProperties();
098: Session session = Session.getDefaultInstance(props, null);
099:
100: Address fromUser = new InternetAddress(
101: "\"Eric H. Echidna\"<eric@bouncycastle.org>");
102: Address toUser = new InternetAddress("example@bouncycastle.org");
103:
104: MimeMessage body = new MimeMessage(session);
105: body.setFrom(fromUser);
106: body.setRecipient(Message.RecipientType.TO, toUser);
107: body.setSubject("example encrypted message");
108: body.setContent(mp.getContent(), mp.getContentType());
109: body.saveChanges();
110:
111: body.writeTo(new FileOutputStream("encrypted.message"));
112: }
113: }
|