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