KickJava   Java API By Example, From Geeks To Geeks.

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


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

42 public class AsymmetricTest extends TestCase {
43     private StringBuffer JavaDoc ciphertext = null;
44     private StringBuffer JavaDoc plaintext = null;
45
46     /**
47      * test asymmetric encryption
48      *
49      * @throws Exception
50      */

51     public void testAsymmetric() throws Exception JavaDoc {
52         // load Alice's certificate to encrypt
53
X509Certificate JavaDoc alicecert = X509Cert.getCertificateFromP12(RunTest.TEMPFOLDER + "alice.p12", "alice", new StringBuffer JavaDoc("password"));
54
55         // load Alice's private key to decrypt
56
PrivateKey JavaDoc aliceprivate = X509Cert.getPrivateFromP12(RunTest.TEMPFOLDER + "alice.p12", "alice", new StringBuffer JavaDoc("password"));
57
58         ciphertext = Asymmetric.encrypt(new StringBuffer JavaDoc("Make sure this text is not too long or errors will be generated"), alicecert.getPublicKey());
59         plaintext = Asymmetric.decrypt(ciphertext, aliceprivate);
60
61         Assert.assertEquals(plaintext.toString(), "Make sure this text is not too long or errors will be generated");
62     }
63 }
64
Popular Tags