KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Name: CryptTest.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 package net.sourceforge.jcetaglib.test;
22
23 import junit.framework.Assert;
24 import junit.framework.TestCase;
25 import net.sourceforge.jcetaglib.lib.Crypt;
26
27 import java.io.*;
28
29 /**
30  * A collection of blockcipher encryption/decryption tests
31  * <P>
32  * These tests can be run using JUnit (http://www.junit.org)
33  *
34  * @author Gert Van Ham
35  * @author hamgert@users.sourceforge.net
36  * @author http://jcetaglib.sourceforge.net
37  * @version $Id: CryptTest.java,v 1.1 2003/12/28 19:06:57 hamgert Exp $
38  */

39 public class CryptTest extends TestCase {
40     private StringBuffer JavaDoc ciphertext = null;
41     private StringBuffer JavaDoc plaintext = null;
42
43     /**
44      * setup test
45      *
46      * @throws java.io.IOException
47      */

48     protected void setUp() throws IOException {
49         // create text file
50
FileOutputStream outStr = new FileOutputStream(RunTest.TEMPFOLDER + "readable.txt");
51         DataOutputStream dataStr = new DataOutputStream(outStr);
52
53         dataStr.writeBytes("This is a readable string inside a file");
54
55         dataStr.flush();
56         dataStr.close();
57
58         outStr.close();
59     }
60
61     /**
62      * test block cipher encryption
63      *
64      * @throws Exception
65      */

66     public void testBlockCipher() throws Exception JavaDoc {
67         for (int i = 0; i < RunTest.alg.length; i++) {
68             for (int j = 0; j < RunTest.modes.length; j++) {
69                 for (int k = 0; k < RunTest.padding.length; k++) {
70                     for (int l = 0; l < RunTest.text.length; l++) {
71                         if (RunTest.padding[k].equalsIgnoreCase("WithCTS") && (RunTest.modes[j].equalsIgnoreCase("OFB8") || RunTest.modes[j].equalsIgnoreCase("CFB8"))) {
72                             // skip, CTS can only accept ECB or CBC block ciphers
73
} else {
74                             String JavaDoc keyfile = RunTest.TEMPFOLDER + RunTest.alg[i][0] + "_" + RunTest.alg[i][1] + ".key";
75                             ciphertext = Crypt.encrypt(RunTest.text[l], keyfile, new StringBuffer JavaDoc("password"), RunTest.alg[i][0], RunTest.modes[j], RunTest.padding[k], null);
76                             plaintext = Crypt.decrypt(ciphertext, keyfile, new StringBuffer JavaDoc("password"), RunTest.alg[i][0], RunTest.modes[j], RunTest.padding[k]);
77
78                             Assert.assertEquals(plaintext.toString(), RunTest.text[l].toString());
79                         }
80                     }
81                 }
82             }
83         }
84     }
85
86     /**
87      * test file block cipher encryption
88      *
89      * @throws Exception
90      */

91     public void testFileBlockCipher() throws Exception JavaDoc {
92         for (int i = 0; i < RunTest.alg.length; i++) {
93             for (int j = 0; j < RunTest.modes.length; j++) {
94                 for (int k = 0; k < RunTest.padding.length; k++) {
95                     if (RunTest.padding[k].equalsIgnoreCase("WithCTS") && (RunTest.modes[j].equalsIgnoreCase("OFB8") || RunTest.modes[j].equalsIgnoreCase("CFB8"))) {
96                         // skip, CTS can only accept ECB or CBC block ciphers
97
} else {
98                         String JavaDoc keyfile = RunTest.TEMPFOLDER + RunTest.alg[i][0] + "_" + RunTest.alg[i][1] + ".key";
99                         Crypt.encryptFile(RunTest.TEMPFOLDER + "readable.txt", RunTest.TEMPFOLDER + "readable.txt.encrypted", keyfile, new StringBuffer JavaDoc("password"), RunTest.alg[i][0], RunTest.modes[j], RunTest.padding[k], null);
100                         Crypt.decryptFile(RunTest.TEMPFOLDER + "readable.txt.encrypted", RunTest.TEMPFOLDER + "readable.txt.decrypted", keyfile, new StringBuffer JavaDoc("password"), RunTest.alg[i][0], RunTest.modes[j], RunTest.padding[k]);
101
102                         // read the file
103
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(RunTest.TEMPFOLDER + "readable.txt.decrypted")));
104
105                         StringBuffer JavaDoc line = new StringBuffer JavaDoc();
106                         int c;
107
108                         while ((c = reader.read()) != -1) {
109                             line.append((char) c);
110                         }
111
112                         reader.close();
113
114                         String JavaDoc t = line.toString();
115
116                         Assert.assertEquals("This is a readable string inside a file", t);
117                     }
118                 }
119             }
120         }
121     }
122 }
123
Popular Tags