1 /*
2 * Title: S/MIME Project
3 * Description: S/MIME email sending capabilities
4 * @Author Vladan Obradovic
5 * @Version 2.0.1
6 */
7
8 package org.webdocwf.util.smime.test;
9
10
11 import javax.mail.Transport;
12 import org.webdocwf.util.smime.smime.SignedAndEnvelopedSMIME;
13 import org.webdocwf.util.smime.exception.SMIMEException;
14
15
16 /***
17 * Tests enveloping and signing process. Enveloped and signed
18 * text/plain message with or withouth attachments can be sent by this test.
19 * To get help for this example type: "java org.webdocwf.util.smime.test.TestEncSig"
20 * in command line. It is assumed that smime200tests.jar is in your classpath.<BR>
21 * <BR>
22 * Parameters passed to example are:<BR>
23 * <mailHost> <mailAddress> <cerFileName> <algorithmName>
24 * <digestAlgorithm> <includingCert> <includingSignAttrib>
25 * <pfxFileName> [<attachment>] <BR>
26 * <BR>
27 * <digestAlgorithm> could be: SHA1_WITH_RSA, MD2_WITH_RSA, MD5_WITH_RSA or SHA1_WITH_DSA.<BR>
28 * <includingCert> could be: true/false<BR>
29 * <includingSignAttrib> could be: true/false<BR>
30 * <algorithmName> could be: RC240, RC264, RC2128, 3DES or 3DES<BR>
31 * <BR>
32 * Note that for this example's passwords for .pfx or .p12 files are fixed to
33 * "sea1". All .pfx files or .p12 files provided with this example have this
34 * password. Also, email address "FROM" is fixed to: "sender@seateam.co.yu".
35 * You should change this values in source code of TestEncSig.java in order to use
36 * them with other .pfx or .p12 files and corresponding "FROM" addresses and passwords.
37 */
38 public class TestEncSig {
39
40 public static void main(String[] args) {
41
42 String subject = "S/MIME enveloped and signed message - Subject test: ÜüÄäÖöÜüß";
43 String content = "S/MIME enveloped and signed message example\r\nContent test: ÜüÄäÖöÜüß!";
44 String from = "sender@seateam.co.yu";
45 String password = "sea1";
46
47 if (args.length < 8) {
48 System.err.println(
49 System.getProperty("line.separator") +
50 "Usage of TestEncSig: " +
51 System.getProperty("line.separator") +
52 "java TestEncSig <mailHost> <mailAddress> <cerFileName> " +
53 "<algorithmName> <digestAlgorithm> <includingCert> <includingSignAttrib> " +
54 "<pfxFileName> [<attachment>]" +
55 System.getProperty("line.separator") +
56 System.getProperty("line.separator") +
57 "Examples:" +
58 System.getProperty("line.separator") +
59 "java TestEncSig seateam.co.yu recipient@seateam.co.yu recipient512.cer " +
60 "RC240 SHA1_WITH_RSA true true sender512.pfx" +
61 System.getProperty("line.separator") +
62 "java TestEncSig seateam.co.yu recipient@seateam.co.yu recipient512.cer " +
63 "DES MD5_WITH_RSA true true sender512.pfx .//test//Zip8Test.zip");
64
65 System.exit(-1);
66 }
67
68 String smtpHost = args[0];
69 String addressTO = args[1];
70 String cerFileName = args[2];
71 String algorithmName = args[3];
72 String digestAlgorithm = args[4];
73
74 boolean includingCert = true;
75
76 if (args[5].equals("true"))
77 includingCert = true;
78 else
79 includingCert = false;
80
81 boolean includingSignAttrib = true;
82
83 if (args[6].equals("true"))
84 includingSignAttrib = true;
85 else
86 includingSignAttrib = false;
87
88 String pfxfileName = args[7];
89
90 String fileName = null;
91
92 if (args.length > 8)
93 fileName = args[8];
94
95 String addressCC = "recipient@seateam.co.yu";
96 String addressBCC = "recipient@seateam.co.yu";
97
98 subject = args[2] + " " + args[3] + " " + args[4] + " " + args[5] + " " +
99 args[6] + " " + args[7] + " " + subject;
100
101 SignedAndEnvelopedSMIME ess = null;
102
103 try {
104 ess = new SignedAndEnvelopedSMIME(smtpHost, from, subject, content);
105
106 if (fileName != null) {
107 ess.addAttachment(fileName); // optional - use this if send attachment
108 }
109
110 ess.setReply(from); // optional
111
112 ess.addRecipient(addressTO, "TO", cerFileName); // mandatory
113 // ess.addRecipient(addressCC, "CC", cerFileName); // optional
114 // ess.addRecipient(addressBCC, "BCC", cerFileName); // optional
115
116 ess.setCapabilities("SYMMETRIC", 5, 3, 1, 4, 2); // optional
117 ess.setCapabilities("ENCIPHER", 1, 0, 0, 0, 0); // optional
118 ess.setCapabilities("SIGNATURE", 3, 2, 1, 4, 0); // optional
119
120 ess.addSigner(pfxfileName, password, digestAlgorithm, includingCert, includingSignAttrib);
121
122 if (algorithmName.equals("RC240")) {
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("RC2_CBC", 40, "ENCRYPT_FIRST");
126 } else if (algorithmName.equals("RC264")) {
127 System.out.println("Creating enveloped and signed message with RC2 - 64 bits algorithm... ");
128 ess.signingAndEnveloping("RC2_CBC", 64, "ENCRYPT_FIRST"); // send message with RC2 - 64 bits algorithm
129 } else if (algorithmName.equals("RC2128")) {
130 System.out.println("Creating enveloped and signed message with RC2 - 128 bits algorithm... ");
131 ess.signingAndEnveloping("RC2_CBC", 128, "ENCRYPT_FIRST"); // send message with RC2 - 128 bits algorithm
132 } else if (algorithmName.equals("DES")) {
133 System.out.println("Creating enveloped and signed message with DES algorithm... ");
134 ess.signingAndEnveloping("DES", 56, "ENCRYPT_FIRST"); // send message with DES - 56 bits algorithm
135 } else if (algorithmName.equals("3DES")) {
136 System.out.println("Creating enveloped and signed message with 3DES algorithm... ");
137 ess.signingAndEnveloping("DES_EDE3_CBC", 192, "ENCRYPT_FIRST"); // send message with 3DES - 192 bits algorithm
138 }
139
140 System.out.print("Sending enveloped and signed message ... ");
141 ess.send(); // instead of this next line could be used
142 // Transport.send(ess.getSignedMessage());
143 System.out.println("done.");
144
145 } catch (Exception e) {
146 SMIMEException.setErrorFilePath("Log"); //specifies directory for logging
147 if (e instanceof SMIMEException) {
148 // ((SMIMEException)e).loggingErrors(null); //logging
149 ((SMIMEException) e).displayErrors(null);
150 } else
151 System.out.println(e.getMessage());
152 }
153 }
154 }
This page was automatically generated by Maven