Chris Rathman / ChrisRath@aol.com

/************************************************************************
 *                                                                      *
 * javax.crypto.spec                                                    *
 *                                                                      *
 *    Classes:                                                          *
 *       DESedeKeySpec                       IvParameterSpec            *
 *       DESKeySpec                          PBEKeySpec                 *
 *       DHGenParameterSpec                  PBEParameterSpec           *
 *       DHParameterSpec                     RC2ParameterSpec           *
 *       DHPrivateKeySpec                    RC5ParameterSpec           *
 *       DHPublicKeySpec                     SecretKeySpec              *
 *                                                                      *
 ************************************************************************/
package Test.Chris;
import javax.crypto.spec.*;

public class Javax_crypto_spec {

   public static void main(String[] args) {
      Javax_crypto_spec obj = new Javax_crypto_spec();
      obj.exercise();
      System.exit(0);
   }

   public void exercise() {
      desedekeyspec();
      deskeyspec();
      dhgenparameterspec();
      dhparameterspec();
      dhprivatekeyspec();
      dhpublickeyspec();
      ivparameterspec();
      pbekeyspec();
      pbeparameterspec();
      rc2parameterspec();
      rc5parameterspec();
      secretkeyspec();
   }

   /*********************************************************************
    *                                                                   *
    * DESedeKeySpec:                                                    *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Fields:                                                        *
    *       DES_EDE_KEY_LEN                                             *
    *                                                                   *
    *    Methods:                                                       *
    *       getKey               isParityAdjusted                       *
    *                                                                   *
    *********************************************************************/
   void desedekeyspec() {
      String s;
      byte[] bx = new byte[40];
      DESedeKeySpec ks;
      javax.crypto.SecretKey k;
      javax.crypto.SecretKeyFactory kf;


      try {
         ks = new javax.crypto.spec.DESedeKeySpec(bx, 0);
         kf = javax.crypto.SecretKeyFactory.getInstance("DESede");
         k = kf.generateSecret(ks);
         ks = (DESedeKeySpec)kf.getKeySpec(k, DESedeKeySpec.class);

      } catch(java.security.NoSuchAlgorithmException e) {
         System.out.println(e);
      } catch(java.security.InvalidKeyException e) {
         System.out.println(e);
      } catch(java.security.spec.InvalidKeySpecException e) {
         System.out.println(e);
      }
   /* TO BE DETERMINED
      static int DES_EDE_KEY_LEN

      byte[] getKey() // Returns the DES-EDE key.
      static boolean isParityAdjusted(byte[] key, int offset) // Checks if the given DES-EDE key, starting at offset inclusive, is parity-adjusted.
   */
   }

   /*********************************************************************
    *                                                                   *
    * DESKeySpec:                                                       *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Fields:                                                        *
    *       DES_KEY_LEN                                                 *
    *                                                                   *
    *    Methods:                                                       *
    *       getKey               isParityAdjusted     isWeak            *
    *                                                                   *
    *********************************************************************/
   void deskeyspec() {
      String s;
      byte[] bx = new byte[20];
      DESKeySpec ks;
      javax.crypto.SecretKey k;
      javax.crypto.SecretKeyFactory kf;


      try {
         ks = new javax.crypto.spec.DESKeySpec(bx, 0);
         kf = javax.crypto.SecretKeyFactory.getInstance("DES");
         k = kf.generateSecret(ks);
         ks = (DESKeySpec)kf.getKeySpec(k, DESKeySpec.class);

      } catch(java.security.NoSuchAlgorithmException e) {
         System.out.println(e);
      } catch(java.security.InvalidKeyException e) {
         System.out.println(e);
      } catch(java.security.spec.InvalidKeySpecException e) {
         System.out.println(e);
      }

   /* TO BE DETERMINED
      static int DES_KEY_LEN

      byte[] getKey() // Returns the DES key material.
      static boolean isParityAdjusted(byte[] key, int offset) // Checks if the given DES key material, starting at offset inclusive, is parity-adjusted.
      static boolean isWeak(byte[] key, int offset) // Checks if the given DES key material is weak or semi-weak.
   */
   }

   /*********************************************************************
    *                                                                   *
    * DHGenParameterSpec:                                               *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Methods:                                                       *
    *       getExponentSize      getPrimeSize                           *
    *                                                                   *
    *********************************************************************/
   void dhgenparameterspec() {
   /* TO BE DETERMINED
      int getExponentSize() // Returns the size in bits of the random exponent (private value).
      int getPrimeSize() // Returns the size in bits of the prime modulus.
   */
   }

   /*********************************************************************
    *                                                                   *
    * DHParameterSpec:                                                  *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Methods:                                                       *
    *       getG                 getL                 getP              *
    *                                                                   *
    *********************************************************************/
   void dhparameterspec() {
      String skip1024String =
         "F488FD584E49DBCD20B49DE49107366B336C380D451D0F7C88B31C7C5B2D8EF6" +
         "F3C923C043F0A55B188D8EBB558CB85D38D334FD7C175743A31D186CDE33212C" +
         "B52AFF3CE1B1294018118D7C84A70A72D686C40319C807297ACA950CD9969FAB" +
         "D00A509B0246D3083D66A45D419F9C7CBD894B221926BAABA25EC355E92F78C7";
      java.math.BigInteger skip1024Modulus = new java.math.BigInteger(skip1024String, 16);
      java.math.BigInteger skip1024Base = java.math.BigInteger.valueOf(2);
      java.math.BigInteger g;
      int l;
      java.math.BigInteger p;

      DHParameterSpec dhp = new DHParameterSpec(skip1024Modulus, skip1024Base);
      g = dhp.getG();                   // base generator g
System.out.println(g);
      l = dhp.getL();                   // size in bits, l, of the random exponent
System.out.println(l);
      p = dhp.getP();                   // prime modulus p
System.out.println(p);
   }

   /*********************************************************************
    *                                                                   *
    * DHPrivateKeySpec:                                                 *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Methods:                                                       *
    *       getG                 getP                 getX              *
    *                                                                   *
    *********************************************************************/
   void dhprivatekeyspec() {
   /* TO BE DETERMINED
      java.math.BigInteger getG() // Returns the base generator g.
      java.math.BigInteger getP() // Returns the prime modulus p.
      java.math.BigInteger getX() // Returns the private value x.
   */
   }

   /*********************************************************************
    *                                                                   *
    * DHPublicKeySpec:                                                  *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Methods:                                                       *
    *       getG                 getP                 getY              *
    *                                                                   *
    *********************************************************************/
   void dhpublickeyspec() {
   /* TO BE DETERMINED
      java.math.BigInteger getG() // Returns the base generator g.
      java.math.BigInteger getP() // Returns the prime modulus p.
      java.math.BigInteger getY() // Returns the public value y.
   */
   }

   /*********************************************************************
    *                                                                   *
    * IvParameterSpec:                                                  *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Methods:                                                       *
    *       getIV                                                       *
    *                                                                   *
    *********************************************************************/
   void ivparameterspec() {
   /* TO BE DETERMINED
      byte[] getIV() // Returns the initialization vector (IV).
   */
   }

   /*********************************************************************
    *                                                                   *
    * PBEKeySpec:                                                       *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Methods:                                                       *
    *       getPassword                                                 *
    *                                                                   *
    *********************************************************************/
   void pbekeyspec() {
   /* TO BE DETERMINED
      char[] getPassword() // Returns the password.
   */
   }

   /*********************************************************************
    *                                                                   *
    * PBEParameterSpec:                                                 *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Methods:                                                       *
    *       getIterationCount    getSalt                                *
    *                                                                   *
    *********************************************************************/
   void pbeparameterspec() {
   /* TO BE DETERMINED
      int getIterationCount() // Returns the iteration count.
      byte[] getSalt() // Returns the salt.
   */
   }

   /*********************************************************************
    *                                                                   *
    * RC2ParameterSpec:                                                 *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Methods:                                                       *
    *       getEffectiveKeyBits  getIV                                  *
    *                                                                   *
    *********************************************************************/
   void rc2parameterspec() {
   /* TO BE DETERMINED
      int getEffectiveKeyBits() // Returns the effective key size in bits.
      byte[] getIV() // Returns the IV or null if this parameter set does not contain an IV.
   */
   }

   /*********************************************************************
    *                                                                   *
    * RC5ParameterSpec:                                                 *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Methods:                                                       *
    *       getIV                getVersion           getWordSize       *
    *       getRounds                                                   *
    *                                                                   *
    *********************************************************************/
   void rc5parameterspec() {
   /* TO BE DETERMINED
      byte[] getIV() // Returns the IV or null if this parameter set does not contain an IV.
      int getRounds() // Returns the number of rounds.
      int getVersion() // Returns the version.
      int getWordSize() // Returns the word size in bits.
   */
   }

   /*********************************************************************
    *                                                                   *
    * SecretKeySpec:                                                    *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Methods:                                                       *
    *       equals               getEncoded           hashCode          *
    *       getAlgorithm         getFormat                              *
    *                                                                   *
    *********************************************************************/
   void secretkeyspec() {
      int i;
      boolean b;
      String s;
      byte[] bx = new byte[20];
      java.security.SecureRandom r;
      javax.crypto.SecretKey k;
      javax.crypto.SecretKey n;

      r = new java.security.SecureRandom();
      r.nextBytes(bx);
      k = new SecretKeySpec(bx, "HmacSHA1");
      n = new SecretKeySpec(bx, "HmacSHA1");

      b = k.equals(n);
      s = k.getAlgorithm();             // name of the algorithm
      s = k.getFormat();                // name of the encoding format
      bx = k.getEncoded();              // key material of this secret key
      i = k.hashCode();                 // hash code value for the object
   }
}

Chris Rathman / ChrisRath@aol.com