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