1 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 39 public class CryptTest extends TestCase { 40 private StringBuffer ciphertext = null; 41 private StringBuffer plaintext = null; 42 43 48 protected void setUp() throws IOException { 49 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 66 public void testBlockCipher() throws Exception { 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 } else { 74 String keyfile = RunTest.TEMPFOLDER + RunTest.alg[i][0] + "_" + RunTest.alg[i][1] + ".key"; 75 ciphertext = Crypt.encrypt(RunTest.text[l], keyfile, new StringBuffer ("password"), RunTest.alg[i][0], RunTest.modes[j], RunTest.padding[k], null); 76 plaintext = Crypt.decrypt(ciphertext, keyfile, new StringBuffer ("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 91 public void testFileBlockCipher() throws Exception { 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 } else { 98 String 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 ("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 ("password"), RunTest.alg[i][0], RunTest.modes[j], RunTest.padding[k]); 101 102 BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(RunTest.TEMPFOLDER + "readable.txt.decrypted"))); 104 105 StringBuffer line = new StringBuffer (); 106 int c; 107 108 while ((c = reader.read()) != -1) { 109 line.append((char) c); 110 } 111 112 reader.close(); 113 114 String 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 |