package net.suberic.crypto.examples; import net.suberic.crypto.*; import javax.mail.*; import javax.mail.internet.*; import java.util.Set; import java.security.Key; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; /** * An example of using this API to sign a JavaMail Message. */ public class SignMessage { /** * Runs the Example. */ public static void main(String[] argv) { try { Session mailSession = Session.getDefaultInstance(System.getProperties()); // create the message. MimeMessage newMessage = new MimeMessage(mailSession); String content = "This is a signed message. \n\nYou know it has actually been sent by this fictional person.\n\nImagine if you received an email claiming to be from this nonexistent person, but was actually from some other made-up persona? That would be bad.\n\n"; newMessage.setContent(content, "text/plain"); newMessage.setFrom(); newMessage.setRecipients(Message.RecipientType.TO, "example@bouncycastle.org"); newMessage.setSubject("This one is signed."); newMessage.saveChanges(); // get our signer(s) EncryptionUtils smimeUtils = EncryptionManager.getEncryptionUtils(EncryptionManager.SMIME); EncryptionUtils pgpUtils = EncryptionManager.getEncryptionUtils(EncryptionManager.PGP); // load the associated store(s) char[] smimePw = new String("hello world").toCharArray(); EncryptionKeyManager smimeKeyMgr = smimeUtils.createKeyManager(); smimeKeyMgr.loadPrivateKeystore(new FileInputStream(new File("./id.p12")), smimePw); EncryptionKeyManager pgpKeyMgr = pgpUtils.createKeyManager(); pgpKeyMgr.loadPrivateKeystore(new FileInputStream(new File("./bob.skr")), null); // get our keys. java.security.Key smimeKey = smimeKeyMgr.getPrivateKey("Eric's Key", smimePw); java.security.Key pgpKey = pgpKeyMgr.getPrivateKey((String) pgpKeyMgr.privateKeyAliases().iterator().next(), new String("TestingPassphrase").toCharArray()); // write smime message. MimeMessage smimeSignedMsg = smimeUtils.signMessage(mailSession, newMessage, smimeKey); smimeSignedMsg.writeTo(new com.sun.mail.util.CRLFOutputStream(new FileOutputStream(new File("smimeSigned.msg")))); // write pgp message MimeMessage pgpSignedMsg = pgpUtils.signMessage(mailSession, newMessage, pgpKey); pgpSignedMsg.writeTo(new com.sun.mail.util.CRLFOutputStream(new FileOutputStream(new File("pgpSigned.msg")))); } catch (Exception e) { e.printStackTrace(); } } }