1 7 package org.jboss.test; 8 9 import java.io.Serializable ; 10 import java.math.BigInteger ; 11 import java.security.AlgorithmParameters ; 12 import java.security.Key ; 13 import java.security.KeyException ; 14 import java.security.MessageDigest ; 15 import java.security.Provider ; 16 import java.security.SecureRandom ; 17 import java.security.Security ; 18 import java.util.Iterator ; 19 import java.lang.reflect.Constructor ; 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 30 public class TestJCE 31 { 32 static void showProviders() throws Exception 33 { 34 Provider [] providers = Security.getProviders(); 35 for(int p = 0; p < providers.length; p ++) 36 { 37 Iterator iter = providers[p].keySet().iterator(); 38 System.out.println("Provider: "+providers[p].getInfo()); 39 while( iter.hasNext() ) 40 { 41 String key = (String ) iter.next(); 42 System.out.println(" key="+key+", value="+providers[p].getProperty(key)); 43 } 44 } 45 } 46 47 static void testBlowfish() throws Exception 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 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 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 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 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 theMsg = (String ) msg.getObject(scipher); 109 System.out.println("Decrypted: "+theMsg); 110 111 SecureRandom rnd = SecureRandom.getInstance("SHA1PRNG"); 112 BigInteger bi = new BigInteger (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 121 { 122 byte[] key = new byte[40]; 123 for(int n = 0; n < 40; n ++) 124 key[n] = (byte) (n+100); 125 String cipherAlgorithm = "Blowfish"; 126 Class [] signature = {key.getClass(), String .class}; 127 Object [] args = {key, cipherAlgorithm}; 128 Object secretKey = null; 129 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 130 Class secretKeySpecClass = loader.loadClass("javax.crypto.spec.SecretKeySpec"); 131 Constructor ctor = secretKeySpecClass.getDeclaredConstructor(signature); 132 secretKey = ctor.newInstance(args); 133 System.out.println("SecretKey: "+secretKey); 134 } 135 public static void main(String [] args) 136 { 137 try 138 { 139 System.setOut(System.err); 140 TestJCE tst = new TestJCE(); 141 tst.testKey2(); 143 } 145 catch(Throwable t) 146 { 147 t.printStackTrace(); 148 } 149 } 150 } 151 | Popular Tags |