1 /* $Id: TestRSA_ECB_PKCS1.java,v 1.3 2001/02/26 16:21:08 gelderen Exp $
2  *
3  * Copyright (C) 1995-2000 The Cryptix Foundation Limited.
4  * All rights reserved.
5  *
6  * Use, modification, copying and distribution of this software is subject
7  * the terms and conditions of the Cryptix General Licence. You should have
8  * received a copy of the Cryptix General Licence along with this library;
9  * if not, you can download a copy from http://www.cryptix.org/ .
10  */
11 package cryptix.jce.test;
12 
13 
14 import java.io.File;
15 import java.io.FileReader;
16 import java.io.FileWriter;
17 import java.io.FileInputStream;
18 import java.io.FileOutputStream;
19 import java.security.KeyPair;
20 import java.security.KeyPair;
21 import java.security.KeyPairGenerator;
22 import java.security.interfaces.RSAPrivateKey;
23 import java.security.interfaces.RSAPublicKey;
24 import javax.crypto.KeyGenerator;
25 import javax.crypto.Cipher;
26 import javax.crypto.SecretKey;
27 import javax.crypto.CipherInputStream;
28 import javax.crypto.CipherOutputStream;
29 
30 
31 /**
32  * <B>This is a testclass for RSA/ECB/PKCS#1
33  *
34  * @author Paul Waserbrot (pw@cryptix.org)
35  */
36 final class TestRSA_ECB_PKCS1 extends Test {
37 
38     private static final String name = "RSA_ECB_PKCS1";
39     private Cipher c, d;
40     private final String s = "ThisIsASecretSecretMessageOfNull";
41 
42     private String algo;
43     private int KEY_SIZE;
44 
TestRSA_ECB_PKCS1(String algo, int KEY_SIZE)45     protected TestRSA_ECB_PKCS1 (String algo, int KEY_SIZE)
46     {
47         super(name);
48         this.algo = algo;
49         this.KEY_SIZE = KEY_SIZE;
50     }
51 
TestRSA_ECB_PKCS1()52     protected TestRSA_ECB_PKCS1 ()
53     {
54         super(name);
55         this.algo = "RSA";
56         this.KEY_SIZE = 1024;
57     }
58 
doIt()59     protected void doIt()
60     throws Exception
61     {
62         beginTest("ECB with PKCS1");
63         init();
64         passIf(test(c,d,s));
65 
66         return;
67     }
68 
init()69     private void init ()
70     throws Exception
71     {
72         this.algo = "RSA";
73         this.KEY_SIZE = 1024;
74 
75         // Generate and initilize keys and ciphers.
76         KeyPairGenerator kpg = KeyPairGenerator.getInstance(algo);
77         kpg.initialize(KEY_SIZE);
78         KeyPair kp = kpg.generateKeyPair(); // JDK 1.1 call
79         RSAPrivateKey prk = (RSAPrivateKey)kp.getPrivate();
80         RSAPublicKey puk = (RSAPublicKey)kp.getPublic();
81 
82         algo = algo + "/ECB/PKCS#1";
83         c = Cipher.getInstance(algo);
84         d = Cipher.getInstance(algo);
85 
86         c.init(Cipher.ENCRYPT_MODE, puk);
87         d.init(Cipher.DECRYPT_MODE, prk);
88         return;
89     }
90 
test(Cipher c, Cipher d, String s)91     private boolean test(Cipher c, Cipher d, String s)
92     throws Exception
93     {
94         byte [] b = s.getBytes();
95         byte [] ca = d.doFinal(c.doFinal(b));
96 
97         return (new String(ca).equals(s));
98     }
99 }
100