KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Title: Oyster Project
3  * Description: S/MIME email transport 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.EnvelopedSMIME;
12 import org.enhydra.oyster.exception.SMIMEException;
13
14 /**
15  * Tests enveloping process. Encrypted text/plain message with or withouth
16  * attachments can be sent by this test. This example is test for encrypting of
17  * email message by usage of keys and certificates from optional KeyStore
18  * storage file. To get help for this example type:
19  * "java org.enhydra.oyster.test.TestEncryptKeyStore" 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;attachment&gt;]<BR>
25  * <BR>
26  * &lt;algorithmName&gt; could be: RC240, RC264, RC2128, 3DES or 3DES<BR>
27  * <BR>
28  * Note that password for KeyStore file ('keystore.ks') used in this example,
29  * is "together", and the KeyStore has type 'BKS'. You may change this values in
30  * source code of TestSignedKeyStore.java in order to use them with other
31  * KeyStore files. Also, email address "FROM" is fixed to: "sender@together.at",
32  * and should change too, in order to use them with other KeyStore file entryes.
33  * Aliases which exist in keystore.ks for corresponding private keys and
34  * certificate chains are: 'senderDSA512.pfx', 'recipient1024.pfx',
35  * 'recipient2048.pfx', 'recipient512.pfx', 'recipientDSA1024.pfx',
36  * 'recipientDSA512.pfx', 'sender1024.pfx', 'sender2048.pfx', 'sender512.pfx',
37  * 'senderDSA1024.pfx'.
38  */

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

96       String JavaDoc alias = cerFileName.replaceAll(".cer", ".pfx"); // switch name to appropriate alias
97
es.addRecipient(addressTO, "TO", keyStoreFile, es.BKS, password, alias); // mandatory
98
// es.addRecipient(addressTO, "CC", keyStoreFile, es.BKS, password, cerFileName); // optional
99
// es.addRecipient(addressTO, "BCC", keyStoreFile, es.BKS, password, cerFileName); // optional
100

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