KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > oyster > test > TestEncSigKeyStore


1 /*
2  * Title: Oyster Project
3  * Description: S/MIME email sending capabilities
4  * @Author Vladimir Radisic
5  * @Version 2.1.5
6  */

7
8 package org.enhydra.oyster.test;
9
10 import javax.mail.Transport JavaDoc;
11 import org.enhydra.oyster.smime.SignedAndEnvelopedSMIME;
12 import org.enhydra.oyster.exception.SMIMEException;
13
14 /**
15  * Tests enveloping and signing process. Enveloped and signed text/plain message
16  * with or withouth attachments can be sent by this test. This example is test
17  * for enveloping and signing of email message by usage of keys and certificates
18  * from optional KeyStore storage file. To get help for this example type:
19  * "java org.enhydra.oyster.test.TestEncSigKeyStore" in command line. It is
20  * assumed that oyster_tests.jar is in your classpath.<BR>
21  * <BR>
22  * Parameters passed to example are:<BR>
23  * &lt;mailHost&gt; &lt;mailAddress&gt; &lt;cerFileName&gt; &lt;algorithmName&gt;
24  * &lt;digestAlgorithm&gt; &lt;includingCert&gt; &lt;includingSignAttrib&gt;
25  * &lt;pfxFileName&gt; [&lt;attachment&gt;] <BR>
26  * <BR>
27  * &lt;digestAlgorithm&gt; could be: SHA1_WITH_RSA, MD2_WITH_RSA, MD5_WITH_RSA or SHA1_WITH_DSA.<BR>
28  * &lt;includingCert&gt; could be: true/false<BR>
29  * &lt;includingSignAttrib&gt; could be: true/false<BR>
30  * &lt;algorithmName&gt; could be: RC240, RC264, RC2128, 3DES or 3DES<BR>
31  * <BR>
32  * Note that password for KeyStore file ('keystore.ks') used in this example,
33  * is "together", and the KeyStore has type 'BKS'. You may change this values in
34  * source code of TestSignedKeyStore.java in order to use them with other
35  * KeyStore files. Also, email address "FROM" is fixed to: "sender@together.at",
36  * and should change too, in order to use them with other KeyStore file entryes.
37  * Aliases which exist in keystore.ks for corresponding private keys and
38  * certificate chains are: 'senderDSA512.pfx', 'recipient1024.pfx',
39  * 'recipient2048.pfx', 'recipient512.pfx', 'recipientDSA1024.pfx',
40  * 'recipientDSA512.pfx', 'sender1024.pfx', 'sender2048.pfx', 'sender512.pfx',
41  * 'senderDSA1024.pfx'.
42  */

43 public class TestEncSigKeyStore
44 {
45
46   public static void main(String JavaDoc[] args)
47   {
48
49     String JavaDoc subject = "S/MIME enveloped and signed message - Subject test: ÜüÄäÖöÜüß";
50     String JavaDoc content = "S/MIME enveloped and signed message example\r\nContent test: ÜüÄäÖöÜüß!";
51     String JavaDoc from = "sender@together.at";
52     String JavaDoc password = "together";
53     String JavaDoc keyStoreFile = "keystore.ks";
54
55     if (args.length < 8)
56     {
57       System.err.println(
58       System.getProperty("line.separator") +
59       "Usage of TestEncSigKeyStore: " +
60       System.getProperty("line.separator") +
61       "java TestEncSig <mailHost> <mailAddress> <cerKeyStoreAlias> " +
62       "<algorithmName> <digestAlgorithm> <includingCert> <includingSignAttrib> " +
63       "<pfxKeyStoreAlias> [<attachment>]"+
64       System.getProperty("line.separator") +
65       System.getProperty("line.separator") +
66       "Examples:" +
67       System.getProperty("line.separator") +
68       "java TestEncSigKeyStore together.at recipient@together.at recipient512.cer " +
69       "RC240 SHA1_WITH_RSA true true sender512.pfx" +
70       System.getProperty("line.separator") +
71       "java TestEncSigKeyStore together.at recipient@together.at recipient512.cer " +
72       "DES MD5_WITH_RSA true true sender512.pfx .\\test\\Zip8Test.zip");
73
74       System.exit(-1);
75     }
76
77     String JavaDoc smtpHost = args[0];
78     String JavaDoc addressTO = args[1];
79     String JavaDoc cerFileName = args[2];
80     String JavaDoc algorithmName = args[3];
81     String JavaDoc digestAlgorithm = args[4];
82
83     boolean includingCert = true;
84     if (args[5].equals("true"))
85       includingCert = true;
86     else
87       includingCert = false;
88
89     boolean includingSignAttrib = true;
90     if (args[6].equals("true"))
91       includingSignAttrib = true;
92     else
93       includingSignAttrib = false;
94
95     String JavaDoc pfxfileName = args[7];
96
97     String JavaDoc fileName = null;
98     if (args.length > 8)
99       fileName = args[8];
100
101     String JavaDoc addressCC = "recipient@together.at";
102     String JavaDoc addressBCC = "recipient@together.at";
103
104     subject = args[2] + " " + args[3] + " " + args[4] + " " + args[5] + " " +
105               args[6] + " " + args[7] + " " + subject;
106
107     SignedAndEnvelopedSMIME ess = null;
108
109     try {
110 // Construction of enveloped and signed smime object
111
ess = new SignedAndEnvelopedSMIME(smtpHost, from, subject, content, "ISO-8859-1");
112
113       if (fileName!=null) {
114         ess.addAttachment(fileName); // optional - use this if send attachment
115
}
116
117       ess.setReply(from); // optional
118

119       String JavaDoc alias = cerFileName.replaceAll(".cer", ".pfx"); // switch name to appropriate alias
120
ess.addRecipient(addressTO, "TO", keyStoreFile, ess.BKS, password, alias); // mandatory
121
// ess.addRecipient(addressTO, "CC", keyStoreFile, ess.BKS, password, cerFileName); // optional
122
// ess.addRecipient(addressTO, "BCC", keyStoreFile, ess.BKS, password, cerFileName); // optional
123

124       ess.setCapabilities("SYMMETRIC", 5, 3, 1, 4, 2); // optional
125
ess.setCapabilities("ENCIPHER", 1, 0, 0, 0, 0); // optional
126
ess.setCapabilities("SIGNATURE", 3, 2, 1, 4, 0); // optional
127

128       alias = pfxfileName; // switch name to appropriate alias
129
ess.addSigner(keyStoreFile, ess.BKS, password, alias, digestAlgorithm,
130                                 includingCert, includingSignAttrib); // mandatory
131

132       if (algorithmName.equals("RC240"))
133       {
134         System.out.println("Creating enveloped and signed message with RC2 - 40 bits algorithm... ");
135         ess.signingAndEnveloping("ENCRYPT_FIRST"); // instead of this next line could be used
136
// ess.enveloping(ess.RC2_CBC, 40, "ENCRYPT_FIRST");
137
}
138       else if (algorithmName.equals("RC264"))
139       {
140         System.out.println("Creating enveloped and signed message with RC2 - 64 bits algorithm... ");
141         ess.signingAndEnveloping(ess.RC2_CBC, 64, "ENCRYPT_FIRST"); // send message with RC2 - 64 bits algorithm
142
}
143       else if (algorithmName.equals("RC2128"))
144       {
145         System.out.println("Creating enveloped and signed message with RC2 - 128 bits algorithm... ");
146         ess.signingAndEnveloping(ess.RC2_CBC, 128, "ENCRYPT_FIRST"); // send message with RC2 - 128 bits algorithm
147
}
148       else if (algorithmName.equals("DES"))
149       {
150         System.out.println("Creating enveloped and signed message with DES algorithm... ");
151         ess.signingAndEnveloping(ess.DES, 56, "ENCRYPT_FIRST"); // send message with DES - 56 bits algorithm
152
}
153       else if (algorithmName.equals("3DES"))
154       {
155         System.out.println("Creating enveloped and signed message with 3DES algorithm... ");
156         ess.signingAndEnveloping(ess.DES_EDE3_CBC, 192, "ENCRYPT_FIRST"); // send message with 3DES - 192 bits algorithm
157
}
158
159       System.out.print("Sending enveloped and signed message ... ");
160       ess.send(); // instead of this next line could be used
161
// Transport.send(ess.getSignedMessage());
162
System.out.println("done.");
163
164     }
165     catch (Exception JavaDoc e) {
166       SMIMEException.setErrorFilePath("Log"); //specifies directory for logging
167
if(e instanceof SMIMEException) {
168         SMIMEException eTmp = (SMIMEException)e;
169 // eTmp.loggingErrors(null); //logging
170
eTmp.displayErrors(null);
171        e = eTmp.getNonSMIMEException();
172        if(e != null)
173          e.printStackTrace();
174       }
175       else {
176         e.printStackTrace();
177       }
178     }
179   }
180 }
Popular Tags