KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > jcetaglib > test > HybridTest


1 /*
2   Name: HybridTest.java
3   Licensing: LGPL
4
5   API: Sun (http://java.sun.com) JCE 1.2.2 API (cleanroom implementation by Bouncy Castle)
6   Provider: Bouncy Castle (http://www.bouncycastle.org)
7
8   Disclaimer:
9
10   COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND,
11   EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT THE COVERED CODE
12   IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING. THE ENTIRE
13   RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED CODE IS WITH YOU. SHOULD ANY COVERED CODE
14   PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR)
15   ASSUME THE COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY
16   CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED
17   HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
18
19   (C) Copyright 2003 Gert Van Ham
20 */

21
22 package net.sourceforge.jcetaglib.test;
23
24 import junit.framework.Assert;
25 import junit.framework.TestCase;
26 import net.sourceforge.jcetaglib.lib.Hybrid;
27 import net.sourceforge.jcetaglib.lib.X509Cert;
28 import net.sourceforge.jcetaglib.tools.SignerCertificate;
29
30 import java.io.*;
31 import java.security.PrivateKey JavaDoc;
32 import java.security.cert.X509Certificate JavaDoc;
33
34 /**
35  * A collection of hybrid encryption tests
36  * <P>
37  * These tests can be run using JUnit (http://www.junit.org)
38  *
39  * @author Gert Van Ham
40  * @author hamgert@users.sourceforge.net
41  * @author http://jcetaglib.sourceforge.net
42  * @version $Id: HybridTest.java,v 1.2 2004/04/14 09:48:28 hamgert Exp $
43  */

44 public class HybridTest extends TestCase {
45     private StringBuffer JavaDoc ciphertext = null;
46     private StringBuffer JavaDoc plaintext = null;
47
48     /**
49      * setup test
50      *
51      * @throws IOException
52      */

53     protected void setUp() throws IOException {
54         // create text file
55
FileOutputStream outStr = new FileOutputStream(RunTest.TEMPFOLDER + "readable.txt");
56         DataOutputStream dataStr = new DataOutputStream(outStr);
57
58         dataStr.writeBytes("This is a readable string inside a file");
59
60         dataStr.flush();
61         dataStr.close();
62
63         outStr.close();
64     }
65
66     /**
67      * test encryption with HMAC
68      *
69      * @throws Exception
70      */

71     public void testEncryptWithHMAC() throws Exception JavaDoc {
72         // load Alice's certificate
73
X509Certificate JavaDoc alicecert = X509Cert.getCertificateFromP12(RunTest.TEMPFOLDER + "alice.p12", "alice", new StringBuffer JavaDoc("password"));
74
75         // load Alice's private key to decrypt
76
PrivateKey JavaDoc aliceprivate = X509Cert.getPrivateFromP12(RunTest.TEMPFOLDER + "alice.p12", "alice", new StringBuffer JavaDoc("password"));
77
78         for (int i = 0; i < RunTest.text.length; i++) {
79             ciphertext = Hybrid.encryptWithHMAC(RunTest.text[i]
80                     , alicecert.getPublicKey()
81                     , "AES"
82                     , null
83                     , 128
84                     , "CBC"
85                     , "PKCS7Padding");
86
87             plaintext = Hybrid.decryptAndVerifyHMAC(ciphertext
88                     , aliceprivate
89                     , "AES"
90                     , "CBC"
91                     , "PKCS7Padding");
92
93             Assert.assertEquals(RunTest.text[i].toString(), plaintext.toString());
94         }
95     }
96
97     /**
98      * test file encryption with HMAC
99      *
100      * @throws Exception
101      */

102     public void testEncryptFileWithHMAC() throws Exception JavaDoc {
103         // load Bob's's certificate
104
X509Certificate JavaDoc bobcert = X509Cert.getCertificateFromP12(RunTest.TEMPFOLDER + "bob.p12", "bob", new StringBuffer JavaDoc("password"));
105
106         // load Bob's private key to decrypt
107
PrivateKey JavaDoc bobprivate = X509Cert.getPrivateFromP12(RunTest.TEMPFOLDER + "bob.p12", "bob", new StringBuffer JavaDoc("password"));
108
109         Hybrid.encryptFileWithHMAC(RunTest.TEMPFOLDER + "readable.txt"
110                 , RunTest.TEMPFOLDER + "readable.txt.encrypted"
111                 , bobcert.getPublicKey()
112                 , "AES"
113                 , null
114                 , 128
115                 , "CBC"
116                 , "PKCS7Padding");
117
118         Hybrid.decryptFileAndVerifyHMAC(RunTest.TEMPFOLDER + "readable.txt.encrypted"
119                 , RunTest.TEMPFOLDER + "readable.txt.decrypted"
120                 , bobprivate
121                 , "AES"
122                 , "CBC"
123                 , "PKCS7Padding");
124
125         // read the file
126
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(RunTest.TEMPFOLDER + "readable.txt.decrypted")));
127
128         StringBuffer JavaDoc line = new StringBuffer JavaDoc();
129         int c;
130
131         while ((c = reader.read()) != -1) {
132             line.append((char) c);
133         }
134
135         reader.close();
136
137         String JavaDoc t = line.toString();
138
139         Assert.assertEquals("This is a readable string inside a file", t);
140
141     }
142
143     /**
144      * test encryption with signature
145      *
146      * @throws Exception
147      */

148     public void testEncryptAndSign() throws Exception JavaDoc {
149         // load Alice's certificate to encrypt (receiver)
150
X509Certificate JavaDoc alicecert = X509Cert.getCertificateFromP12(RunTest.TEMPFOLDER + "alice.p12", "alice", new StringBuffer JavaDoc("password"));
151
152         // load Alice's private key to decrypt (receiver)
153
PrivateKey JavaDoc aliceprivate = X509Cert.getPrivateFromP12(RunTest.TEMPFOLDER + "alice.p12", "alice", new StringBuffer JavaDoc("password"));
154
155         // load Bob's certificate for identification (sender)
156
X509Certificate JavaDoc bobcert = X509Cert.getCertificateFromP12(RunTest.TEMPFOLDER + "bob.p12", "bob", new StringBuffer JavaDoc("password"));
157
158         // load Bob's private key to sign (sender)
159
PrivateKey JavaDoc bobprivate = X509Cert.getPrivateFromP12(RunTest.TEMPFOLDER + "bob.p12", "bob", new StringBuffer JavaDoc("password"));
160
161         for (int i = 0; i < RunTest.text.length; i++) {
162             ciphertext = Hybrid.encryptAndSign(RunTest.text[i]
163                     , alicecert.getPublicKey()
164                     , bobprivate
165                     , bobcert
166                     , "MD5WithRSA"
167                     , "AES"
168                     , null
169                     , 128
170                     , "CBC"
171                     , "PKCS7Padding");
172
173             SignerCertificate s = new SignerCertificate();
174             plaintext = Hybrid.decryptAndVerify(ciphertext
175                     , aliceprivate
176                     , s
177                     , "MD5WithRSA"
178                     , "AES"
179                     , "CBC"
180                     , "PKCS7Padding");
181
182             Assert.assertEquals(RunTest.text[i].toString(), plaintext.toString());
183             Assert.assertEquals(s.getCert().getSubjectDN().toString(), "EMAILADDRESS=bob@sourceforge.net, CN=Bob, OU=Sourceforge, O=NET, C=BE");
184
185         }
186     }
187
188     /**
189      * test file encryption with signature
190      *
191      * @throws Exception
192      */

193     public void testEncryptFileAndSign() throws Exception JavaDoc {
194         // load Alice's certificate to encrypt (receiver)
195
X509Certificate JavaDoc alicecert = X509Cert.getCertificateFromP12(RunTest.TEMPFOLDER + "alice.p12", "alice", new StringBuffer JavaDoc("password"));
196
197         // load Alice's private key to decrypt (receiver)
198
PrivateKey JavaDoc aliceprivate = X509Cert.getPrivateFromP12(RunTest.TEMPFOLDER + "alice.p12", "alice", new StringBuffer JavaDoc("password"));
199
200         // load Bob's certificate for identification (sender)
201
X509Certificate JavaDoc bobcert = X509Cert.getCertificateFromP12(RunTest.TEMPFOLDER + "bob.p12", "bob", new StringBuffer JavaDoc("password"));
202
203         // load Bob's private key to sign (sender)
204
PrivateKey JavaDoc bobprivate = X509Cert.getPrivateFromP12(RunTest.TEMPFOLDER + "bob.p12", "bob", new StringBuffer JavaDoc("password"));
205
206         Hybrid.encryptFileAndSign(RunTest.TEMPFOLDER + "readable.txt"
207                 , RunTest.TEMPFOLDER + "readable.txt.encrypted"
208                 , alicecert.getPublicKey()
209                 , bobprivate
210                 , bobcert
211                 , "MD5WithRSA"
212                 , "AES"
213                 , null
214                 , 256
215                 , "CBC"
216                 , "PKCS7Padding");
217
218         SignerCertificate s = new SignerCertificate();
219
220         Hybrid.decryptFileAndVerify(RunTest.TEMPFOLDER + "readable.txt.encrypted"
221                 , RunTest.TEMPFOLDER + "readable.txt.decrypted"
222                 , aliceprivate
223                 , s
224                 , "MD5WithRSA"
225                 , "AES"
226                 , "CBC"
227                 , "PKCS7Padding");
228
229         // read the file
230
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(RunTest.TEMPFOLDER + "readable.txt.decrypted")));
231
232         StringBuffer JavaDoc line = new StringBuffer JavaDoc();
233         int c;
234
235         while ((c = reader.read()) != -1) {
236             line.append((char) c);
237         }
238
239         reader.close();
240
241         String JavaDoc t = line.toString();
242
243         Assert.assertEquals("This is a readable string inside a file", t);
244         Assert.assertEquals(s.getCert().getSubjectDN().toString(), "EMAILADDRESS=bob@sourceforge.net, CN=Bob, OU=Sourceforge, O=NET, C=BE");
245
246     }
247 }
248
Popular Tags