how to create signed and encrypted mail in java using bouncycastle provider - Java

This is a discussion on how to create signed and encrypted mail in java using bouncycastle provider - Java ; Respected sir, i am creating a file for creating an encrypted mail in java.here is my code below.Sir the problem i am facing is exception in thread "main" java.security.KeystoreException pkcs12 not Found at java.security.keystore.getInstance(Unknown source) at createencryptedmail.main(CreateEncryptedMail.java 54) when i ...

+ Reply to Thread
Results 1 to 2 of 2

how to create signed and encrypted mail in java using bouncycastle provider

  1. Default how to create signed and encrypted mail in java using bouncycastle provider

    Respected sir,
    i am creating a file for creating an encrypted mail in java.here is my
    code below.Sir the problem i am facing is

    exception in thread "main" java.security.KeystoreException pkcs12 not
    Found
    at java.security.keystore.getInstance(Unknown source)
    at createencryptedmail.main(CreateEncryptedMail.java 54)

    when i am writing this
    /*KeyStore ks = KeyStore.getInstance("PKCS12");
    FileInputStream fis = new FileInputStream(args[0]);
    ks.load(fis, args[1].toCharArray());
    fis.close();
    */
    in place of
    KeyStore ks = KeyStore.getInstance("PKCS12", "BC");
    ks.load(new FileInputStream(args[0]), args[1].toCharArray());
    then the first error is replaced by the follwing error
    exception in thread main org.bouncycastle.mail.smime.smimeexception
    can't find algorithm
    at org.bouncycastle.mail.smime.SMimeEnvelopedgenerator.make(SMimeEnvelopedGenerator.java
    120)

    at org.bouncycastle.mail.smime.SMimeEnvelopedgenerator.generate(SMimeEnvelopedGenerator.java
    166)
    at createEncryptedmail.main(CreateEncryptedMail.java 126)


    finally i also set all the classpaths required for bouncycastle as
    follows
    C:\j2sdk1.4.2_01\jre\lib\rt.jar;.;D:\chandan\javamail-1.2\mail.jar;D:\chandanmail\jaf-1.0.2\activation.jar;.;C:\Documents
    and Settings\Administrator\Desktop\Encrypted mail
    downloads\bcmail-jdk14-124.jar;.;C:\j2sdk1.4.2_01\jre\lib\ext\bcprov-jdk14-124.jar

    but sir i don't know what's the problem . i am in this problem from
    last 18 days.please help me.if possible mail me a sample code for
    signedmail and signedencryptedmail so that i will remain great full to
    u.


    Your sincerely chandan kumar



    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.security.KeyStore;
    import java.security.cert.Certificate;
    import java.security.cert.X509Certificate;
    import java.util.Enumeration;
    import java.util.Properties;

    import javax.mail.Address;
    import javax.mail.Message;
    import javax.mail.Session;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.internet.MimeMessage;

    import org.bouncycastle.mail.smime.SMIMEEnvelopedGenerator;
    import java.security.*;
    import org.bouncycastle.jce.provider.*;

    /**
    * a simple example that creates a single encrypted mail message.
    * <p>
    * The key store can be created using the class in
    * org.bouncycastle.jce.examples.PKCS12Example - the program expects
    only one
    * key to be present in the key file.
    * <p>
    * Note: while this means that both the private key is available to
    * the program, the private key is retrieved from the keystore only
    for
    * the purposes of locating the corresponding public key, in normal
    circumstances
    * you would only be doing this with a certificate available.
    */
    public class CreateEncryptedMail
    {
    public static void main(
    String args[])
    throws Exception
    {
    if (args.length != 2)
    {
    System.err.println("usage: CreateEncryptedMail
    pkcs12Keystore password");
    System.exit(0);
    }
    if (java.security.Security.getProvider("BC") == null) {
    System.out.println("Can't find BC - adding");
    java.security.Security.addProvider(new
    org.bouncycastle.jce.provider.BouncyCastleProvider());
    }


    //
    // Open the key store
    //
    KeyStore ks = KeyStore.getInstance("PKCS12","BC");

    ks.load(new FileInputStream(args[0]), args[1].toCharArray());

    /*KeyStore ks = KeyStore.getInstance("PKCS12");
    FileInputStream fis = new FileInputStream(args[0]);
    ks.load(fis, args[1].toCharArray());
    fis.close();
    */
    Enumeration e = ks.aliases();
    String keyAlias = null;

    while (e.hasMoreElements())
    {
    String alias = (String)e.nextElement();

    if (ks.isKeyEntry(alias))
    {
    keyAlias = alias;
    }
    }

    if (keyAlias == null)
    {
    System.err.println("can't find a private key!");
    System.exit(0);
    }

    Certificate[] chain = ks.getCertificateChain(keyAlias);

    //
    // create the generator for creating an smime/encrypted
    message
    //
    SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator();

    gen.addKeyTransRecipient((X509Certificate)chain[0]);

    //
    // create a subject key id - this has to be done the same way
    as
    // it is done in the certificate associated with the private
    key
    // version 3 only.
    //
    /*
    MessageDigest dig =
    MessageDigest.getInstance("SHA1", "BC");

    dig.update(cert.getPublicKey().getEncoded());

    gen.addKeyTransRecipient(cert.getPublicKey(), dig.digest());
    */

    //
    // create the base for our message
    //
    MimeBodyPart msg = new MimeBodyPart();

    msg.setText("Hello world!");

    //java.security.Security.insertProviderAt(new
    org.bouncycastle.jce.provider.BouncyCastleProvider(), 6);

    //MimeBodyPart mp = gen.generate(msg,SMIMEEnvelopedGenerator.DES_EDE3_CBC,"BC");
    // MimeBodyPart mp = gen.generate(msg,
    SMIMEEnvelopedGenerator.RC2_CBC, "BC");


    // MimeBodyPart mp = gen.generate(msg,
    SMIMEEnvelopedGenerator.IDEA_CBC, "BC");
    // MimeBodyPart mp = gen.generate(msg,
    SMIMEEnvelopedGenerator.DES_EDE3_CBC, "BC");
    // MimeBodyPart mp = gen.generate(msg,
    SMIMEEnvelopedGenerator.CAST5_CBC, "BC");
    // MimeBodyPart mp = gen.generate(msg,
    SMIMEEnvelopedGenerator.AES256_CBC, "BC");
    // MimeBodyPart mp = gen.generate(msg,
    SMIMEEnvelopedGenerator.AES192_CBC, "BC");
    MimeBodyPart mp = gen.generate(msg,
    SMIMEEnvelopedGenerator.AES128_CBC, "BC");



    // Get a Session object and create the mail message
    //
    Properties props = System.getProperties();
    Session session = Session.getDefaultInstance(props, null);

    Address fromUser = new InternetAddress("\"Eric H.
    Echidna\"<eric@bouncycastle.org>");
    Address toUser = new
    InternetAddress("example@bouncycastle.org");

    MimeMessage body = new MimeMessage(session);
    body.setFrom(fromUser);
    body.setRecipient(Message.RecipientType.TO, toUser);
    body.setSubject("example encrypted message");
    body.setContent(mp.getContent(), mp.getContentType());
    body.saveChanges();

    body.writeTo(new FileOutputStream("encrypted.message"));
    }
    }

  2. Default Re: how to create signed and encrypted mail in java using bouncycastle provider

    your post helped me get my code working, thanks

    I generated my keystore using keytool in java

    I used "JKS" as the keystore type rather than "PKCS12", and I took out the "BC", and I put in my keystore location, after that it worked fine for me

+ Reply to Thread

Similar Threads

  1. how to deal with encrypted zip in JAVA?
    By Application Development in forum Java
    Replies: 5
    Last Post: 09-10-2007, 01:43 AM
  2. encrypted connection with Mail::Sender?
    By Application Development in forum Perl
    Replies: 1
    Last Post: 06-17-2007, 02:25 PM
  3. Block size of CMS Signed Data using Bouncycastle
    By Application Development in forum Java
    Replies: 0
    Last Post: 05-26-2005, 04:41 AM
  4. importing org.bouncycastle.jce.provider keys in .keystore
    By Application Development in forum Java
    Replies: 1
    Last Post: 05-04-2004, 01:10 PM
  5. Signed Applet & Signed Provider
    By Application Development in forum Java
    Replies: 0
    Last Post: 08-27-2003, 11:13 AM