KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Name: net.sourceforge.lightcrypto.test.PBETest
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.TestCase;
24 import junit.framework.Assert;
25
26 import java.io.*;
27
28 import net.sourceforge.lightcrypto.PBECrypt;
29
30 /**
31  * A collection of PBE encryption tests
32  * <P>
33  * These tests can be run using JUnit (http://www.junit.org)
34  *
35  * @author Gert Van Ham
36  * @author hamgert@users.sourceforge.net
37  * @author http://jcetaglib.sourceforge.net
38  * @version $Id: PBETest.java,v 1.1 2003/10/28 20:12:54 hamgert Exp $
39  */

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

61     protected void setUp() throws IOException {
62         text1 = new StringBuffer JavaDoc("The Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms");
63         text2 = new StringBuffer JavaDoc("This software is distributed under a license based on the MIT X Consortium license");
64         text3 = new StringBuffer JavaDoc("found in $JAVA_HOME/jre/lib/security/java.security, where $JAVA_HOME is the location of your JDK/JRE distribution");
65         text4 = new StringBuffer JavaDoc("Mit Project 2002 zum erfolgreichen Projektmanagement Damit Sie in Zukunft Ihre Projekte präzise und komfortabel steuern können");
66         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");
67         text6 = new StringBuffer JavaDoc("Lees de productinformatie en ontdek alles over de krachtige tools binnen Visual Studio .NET");
68         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");
69         text8 = new StringBuffer JavaDoc("An implementation of ECIES (stream mode) as described in IEEE P 1363a.");
70         text9 = new StringBuffer JavaDoc("This makes the entire keystore resistant to tampering and inspection, and forces verification");
71         text10 = new StringBuffer JavaDoc("application/pkcs7-signature;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.pkcs7_signature");
72
73         // create text file
74
FileOutputStream outStr = new FileOutputStream(RunTest.TEMPFOLDER + "readable.txt");
75         DataOutputStream dataStr = new DataOutputStream(outStr);
76
77         dataStr.writeBytes("This is a readable string inside a file");
78
79         dataStr.flush();
80         dataStr.close();
81
82         outStr.close();
83     }
84
85     /**
86      * test encryption
87      *
88      * @throws Exception
89      */

90     public void testEncryption() throws Exception JavaDoc {
91         StringBuffer JavaDoc p = null;
92         p = new StringBuffer JavaDoc("mypassword");
93
94         // encrypt & decrypt
95
ciphertext = PBECrypt.encrypt(text1, p);
96         plaintext = PBECrypt.decrypt(ciphertext, p);
97         Assert.assertEquals(text1.toString(), plaintext.toString());
98
99         ciphertext = PBECrypt.encrypt(text2, p);
100         plaintext = PBECrypt.decrypt(ciphertext, p);
101         Assert.assertEquals(text2.toString(), plaintext.toString());
102
103         ciphertext = PBECrypt.encrypt(text3, p);
104         plaintext = PBECrypt.decrypt(ciphertext, p);
105         Assert.assertEquals(text3.toString(), plaintext.toString());
106
107         ciphertext = PBECrypt.encrypt(text4, p);
108         plaintext = PBECrypt.decrypt(ciphertext, p);
109         Assert.assertEquals(text4.toString(), plaintext.toString());
110
111         ciphertext = PBECrypt.encrypt(text5, p);
112         plaintext = PBECrypt.decrypt(ciphertext, p);
113         Assert.assertEquals(text5.toString(), plaintext.toString());
114
115         ciphertext = PBECrypt.encrypt(text6, p);
116         plaintext = PBECrypt.decrypt(ciphertext, p);
117         Assert.assertEquals(text6.toString(), plaintext.toString());
118
119         ciphertext = PBECrypt.encrypt(text7, p);
120         plaintext = PBECrypt.decrypt(ciphertext, p);
121         Assert.assertEquals(text7.toString(), plaintext.toString());
122
123         ciphertext = PBECrypt.encrypt(text8, p);
124         plaintext = PBECrypt.decrypt(ciphertext, p);
125         Assert.assertEquals(text8.toString(), plaintext.toString());
126
127         ciphertext = PBECrypt.encrypt(text9, p);
128         plaintext = PBECrypt.decrypt(ciphertext, p);
129         Assert.assertEquals(text9.toString(), plaintext.toString());
130
131         ciphertext = PBECrypt.encrypt(text10, p);
132         plaintext = PBECrypt.decrypt(ciphertext, p);
133         Assert.assertEquals(text10.toString(), plaintext.toString());
134
135     }
136
137     /**
138      * test file encryption
139      *
140      * @throws Exception
141      */

142     public void testFileEncryption() throws Exception JavaDoc {
143         StringBuffer JavaDoc p = new StringBuffer JavaDoc("passphrase");
144
145         PBECrypt.encryptFile(RunTest.TEMPFOLDER + "readable.txt", RunTest.TEMPFOLDER + "readable.txt.encrypted", p);
146         PBECrypt.decryptFile(RunTest.TEMPFOLDER + "readable.txt.encrypted", RunTest.TEMPFOLDER + "readable.txt.decrypted", p);
147
148         // read the file
149
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("c:/temp/readable.txt.decrypted")));
150
151         //int n = 0; // number of valid read characters
152
StringBuffer JavaDoc line = new StringBuffer JavaDoc();
153         int c;
154
155         while ((c = reader.read()) != -1) {
156             //n++;
157
line.append((char) c);
158         }
159
160         reader.close();
161
162         String JavaDoc t = line.toString();
163
164         Assert.assertEquals("This is a readable string inside a file", t);
165     }
166 }
167
Popular Tags