1 21 22 package net.sourceforge.jcetaglib.test; 23 24 import junit.framework.Assert; 25 import junit.framework.TestCase; 26 import net.sourceforge.jcetaglib.lib.Hybrid; 27 import net.sourceforge.jcetaglib.lib.X509Cert; 28 import net.sourceforge.jcetaglib.tools.SignerCertificate; 29 30 import java.io.*; 31 import java.security.PrivateKey ; 32 import java.security.cert.X509Certificate ; 33 34 44 public class HybridTest extends TestCase { 45 private StringBuffer ciphertext = null; 46 private StringBuffer plaintext = null; 47 48 53 protected void setUp() throws IOException { 54 FileOutputStream outStr = new FileOutputStream(RunTest.TEMPFOLDER + "readable.txt"); 56 DataOutputStream dataStr = new DataOutputStream(outStr); 57 58 dataStr.writeBytes("This is a readable string inside a file"); 59 60 dataStr.flush(); 61 dataStr.close(); 62 63 outStr.close(); 64 } 65 66 71 public void testEncryptWithHMAC() throws Exception { 72 X509Certificate alicecert = X509Cert.getCertificateFromP12(RunTest.TEMPFOLDER + "alice.p12", "alice", new StringBuffer ("password")); 74 75 PrivateKey aliceprivate = X509Cert.getPrivateFromP12(RunTest.TEMPFOLDER + "alice.p12", "alice", new StringBuffer ("password")); 77 78 for (int i = 0; i < RunTest.text.length; i++) { 79 ciphertext = Hybrid.encryptWithHMAC(RunTest.text[i] 80 , alicecert.getPublicKey() 81 , "AES" 82 , null 83 , 128 84 , "CBC" 85 , "PKCS7Padding"); 86 87 plaintext = Hybrid.decryptAndVerifyHMAC(ciphertext 88 , aliceprivate 89 , "AES" 90 , "CBC" 91 , "PKCS7Padding"); 92 93 Assert.assertEquals(RunTest.text[i].toString(), plaintext.toString()); 94 } 95 } 96 97 102 public void testEncryptFileWithHMAC() throws Exception { 103 X509Certificate bobcert = X509Cert.getCertificateFromP12(RunTest.TEMPFOLDER + "bob.p12", "bob", new StringBuffer ("password")); 105 106 PrivateKey bobprivate = X509Cert.getPrivateFromP12(RunTest.TEMPFOLDER + "bob.p12", "bob", new StringBuffer ("password")); 108 109 Hybrid.encryptFileWithHMAC(RunTest.TEMPFOLDER + "readable.txt" 110 , RunTest.TEMPFOLDER + "readable.txt.encrypted" 111 , bobcert.getPublicKey() 112 , "AES" 113 , null 114 , 128 115 , "CBC" 116 , "PKCS7Padding"); 117 118 Hybrid.decryptFileAndVerifyHMAC(RunTest.TEMPFOLDER + "readable.txt.encrypted" 119 , RunTest.TEMPFOLDER + "readable.txt.decrypted" 120 , bobprivate 121 , "AES" 122 , "CBC" 123 , "PKCS7Padding"); 124 125 BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(RunTest.TEMPFOLDER + "readable.txt.decrypted"))); 127 128 StringBuffer line = new StringBuffer (); 129 int c; 130 131 while ((c = reader.read()) != -1) { 132 line.append((char) c); 133 } 134 135 reader.close(); 136 137 String t = line.toString(); 138 139 Assert.assertEquals("This is a readable string inside a file", t); 140 141 } 142 143 148 public void testEncryptAndSign() throws Exception { 149 X509Certificate alicecert = X509Cert.getCertificateFromP12(RunTest.TEMPFOLDER + "alice.p12", "alice", new StringBuffer ("password")); 151 152 PrivateKey aliceprivate = X509Cert.getPrivateFromP12(RunTest.TEMPFOLDER + "alice.p12", "alice", new StringBuffer ("password")); 154 155 X509Certificate bobcert = X509Cert.getCertificateFromP12(RunTest.TEMPFOLDER + "bob.p12", "bob", new StringBuffer ("password")); 157 158 PrivateKey bobprivate = X509Cert.getPrivateFromP12(RunTest.TEMPFOLDER + "bob.p12", "bob", new StringBuffer ("password")); 160 161 for (int i = 0; i < RunTest.text.length; i++) { 162 ciphertext = Hybrid.encryptAndSign(RunTest.text[i] 163 , alicecert.getPublicKey() 164 , bobprivate 165 , bobcert 166 , "MD5WithRSA" 167 , "AES" 168 , null 169 , 128 170 , "CBC" 171 , "PKCS7Padding"); 172 173 SignerCertificate s = new SignerCertificate(); 174 plaintext = Hybrid.decryptAndVerify(ciphertext 175 , aliceprivate 176 , s 177 , "MD5WithRSA" 178 , "AES" 179 , "CBC" 180 , "PKCS7Padding"); 181 182 Assert.assertEquals(RunTest.text[i].toString(), plaintext.toString()); 183 Assert.assertEquals(s.getCert().getSubjectDN().toString(), "EMAILADDRESS=bob@sourceforge.net, CN=Bob, OU=Sourceforge, O=NET, C=BE"); 184 185 } 186 } 187 188 193 public void testEncryptFileAndSign() throws Exception { 194 X509Certificate alicecert = X509Cert.getCertificateFromP12(RunTest.TEMPFOLDER + "alice.p12", "alice", new StringBuffer ("password")); 196 197 PrivateKey aliceprivate = X509Cert.getPrivateFromP12(RunTest.TEMPFOLDER + "alice.p12", "alice", new StringBuffer ("password")); 199 200 X509Certificate bobcert = X509Cert.getCertificateFromP12(RunTest.TEMPFOLDER + "bob.p12", "bob", new StringBuffer ("password")); 202 203 PrivateKey bobprivate = X509Cert.getPrivateFromP12(RunTest.TEMPFOLDER + "bob.p12", "bob", new StringBuffer ("password")); 205 206 Hybrid.encryptFileAndSign(RunTest.TEMPFOLDER + "readable.txt" 207 , RunTest.TEMPFOLDER + "readable.txt.encrypted" 208 , alicecert.getPublicKey() 209 , bobprivate 210 , bobcert 211 , "MD5WithRSA" 212 , "AES" 213 , null 214 , 256 215 , "CBC" 216 , "PKCS7Padding"); 217 218 SignerCertificate s = new SignerCertificate(); 219 220 Hybrid.decryptFileAndVerify(RunTest.TEMPFOLDER + "readable.txt.encrypted" 221 , RunTest.TEMPFOLDER + "readable.txt.decrypted" 222 , aliceprivate 223 , s 224 , "MD5WithRSA" 225 , "AES" 226 , "CBC" 227 , "PKCS7Padding"); 228 229 BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(RunTest.TEMPFOLDER + "readable.txt.decrypted"))); 231 232 StringBuffer line = new StringBuffer (); 233 int c; 234 235 while ((c = reader.read()) != -1) { 236 line.append((char) c); 237 } 238 239 reader.close(); 240 241 String t = line.toString(); 242 243 Assert.assertEquals("This is a readable string inside a file", t); 244 Assert.assertEquals(s.getCert().getSubjectDN().toString(), "EMAILADDRESS=bob@sourceforge.net, CN=Bob, OU=Sourceforge, O=NET, C=BE"); 245 246 } 247 } 248 | Popular Tags |