1 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 41 42 public class CryptTest extends TestCase { 43 private StringBuffer text1; 44 private StringBuffer text2; 45 private StringBuffer text3; 46 private StringBuffer text4; 47 private StringBuffer text5; 48 private StringBuffer text6; 49 private StringBuffer text7; 50 private StringBuffer text8; 51 private StringBuffer text9; 52 private StringBuffer text10; 53 54 StringBuffer ciphertext = null; 55 StringBuffer plaintext = null; 56 57 62 protected void setUp() throws IOException { 63 text1= new StringBuffer ("The Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms"); 64 text2= new StringBuffer ("This software is distributed under a license based on the MIT X Consortium license"); 65 text3= new StringBuffer ("found in $JAVA_HOME/jre/lib/security/java.security, where $JAVA_HOME is the location of your JDK/JRE distribution"); 66 text4= new StringBuffer ("Mit Project 2002 zum erfolgreichen Projektmanagement Damit Sie in Zukunft Ihre Projekte präzise und komfortabel steuern können"); 67 text5= new StringBuffer ("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 ("Lees de productinformatie en ontdek alles over de krachtige tools binnen Visual Studio .NET"); 69 text7= new StringBuffer ("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 ("An implementation of ECIES (stream mode) as described in IEEE P 1363a."); 71 text9= new StringBuffer ("This makes the entire keystore resistant to tampering and inspection, and forces verification"); 72 text10= new StringBuffer ("application/pkcs7-signature;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.pkcs7_signature"); 73 74 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 91 public void testEncryption() throws Exception { 92 Key.generatekey(RunTest.TEMPFOLDER + "tempkey.key",new StringBuffer ("password")); 94 95 SafeObject k = new SafeObject(); 96 k = Key.loadkey(RunTest.TEMPFOLDER + "tempkey.key",new StringBuffer ("password")); 97 98 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 147 public void testFileEncryption() throws Exception { 148 SafeObject k = new SafeObject(); 149 k = Key.loadkey(RunTest.TEMPFOLDER + "tempkey.key",new StringBuffer ("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 BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("c:/temp/readable.txt.decrypted"))); 156 157 StringBuffer line = new StringBuffer (); 159 int c; 160 161 while ((c = reader.read()) != -1) { 162 line.append((char) c); 164 } 165 166 reader.close(); 167 168 String t = line.toString(); 169 170 Assert.assertEquals("This is a readable string inside a file", t); 171 } 172 } 173 | Popular Tags |