KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > lightcrypto > test > CryptTest


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

20
21 package net.sourceforge.lightcrypto.test;
22
23 import junit.framework.Assert;
24 import junit.framework.TestCase;
25 import net.sourceforge.lightcrypto.Crypt;
26 import net.sourceforge.lightcrypto.Key;
27 import net.sourceforge.lightcrypto.SafeObject;
28
29 import java.io.*;
30
31 /**
32  * A collection of encryption tests
33  * <P>
34  * These tests can be run using JUnit (http://www.junit.org)
35  *
36  * @author Gert Van Ham
37  * @author hamgert@users.sourceforge.net
38  * @author http://jcetaglib.sourceforge.net
39  * @version $Id: CryptTest.java,v 1.2 2003/10/05 11:41:29 hamgert Exp $
40  */

41
42 public class CryptTest extends TestCase {
43     private StringBuffer JavaDoc text1;
44     private StringBuffer JavaDoc text2;
45     private StringBuffer JavaDoc text3;
46     private StringBuffer JavaDoc text4;
47     private StringBuffer JavaDoc text5;
48     private StringBuffer JavaDoc text6;
49     private StringBuffer JavaDoc text7;
50     private StringBuffer JavaDoc text8;
51     private StringBuffer JavaDoc text9;
52     private StringBuffer JavaDoc text10;
53
54     StringBuffer JavaDoc ciphertext = null;
55     StringBuffer JavaDoc plaintext = null;
56
57     /**
58      * setup test
59      *
60      * @throws IOException
61      */

62     protected void setUp() throws IOException {
63         text1= new StringBuffer JavaDoc("The Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms");
64         text2= new StringBuffer JavaDoc("This software is distributed under a license based on the MIT X Consortium license");
65         text3= new StringBuffer JavaDoc("found in $JAVA_HOME/jre/lib/security/java.security, where $JAVA_HOME is the location of your JDK/JRE distribution");
66         text4= new StringBuffer JavaDoc("Mit Project 2002 zum erfolgreichen Projektmanagement Damit Sie in Zukunft Ihre Projekte präzise und komfortabel steuern können");
67         text5= new StringBuffer JavaDoc("En av de största nyheterna är att det finns en .NET Enterprise Server-lösning för stora företagsomspännade projekt");
68         text6= new StringBuffer JavaDoc("Lees de productinformatie en ontdek alles over de krachtige tools binnen Visual Studio .NET");
69         text7= new StringBuffer JavaDoc("Vergeet even die oude tovenaars met puntige hoeden en rondborstige jonkvrouwen in nood... oké, vergeet in ieder geval even die tovenaars, want Lionheart komt met een ambitieuze rollenspelvariant");
70         text8= new StringBuffer JavaDoc("An implementation of ECIES (stream mode) as described in IEEE P 1363a.");
71         text9= new StringBuffer JavaDoc("This makes the entire keystore resistant to tampering and inspection, and forces verification");
72         text10= new StringBuffer JavaDoc("application/pkcs7-signature;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.pkcs7_signature");
73
74         // create text file
75
FileOutputStream outStr = new FileOutputStream(RunTest.TEMPFOLDER + "readable.txt");
76         DataOutputStream dataStr = new DataOutputStream(outStr);
77
78         dataStr.writeBytes("This is a readable string inside a file");
79
80         dataStr.flush();
81         dataStr.close();
82
83         outStr.close();
84     }
85
86     /**
87      * test encryption
88      *
89      * @throws Exception
90      */

91     public void testEncryption() throws Exception JavaDoc {
92         // generate a key
93
Key.generatekey(RunTest.TEMPFOLDER + "tempkey.key",new StringBuffer JavaDoc("password"));
94
95         SafeObject k = new SafeObject();
96         k = Key.loadkey(RunTest.TEMPFOLDER + "tempkey.key",new StringBuffer JavaDoc("password"));
97
98         // encrypt & decrypt
99
ciphertext = Crypt.encrypt(text1,k);
100         plaintext = Crypt.decrypt(ciphertext,k);
101
102         Assert.assertEquals(text1.toString(), plaintext.toString());
103
104         ciphertext = Crypt.encrypt(text2,k);
105         plaintext = Crypt.decrypt(ciphertext,k);
106         Assert.assertEquals(text2.toString(), plaintext.toString());
107
108         ciphertext = Crypt.encrypt(text3,k);
109         plaintext = Crypt.decrypt(ciphertext,k);
110         Assert.assertEquals(text3.toString(), plaintext.toString());
111
112         ciphertext = Crypt.encrypt(text4,k);
113         plaintext = Crypt.decrypt(ciphertext,k);
114         Assert.assertEquals(text4.toString(), plaintext.toString());
115
116         ciphertext = Crypt.encrypt(text5,k);
117         plaintext = Crypt.decrypt(ciphertext,k);
118         Assert.assertEquals(text5.toString(), plaintext.toString());
119
120         ciphertext = Crypt.encrypt(text6,k);
121         plaintext = Crypt.decrypt(ciphertext,k);
122         Assert.assertEquals(text6.toString(), plaintext.toString());
123
124         ciphertext = Crypt.encrypt(text7,k);
125         plaintext = Crypt.decrypt(ciphertext,k);
126         Assert.assertEquals(text7.toString(), plaintext.toString());
127
128         ciphertext = Crypt.encrypt(text8,k);
129         plaintext = Crypt.decrypt(ciphertext,k);
130         Assert.assertEquals(text8.toString(), plaintext.toString());
131
132         ciphertext = Crypt.encrypt(text9,k);
133         plaintext = Crypt.decrypt(ciphertext,k);
134         Assert.assertEquals(text9.toString(), plaintext.toString());
135
136         ciphertext = Crypt.encrypt(text10,k);
137         plaintext = Crypt.decrypt(ciphertext,k);
138         Assert.assertEquals(text10.toString(), plaintext.toString());
139
140     }
141
142    /**
143     * test file encryption
144     *
145     * @throws Exception
146     */

147    public void testFileEncryption() throws Exception JavaDoc {
148         SafeObject k = new SafeObject();
149         k = Key.loadkey(RunTest.TEMPFOLDER + "tempkey.key",new StringBuffer JavaDoc("password"));
150
151         Crypt.encryptFile(RunTest.TEMPFOLDER + "readable.txt",RunTest.TEMPFOLDER + "readable.txt.encrypted",k);
152         Crypt.decryptFile(RunTest.TEMPFOLDER + "readable.txt.encrypted",RunTest.TEMPFOLDER + "readable.txt.decrypted",k);
153
154         // read the file
155
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("c:/temp/readable.txt.decrypted")));
156
157         //int n = 0; // number of valid read characters
158
StringBuffer JavaDoc line = new StringBuffer JavaDoc();
159         int c;
160
161         while ((c = reader.read()) != -1) {
162             //n++;
163
line.append((char) c);
164         }
165
166         reader.close();
167
168         String JavaDoc t = line.toString();
169
170         Assert.assertEquals("This is a readable string inside a file", t);
171     }
172 }
173
Popular Tags