001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test;
023:
024: import java.io.Serializable;
025: import java.math.BigInteger;
026: import java.security.AlgorithmParameters;
027: import java.security.Key;
028: import java.security.KeyException;
029: import java.security.MessageDigest;
030: import java.security.Provider;
031: import java.security.SecureRandom;
032: import java.security.Security;
033: import java.util.Iterator;
034: import java.lang.reflect.Constructor;
035: import javax.crypto.Cipher;
036: import javax.crypto.KeyGenerator;
037: import javax.crypto.SealedObject;
038: import javax.crypto.SecretKey;
039: import javax.crypto.spec.SecretKeySpec;
040:
041: /** Tests of the Java Cryptography Extension framework
042: @author Scott.Stark@jboss.org
043: @version $Revision: 37390 $
044: */
045: public class TestJCE {
046: static void showProviders() throws Exception {
047: Provider[] providers = Security.getProviders();
048: for (int p = 0; p < providers.length; p++) {
049: Iterator iter = providers[p].keySet().iterator();
050: System.out.println("Provider: " + providers[p].getInfo());
051: while (iter.hasNext()) {
052: String key = (String) iter.next();
053: System.out.println(" key=" + key + ", value="
054: + providers[p].getProperty(key));
055: }
056: }
057: }
058:
059: static void testBlowfish() throws Exception {
060: KeyGenerator kgen = KeyGenerator.getInstance("Blowfish");
061: Cipher cipher = Cipher.getInstance("Blowfish");
062: SecretKey key = null;
063: int minKeyBits = -1, maxKeyBits = 0;
064: int minCipherBits = -1, maxCipherBits = 0;
065: for (int size = 1; size <= 448 / 8; size++) {
066: int bits = size * 8;
067: try {
068: kgen.init(bits);
069: key = kgen.generateKey();
070: if (minKeyBits == -1)
071: minKeyBits = bits;
072: maxKeyBits = bits;
073: } catch (Exception e) {
074: System.out.println("Failed to create key with bits="
075: + bits);
076: e.printStackTrace();
077: continue;
078: }
079:
080: try {
081: cipher.init(Cipher.ENCRYPT_MODE, key);
082: if (minCipherBits == -1)
083: minCipherBits = bits;
084: maxCipherBits = bits;
085: } catch (Exception e) {
086: e.printStackTrace();
087: }
088: }
089: System.out.println("Key range: " + minKeyBits + ".."
090: + maxKeyBits);
091: System.out.println("Cipher range: " + minCipherBits + ".."
092: + maxCipherBits);
093: }
094:
095: static void testKey() throws Exception {
096: int size = 8 * 24;
097: KeyGenerator kgen = KeyGenerator.getInstance("Blowfish");
098: kgen.init(size);
099: SecretKey key = kgen.generateKey();
100: byte[] kbytes = key.getEncoded();
101: System.out.println("key.Algorithm = " + key.getAlgorithm());
102: System.out.println("key.Format = " + key.getFormat());
103: System.out.println("key.Encoded Size = " + kbytes.length);
104:
105: Cipher cipher = Cipher.getInstance("Blowfish");
106: AlgorithmParameters params = cipher.getParameters();
107: System.out.println("Blowfish.params = " + params);
108: cipher.init(Cipher.ENCRYPT_MODE, key);
109: SealedObject msg = new SealedObject("This is a secret", cipher);
110:
111: SecretKeySpec serverKey = new SecretKeySpec(kbytes, "Blowfish");
112: Cipher scipher = Cipher.getInstance("Blowfish");
113: scipher.init(Cipher.DECRYPT_MODE, serverKey);
114: String theMsg = (String) msg.getObject(scipher);
115: System.out.println("Decrypted: " + theMsg);
116:
117: SecureRandom rnd = SecureRandom.getInstance("SHA1PRNG");
118: BigInteger bi = new BigInteger(320, rnd);
119: byte[] k2bytes = bi.toByteArray();
120: SecretKeySpec keySpec = new SecretKeySpec(k2bytes, "Blowfish");
121: System.out.println("key2.Algorithm = " + key.getAlgorithm());
122: System.out.println("key2.Format = " + key.getFormat());
123: System.out.println("key2.Encoded Size = " + kbytes.length);
124: }
125:
126: static void testKey2() throws Exception {
127: byte[] key = new byte[40];
128: for (int n = 0; n < 40; n++)
129: key[n] = (byte) (n + 100);
130: String cipherAlgorithm = "Blowfish";
131: Class[] signature = { key.getClass(), String.class };
132: Object[] args = { key, cipherAlgorithm };
133: Object secretKey = null;
134: ClassLoader loader = Thread.currentThread()
135: .getContextClassLoader();
136: Class secretKeySpecClass = loader
137: .loadClass("javax.crypto.spec.SecretKeySpec");
138: Constructor ctor = secretKeySpecClass
139: .getDeclaredConstructor(signature);
140: secretKey = ctor.newInstance(args);
141: System.out.println("SecretKey: " + secretKey);
142: }
143:
144: public static void main(String[] args) {
145: try {
146: System.setOut(System.err);
147: TestJCE tst = new TestJCE();
148: //tst.showProviders();
149: tst.testKey2();
150: //tst.testBlowfish();
151: } catch (Throwable t) {
152: t.printStackTrace();
153: }
154: }
155: }
|