KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > TestJCE


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.test;
8
9 import java.io.Serializable JavaDoc;
10 import java.math.BigInteger JavaDoc;
11 import java.security.AlgorithmParameters JavaDoc;
12 import java.security.Key JavaDoc;
13 import java.security.KeyException JavaDoc;
14 import java.security.MessageDigest JavaDoc;
15 import java.security.Provider JavaDoc;
16 import java.security.SecureRandom JavaDoc;
17 import java.security.Security JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.lang.reflect.Constructor JavaDoc;
20 import javax.crypto.Cipher;
21 import javax.crypto.KeyGenerator;
22 import javax.crypto.SealedObject;
23 import javax.crypto.SecretKey;
24 import javax.crypto.spec.SecretKeySpec;
25
26 /** Tests of the Java Cryptography Extension framework
27  @author Scott.Stark@jboss.org
28  @version $Revision: 1.7.6.2 $
29 */

30 public class TestJCE
31 {
32    static void showProviders() throws Exception JavaDoc
33    {
34       Provider JavaDoc[] providers = Security.getProviders();
35       for(int p = 0; p < providers.length; p ++)
36       {
37          Iterator JavaDoc iter = providers[p].keySet().iterator();
38          System.out.println("Provider: "+providers[p].getInfo());
39          while( iter.hasNext() )
40          {
41             String JavaDoc key = (String JavaDoc) iter.next();
42             System.out.println(" key="+key+", value="+providers[p].getProperty(key));
43          }
44       }
45    }
46
47    static void testBlowfish() throws Exception JavaDoc
48    {
49       KeyGenerator kgen = KeyGenerator.getInstance("Blowfish");
50       Cipher cipher = Cipher.getInstance("Blowfish");
51       SecretKey key = null;
52       int minKeyBits = -1, maxKeyBits = 0;
53       int minCipherBits = -1, maxCipherBits = 0;
54       for(int size = 1; size <= 448/8; size ++)
55       {
56          int bits = size * 8;
57          try
58          {
59             kgen.init(bits);
60             key = kgen.generateKey();
61             if( minKeyBits == -1 )
62                minKeyBits = bits;
63             maxKeyBits = bits;
64          }
65          catch(Exception JavaDoc e)
66          {
67             System.out.println("Failed to create key with bits="+bits);
68             e.printStackTrace();
69             continue;
70          }
71
72          try
73          {
74             cipher.init(Cipher.ENCRYPT_MODE, key);
75             if( minCipherBits == -1 )
76                minCipherBits = bits;
77             maxCipherBits = bits;
78          }
79          catch(Exception JavaDoc e)
80          {
81             e.printStackTrace();
82          }
83       }
84       System.out.println("Key range: "+minKeyBits+".."+maxKeyBits);
85       System.out.println("Cipher range: "+minCipherBits+".."+maxCipherBits);
86    }
87
88    static void testKey() throws Exception JavaDoc
89    {
90       int size = 8 * 24;
91       KeyGenerator kgen = KeyGenerator.getInstance("Blowfish");
92       kgen.init(size);
93       SecretKey key = kgen.generateKey();
94       byte[] kbytes = key.getEncoded();
95       System.out.println("key.Algorithm = "+key.getAlgorithm());
96       System.out.println("key.Format = "+key.getFormat());
97       System.out.println("key.Encoded Size = "+kbytes.length);
98       
99       Cipher cipher = Cipher.getInstance("Blowfish");
100       AlgorithmParameters JavaDoc params = cipher.getParameters();
101       System.out.println("Blowfish.params = "+params);
102       cipher.init(Cipher.ENCRYPT_MODE, key);
103       SealedObject msg = new SealedObject("This is a secret", cipher);
104       
105       SecretKeySpec serverKey = new SecretKeySpec(kbytes, "Blowfish");
106       Cipher scipher = Cipher.getInstance("Blowfish");
107       scipher.init(Cipher.DECRYPT_MODE, serverKey);
108       String JavaDoc theMsg = (String JavaDoc) msg.getObject(scipher);
109       System.out.println("Decrypted: "+theMsg);
110       
111       SecureRandom JavaDoc rnd = SecureRandom.getInstance("SHA1PRNG");
112       BigInteger JavaDoc bi = new BigInteger JavaDoc(320, rnd);
113       byte[] k2bytes = bi.toByteArray();
114       SecretKeySpec keySpec = new SecretKeySpec(k2bytes, "Blowfish");
115       System.out.println("key2.Algorithm = "+key.getAlgorithm());
116       System.out.println("key2.Format = "+key.getFormat());
117       System.out.println("key2.Encoded Size = "+kbytes.length);
118    }
119
120    static void testKey2() throws Exception JavaDoc
121    {
122       byte[] key = new byte[40];
123       for(int n = 0; n < 40; n ++)
124          key[n] = (byte) (n+100);
125       String JavaDoc cipherAlgorithm = "Blowfish";
126       Class JavaDoc[] signature = {key.getClass(), String JavaDoc.class};
127       Object JavaDoc[] args = {key, cipherAlgorithm};
128       Object JavaDoc secretKey = null;
129       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
130       Class JavaDoc secretKeySpecClass = loader.loadClass("javax.crypto.spec.SecretKeySpec");
131       Constructor JavaDoc ctor = secretKeySpecClass.getDeclaredConstructor(signature);
132       secretKey = ctor.newInstance(args);
133       System.out.println("SecretKey: "+secretKey);
134    }
135    public static void main(String JavaDoc[] args)
136    {
137       try
138       {
139          System.setOut(System.err);
140          TestJCE tst = new TestJCE();
141          //tst.showProviders();
142
tst.testKey2();
143          //tst.testBlowfish();
144
}
145       catch(Throwable JavaDoc t)
146       {
147          t.printStackTrace();
148       }
149    }
150 }
151
Popular Tags