KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Title: Oyster Project
3  * Description: S/MIME email sending capabilities
4  * @Author Vladan Obradovic
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
16  * text/plain message with or withouth attachments can be sent by this test.
17  * To get help for this example type: "java org.enhydra.oyster.test.TestEncSig"
18  * in command line. It is assumed that oyster_tests.jar is in your classpath.<BR>
19  * <BR>
20  * Parameters passed to example are:<BR>
21  * &lt;mailHost&gt; &lt;mailAddress&gt; &lt;cerFileName&gt; &lt;algorithmName&gt;
22  * &lt;digestAlgorithm&gt; &lt;includingCert&gt; &lt;includingSignAttrib&gt;
23  * &lt;pfxFileName&gt; [&lt;attachment&gt;] <BR>
24  * <BR>
25  * &lt;digestAlgorithm&gt; could be: SHA1_WITH_RSA, MD2_WITH_RSA, MD5_WITH_RSA or SHA1_WITH_DSA.<BR>
26  * &lt;includingCert&gt; could be: true/false<BR>
27  * &lt;includingSignAttrib&gt; could be: true/false<BR>
28  * &lt;algorithmName&gt; could be: RC240, RC264, RC2128, 3DES or 3DES<BR>
29  * <BR>
30  * Note that for this example's passwords for .pfx or .p12 files are fixed to
31  * "together". All .pfx files or .p12 files provided with this example have this
32  * password. Also, email address "FROM" is fixed to: "sender@together.at".
33  * You should change this values in source code of TestEncSig.java in order to use
34  * them with other .pfx or .p12 files and corresponding "FROM" addresses and passwords.
35  */

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

111       ess.addRecipient(addressTO, "TO", cerFileName); // mandatory
112
// ess.addRecipient(addressCC, "CC", cerFileName); // optional
113
// ess.addRecipient(addressBCC, "BCC", cerFileName); // optional
114

115       ess.setCapabilities("SYMMETRIC", 5, 3, 1, 4, 2); // optional
116
ess.setCapabilities("ENCIPHER", 1, 0, 0, 0, 0); // optional
117
ess.setCapabilities("SIGNATURE", 3, 2, 1, 4, 0); // optional
118

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