KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Title: Oyster Project
3  * Description: S/MIME email transport 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.EnvelopedSMIME;
12 import org.enhydra.oyster.exception.SMIMEException;
13
14 /**
15  * Tests enveloping process. Encrypted text/plain message with or
16  * withouth attachments can be sent by this test. To get help for this
17  * example type: "java org.enhydra.oyster.test.TestEncrypt" in command line.
18  * 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;attachment&gt;]<BR>
23  * <BR>
24  * &lt;algorithmName&gt; could be: RC240, RC264, RC2128, 3DES or 3DES<BR>
25  * <BR>
26  * Note that for this example email address "FROM" is fixed to: "sender@together.at".
27  * You should change this values in source code of TestEncrypt.java in order to use
28  * them with other "FROM" address.
29  */

30 public class TestEncrypt
31 {
32
33   public static void main (String JavaDoc[] args)
34   {
35
36     String JavaDoc subject = "S/MIME encrypted message - Subject test: ";
37     String JavaDoc content = "S/MIME encrypted message example\r\nContent test: !";
38     String JavaDoc from = "sender@together.at";
39
40     if (args.length < 4)
41     {
42       System.err.println(
43       System.getProperty("line.separator") +
44       "Usage of TestEncrypt: " +
45       System.getProperty("line.separator") +
46       "java TestEncrypt <mailHost> <mailAddress> <cerFileName> " +
47       "<algorithmName> [<attachment>]" +
48       System.getProperty("line.separator") +
49       System.getProperty("line.separator") +
50       "Examples:" +
51       System.getProperty("line.separator") +
52       "java TestEncrypt together.at recipient@together.at " +
53       "recipient512.cer RC240 " +
54       System.getProperty("line.separator") +
55       "java TestEncrypt together.at recipient@together.at " +
56       "recipient512.cer DES .\\test\\Zip8Test.zip");
57       System.exit(-1);
58     }
59     String JavaDoc smtpHost = args[0];
60     String JavaDoc addressTO = args[1];
61     String JavaDoc cerFileName = args[2];
62     String JavaDoc algorithmName = args[3];
63     String JavaDoc fileName = null;
64     if (args.length > 4)
65       fileName = args[4];
66
67     String JavaDoc addressCC = "recipient@together.at";
68     String JavaDoc addressBCC = "recipient@together.at";
69
70     subject = algorithmName + " " + cerFileName + " " + subject;
71
72     EnvelopedSMIME es = null;
73
74     try
75     {
76 // Construction of enveloped smime object
77
es = new EnvelopedSMIME(smtpHost, from, subject, content, "ISO-8859-1");
78
79       if (fileName!=null) {
80         es.addAttachment(fileName); // optional - use this if send attachment
81
}
82
83       es.setReply(from); // optional
84

85       es.addRecipient(addressTO, "TO", cerFileName); // mandatory
86
// es.addRecipient(addressCC, "CC", cerFileName); // optional
87
// es.addRecipient(addressBCC, "BCC", cerFileName); // optional
88

89       if (algorithmName.equals("RC240"))
90       {
91         System.out.println("Creating the encrypted message with RC2_CBC - 40 bits algorithm... ");
92         es.enveloping(); // instead of this next line could be used
93
// es.enveloping(es.RC2_CBC, 40);
94
}
95       else if (algorithmName.equals("RC264"))
96       {
97         System.out.println("Creating the encrypted message with RC2_CBC - 64 bits algorithm... ");
98         es.enveloping(es.RC2_CBC, 64); // send message with RC2 - 64 bits algorithm
99
}
100       else if (algorithmName.equals("RC2128"))
101       {
102         System.out.println("Creating the encrypted message with RC2_CBC - 128 bits algorithm... ");
103         es.enveloping(es.RC2_CBC, 128); // send message with RC2 - 128 bits algorithm
104
}
105       else if (algorithmName.equals("DES"))
106       {
107         System.out.println("Creating the encrypted message with DES algorithm... ");
108         es.enveloping(es.DES, 56); // send message with DES algorithm
109
}
110       else if (algorithmName.equals("3DES"))
111       {
112         System.out.println("Creating the encrypted message with DES_EDE3_CBC algorithm... ");
113         es.enveloping(es.DES_EDE3_CBC, 192); // send message with 3DES algorithm
114
}
115       System.out.print("Sending encrypted message ... ");
116       es.send(); // instead of this next line could be used
117
// Transport.send(es.getSignedMessage());
118
System.out.println("done.");
119
120     }
121     catch (Exception JavaDoc e) {
122       SMIMEException.setErrorFilePath("Log"); //specifies directory for logging
123
if(e instanceof SMIMEException) {
124         SMIMEException eTmp = (SMIMEException)e;
125 // eTmp.loggingErrors(null); //logging
126
eTmp.displayErrors(null);
127        e = eTmp.getNonSMIMEException();
128        if(e != null)
129          e.printStackTrace();
130       }
131       else {
132         e.printStackTrace();
133       }
134     }
135   }
136
137 }
138
139
140
141
Popular Tags