List of usage examples for org.bouncycastle.mail.smime SMIMEEnvelopedGenerator AES256_CBC
String AES256_CBC
To view the source code for org.bouncycastle.mail.smime SMIMEEnvelopedGenerator AES256_CBC.
Click Source Link
From source file:chapter9.EnvelopedMailExample.java
/** * * @param args/*from w ww . j ava 2 s .c o m*/ * @throws Exception */ public static void main(String[] args) throws Exception { KeyStore credentials = Utils.createCredentials(); PrivateKey key = (PrivateKey) credentials.getKey(Utils.END_ENTITY_ALIAS, Utils.KEY_PASSWD); Certificate[] chain = credentials.getCertificateChain(Utils.END_ENTITY_ALIAS); X509Certificate cert = (X509Certificate) chain[0]; //1.- Create the message we want encrypted MimeBodyPart dataPart = new MimeBodyPart(); dataPart.setText("Hello World!!"); //2.- Set up the generator SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator(); gen.addKeyTransRecipient(cert); //3.- Generate the enveloped message MimeBodyPart envPart = gen.generate(dataPart, SMIMEEnvelopedGenerator.AES256_CBC, CryptoDefs.Provider.BC.getName()); //4.- Create the mail message MimeMessage mail = Utils.createMimeMessage("example enveloped message", envPart.getContent(), envPart.getContentType()); //5.- Create the enveloped object from the mail message SMIMEEnveloped enveloped = new SMIMEEnveloped(mail); //6.- Look for our recipient identifier RecipientId recId = new KeyTransRecipientId(new X500Name(cert.getIssuerX500Principal().getName()), cert.getSerialNumber()); RecipientInformationStore recipients = enveloped.getRecipientInfos(); RecipientInformation recipient = recipients.get(recId); if (recipient != null) { //7.- Decryption step MimeBodyPart recoveredPart = SMIMEUtil .toMimeBodyPart(recipient.getContent(key, CryptoDefs.Provider.BC.getName())); //8.- Content display step System.out.print("\t Content:"); System.out.println(recoveredPart.getContent()); } else System.out.println("\t could not find a matching recipient!!"); }
From source file:chapter9.EnvelopedSignedMailExample.java
/** * * @param args/*from w w w.j av a 2 s . co m*/ * @throws Exception */ public static void main(String[] args) throws Exception { KeyStore credentials = Utils.createCredentials(); PrivateKey key = (PrivateKey) credentials.getKey(Utils.END_ENTITY_ALIAS, Utils.KEY_PASSWD); Certificate[] chain = credentials.getCertificateChain(Utils.END_ENTITY_ALIAS); CertStore certsAndCRLs = CertStore.getInstance("Collection", new CollectionCertStoreParameters(Arrays.asList(chain)), CryptoDefs.Provider.BC.getName()); X509Certificate cert = (X509Certificate) chain[0]; //1.- Create the message we want signed MimeBodyPart dataPart = new MimeBodyPart(); dataPart.setText("Hello World!!"); //2.- Create the signed message MimeMultipart signedMulti = SignedMailExample.createMultipartWithSignature(key, cert, certsAndCRLs, dataPart); //3.- Create the body part containing the signed message MimeBodyPart signedPart = new MimeBodyPart(); signedPart.setContent(signedMulti); //4.- Set up the generator SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator(); gen.addKeyTransRecipient(cert); //5.- Generate the enveloped message MimeBodyPart envPart = gen.generate(signedPart, SMIMEEnvelopedGenerator.AES256_CBC, CryptoDefs.Provider.BC.getName()); //6.- Create the mail message MimeMessage mail = Utils.createMimeMessage("example signed and enveloped message", envPart.getContent(), envPart.getContentType()); //7.- Create the enveloped object from the mail message SMIMEEnveloped enveloped = new SMIMEEnveloped(mail); //8.- Look for our recipient identifier RecipientId recId = new KeyTransRecipientId(new X500Name(cert.getIssuerX500Principal().getName()), cert.getSerialNumber()); RecipientInformationStore recipients = enveloped.getRecipientInfos(); RecipientInformation recipient = recipients.get(recId); //9.- Decryption step MimeBodyPart res = SMIMEUtil.toMimeBodyPart(recipient.getContent(key, CryptoDefs.Provider.BC.getName())); //10.- Extract the multipart from the body part if (res.getContent() instanceof MimeMultipart) { SMIMESigned signed = new SMIMESigned((MimeMultipart) res.getContent()); //11.- Verification step X509Certificate rootCert = (X509Certificate) credentials.getCertificate(Utils.ROOT_ALIAS); if (isValid(signed, rootCert)) System.out.println("\t verification succeeded!!"); else System.out.println("\t verification failed!!"); //12.- Content display step MimeBodyPart content = signed.getContent(); System.out.print("\t Content: "); System.out.println(content.getContent()); } else System.out.println("\t wrong content found!!"); }
From source file:gov.nih.nci.cacis.nav.SendEncryptedMail.java
License:BSD License
private MimeMessage encryptMessage(MimeMessage message, Session session, Certificate cert) throws NoSuchAlgorithmException, NoSuchProviderException, SMIMEException, MessagingException, IOException {/*from w w w .j a v a2 s .c o m*/ /* Create the encrypter */ final SMIMEEnvelopedGenerator encrypter = new SMIMEEnvelopedGenerator(); encrypter.addKeyTransRecipient((X509Certificate) cert); /* Encrypt the message */ final MimeBodyPart encryptedPart = encrypter.generate(message, SMIMEEnvelopedGenerator.AES256_CBC, PROVIDER_TYPE); /* * Create a new MimeMessage that contains the encrypted and signed content */ final ByteArrayOutputStream out = new ByteArrayOutputStream(); encryptedPart.writeTo(out); final MimeMessage encryptedMessage = new MimeMessage(session, new ByteArrayInputStream(out.toByteArray())); /* Set all original MIME headers in the encrypted message */ final Enumeration headers = message.getAllHeaderLines(); while (headers.hasMoreElements()) { final String headerLine = (String) headers.nextElement(); /* * Make sure not to override any content-* headers from the original message */ if (!Strings.toLowerCase(headerLine).startsWith("content-")) { encryptedMessage.addHeaderLine(headerLine); } } return encryptedMessage; }
From source file:io.aos.crypto.spl09.EnvelopedMailExample.java
License:Apache License
public static void main(String args[]) throws Exception { KeyStore credentials = Utils.createCredentials(); PrivateKey key = (PrivateKey) credentials.getKey(Utils.END_ENTITY_ALIAS, Utils.KEY_PASSWD); Certificate[] chain = credentials.getCertificateChain(Utils.END_ENTITY_ALIAS); X509Certificate cert = (X509Certificate) chain[0]; // create the message we want encrypted MimeBodyPart dataPart = new MimeBodyPart(); dataPart.setText("Hello world!"); // set up the generator SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator(); gen.addKeyTransRecipient(cert);/*from w ww . j a v a 2 s . com*/ // generate the enveloped message MimeBodyPart envPart = gen.generate(dataPart, SMIMEEnvelopedGenerator.AES256_CBC, "BC"); // create the mail message MimeMessage mail = Utils.createMimeMessage("example enveloped message", envPart.getContent(), envPart.getContentType()); // create the enveloped object from the mail message SMIMEEnveloped enveloped = new SMIMEEnveloped(mail); // look for our recipient identifier RecipientId recId = new KEKRecipientId(null); recId.setSerialNumber(cert.getSerialNumber()); recId.setIssuer(cert.getIssuerX500Principal().getEncoded()); RecipientInformationStore recipients = enveloped.getRecipientInfos(); RecipientInformation recipient = recipients.get(recId); if (recipient != null) { // decryption step MimeBodyPart recoveredPart = SMIMEUtil.toMimeBodyPart(recipient.getContent(key, "BC")); // content display step System.out.print("Content: "); System.out.println(recoveredPart.getContent()); } else { System.out.println("could not find a matching recipient"); } }
From source file:io.aos.crypto.spl09.EnvelopedSignedMailExample.java
License:Apache License
public static void main(String[] args) throws Exception { KeyStore credentials = Utils.createCredentials(); PrivateKey key = (PrivateKey) credentials.getKey(Utils.END_ENTITY_ALIAS, Utils.KEY_PASSWD); Certificate[] chain = credentials.getCertificateChain(Utils.END_ENTITY_ALIAS); CertStore certsAndCRLs = CertStore.getInstance("Collection", new CollectionCertStoreParameters(Arrays.asList(chain)), "BC"); X509Certificate cert = (X509Certificate) chain[0]; // create the message we want signed MimeBodyPart dataPart = new MimeBodyPart(); dataPart.setText("Hello world!"); // create the signed message MimeMultipart signedMultipart = SignedMailExample.createMultipartWithSignature(key, cert, certsAndCRLs, dataPart);/*from ww w .ja va 2 s . c om*/ // create the body part containing the signed message MimeBodyPart signedPart = new MimeBodyPart(); signedPart.setContent(signedMultipart); // set up the enveloped message generator SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator(); gen.addKeyTransRecipient(cert); // generate the enveloped message MimeBodyPart envPart = gen.generate(signedPart, SMIMEEnvelopedGenerator.AES256_CBC, "BC"); // create the mail message MimeMessage mail = Utils.createMimeMessage("example signed and enveloped message", envPart.getContent(), envPart.getContentType()); // create the enveloped object from the mail message SMIMEEnveloped enveloped = new SMIMEEnveloped(mail); // look for our recipient identifier RecipientId recId = new KEKRecipientId(null); recId.setSerialNumber(cert.getSerialNumber()); recId.setIssuer(cert.getIssuerX500Principal().getEncoded()); RecipientInformationStore recipients = enveloped.getRecipientInfos(); RecipientInformation recipient = recipients.get(recId); // decryption step MimeBodyPart res = SMIMEUtil.toMimeBodyPart(recipient.getContent(key, "BC")); // extract the multi-part from the body part. if (res.getContent() instanceof MimeMultipart) { SMIMESigned signed = new SMIMESigned((MimeMultipart) res.getContent()); // verification step X509Certificate rootCert = (X509Certificate) credentials.getCertificate(Utils.ROOT_ALIAS); if (isValid(signed, rootCert)) { System.out.println("verification succeeded"); } else { System.out.println("verification failed"); } // content display step MimeBodyPart content = signed.getContent(); System.out.print("Content: "); System.out.println(content.getContent()); } else { System.out.println("wrong content found"); } }
From source file:krypto.KryptoService.java
License:Apache License
/** * Verschlsselt eine E-Mail unter Bercksichtigung der angegebenen Konfiguration. * @param mail/*from w w w .java 2 s .c o m*/ * @param config * @param cert * @return */ public static String encryptMail(Mail mail, Config config, X509Certificate cert, String pword) { try { SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator(); gen.addKeyTransRecipient(cert); Properties props = new Properties(); props.put("mail.smtp.host", "mail.java-tutor.com"); Session session = Session.getDefaultInstance(props); MimeMessage body = new MimeMessage(session); // set Text MimeBodyPart msg = new MimeBodyPart(); msg.setText(Utils.toString(mail.getText())); // set from Address fromUser = new InternetAddress(mail.getAbsender()); body.setFrom(fromUser); // set to Address toUser = new InternetAddress(mail.getEmpfaenger()); body.setRecipient(Message.RecipientType.TO, toUser); // set subject body.setSubject(mail.getBetreff()); // set send date body.setSentDate(mail.getAbsendeDatum()); // select algorithm String algorithm = SMIMEEnvelopedGenerator.AES256_CBC; if (config.getEncrAlg() == 1) algorithm = SMIMEEnvelopedGenerator.DES_EDE3_CBC; // apply smime MimeBodyPart mp = gen.generate(msg, algorithm, "BC"); body.setContent(mp.getContent(), mp.getContentType()); body.saveChanges(); ByteArrayOutputStream out = new ByteArrayOutputStream(); body.writeTo(out); return MailService.removeMessageId(out.toString(Charset.defaultCharset().name())); } catch (MessagingException e) { MessageBox box = new MessageBox(new Shell(), SWT.OK | SWT.ICON_ERROR); box.setText(ITexte.ERR_ENCRYPT.getText(config.getSprache())); box.setMessage(ITexte.ERR_ENCRYPT_MAIL.getText(config.getSprache())); box.open(); } catch (NoSuchAlgorithmException e) { MessageBox box = new MessageBox(new Shell(), SWT.OK | SWT.ICON_ERROR); box.setText(ITexte.ERR_ENCRYPT.getText(config.getSprache())); box.setMessage(ITexte.ERR_ENCRYPT_ALGORITHM.getText(config.getSprache())); box.open(); } catch (NoSuchProviderException e) { MessageBox box = new MessageBox(new Shell(), SWT.OK | SWT.ICON_ERROR); box.setText(ITexte.ERR_ENCRYPT.getText(config.getSprache())); box.setMessage(ITexte.ERR_ENCRYPT_PROVIDER.getText(config.getSprache())); box.open(); } catch (SMIMEException e) { MessageBox box = new MessageBox(new Shell(), SWT.OK | SWT.ICON_ERROR); box.setText(ITexte.ERR_ENCRYPT.getText(config.getSprache())); box.setMessage(ITexte.ERR_ENCRYPT_SMIME.getText(config.getSprache())); box.open(); } catch (Exception e) { MessageBox box = new MessageBox(new Shell(), SWT.OK | SWT.ICON_ERROR); box.setText(ITexte.ERR_ENCRYPT.getText(config.getSprache())); box.setMessage(ITexte.ERR_ENCRYPT_JCE.getText(config.getSprache())); box.open(); } return null; }