/picketbox-4.0.6.final/security-jboss-sx/jbosssx/src/test/java/org/jboss/test/TestJCE.java

# · Java · 143 lines · 107 code · 10 blank · 26 comment · 6 complexity · 6176a012b450a596cff6eb5dc7a24943 MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2005, JBoss Inc., and individual contributors as indicated
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.test;
  23. import java.math.BigInteger;
  24. import java.security.AlgorithmParameters;
  25. import java.security.Provider;
  26. import java.security.SecureRandom;
  27. import java.security.Security;
  28. import java.util.Iterator;
  29. import javax.crypto.Cipher;
  30. import javax.crypto.KeyGenerator;
  31. import javax.crypto.SealedObject;
  32. import javax.crypto.SecretKey;
  33. import javax.crypto.spec.SecretKeySpec;
  34. /** Tests of the Java Cryptography Extension framework
  35. @author Scott.Stark@jboss.org
  36. @version $Revision: 47 $
  37. */
  38. public class TestJCE
  39. {
  40. static void showProviders() throws Exception
  41. {
  42. Provider[] providers = Security.getProviders();
  43. for(int p = 0; p < providers.length; p ++)
  44. {
  45. Iterator<Object> iter = providers[p].keySet().iterator();
  46. System.out.println("Provider: "+providers[p].getInfo());
  47. while( iter.hasNext() )
  48. {
  49. String key = (String) iter.next();
  50. System.out.println(" key="+key+", value="+providers[p].getProperty(key));
  51. }
  52. }
  53. }
  54. static void testBlowfish() throws Exception
  55. {
  56. KeyGenerator kgen = KeyGenerator.getInstance("Blowfish");
  57. Cipher cipher = Cipher.getInstance("Blowfish");
  58. SecretKey key = null;
  59. int minKeyBits = -1, maxKeyBits = 0;
  60. int minCipherBits = -1, maxCipherBits = 0;
  61. for(int size = 1; size <= 448/8; size ++)
  62. {
  63. int bits = size * 8;
  64. try
  65. {
  66. kgen.init(bits);
  67. key = kgen.generateKey();
  68. if( minKeyBits == -1 )
  69. minKeyBits = bits;
  70. maxKeyBits = bits;
  71. }
  72. catch(Exception e)
  73. {
  74. }
  75. try
  76. {
  77. cipher.init(Cipher.ENCRYPT_MODE, key);
  78. if( minCipherBits == -1 )
  79. minCipherBits = bits;
  80. maxCipherBits = bits;
  81. }
  82. catch(Exception e)
  83. {
  84. }
  85. }
  86. System.out.println("Key range: "+minKeyBits+".."+maxKeyBits);
  87. System.out.println("Cipher range: "+minCipherBits+".."+maxCipherBits);
  88. }
  89. static void testKey() throws Exception
  90. {
  91. int size = 8 * 24;
  92. KeyGenerator kgen = KeyGenerator.getInstance("Blowfish");
  93. kgen.init(size);
  94. SecretKey key = kgen.generateKey();
  95. byte[] kbytes = key.getEncoded();
  96. System.out.println("key.Algorithm = "+key.getAlgorithm());
  97. System.out.println("key.Format = "+key.getFormat());
  98. System.out.println("key.Encoded Size = "+kbytes.length);
  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. SecretKeySpec serverKey = new SecretKeySpec(kbytes, "Blowfish");
  105. Cipher scipher = Cipher.getInstance("Blowfish");
  106. scipher.init(Cipher.DECRYPT_MODE, serverKey);
  107. String theMsg = (String) msg.getObject(scipher);
  108. System.out.println("Decrypted: "+theMsg);
  109. SecureRandom rnd = SecureRandom.getInstance("SHA1PRNG");
  110. BigInteger bi = new BigInteger(320, rnd);
  111. byte[] k2bytes = bi.toByteArray();
  112. SecretKeySpec keySpec = new SecretKeySpec(k2bytes, "Blowfish");
  113. System.out.println("key2.Algorithm = "+key.getAlgorithm());
  114. System.out.println("key2.Format = "+key.getFormat());
  115. System.out.println("key2.Encoded Size = "+kbytes.length);
  116. System.out.println("keySpec.Algorithm = " + keySpec.getAlgorithm());
  117. System.out.println("keySpec.Format = " + keySpec.getFormat());
  118. }
  119. public static void main(String[] args)
  120. {
  121. try
  122. {
  123. System.setOut(System.err);
  124. TestJCE.showProviders();
  125. //tst.testKey();
  126. TestJCE.testBlowfish();
  127. }
  128. catch(Throwable t)
  129. {
  130. t.printStackTrace();
  131. }
  132. }
  133. }