Chris Rathman / Chris.Rathman@tx.rr.com

/************************************************************************
 *                                                                      *
 * #java.security.spec                                                  *
 *                                                                      *
 *    Interfaces:                                                       *
 *       AlgorithmParameterSpec          KeySpec                        *
 *                                                                      *
 *    Classes:                                                          *
 *       DSAParameterSpec                RSAPrivateCrtKeySpec           *
 *       DSAPrivateKeySpec               RSAPrivateKeySpec              *
 *       DSAPublicKeySpec                RSAPublicKeySpec               *
 *       EncodedKeySpec                  X509EncodedKeySpec             *
 *       PKCS8EncodedKeySpec                                            *
 *                                                                      *
 *    Exceptions:                                                       *
 *       InvalidKeySpecException         InvalidParameterSpecException  *
 *                                                                      *
 ************************************************************************/
package Test.Chris;
import java.security.spec.*;

public class Java_security_spec {

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

   public void exercise() {
      keyspec();
      dsaprivatekeyspec();
      dsapublickeyspec();
      dsaparameterspec();
      encodedkeyspec();
      pkcs8encodedkeyspec();
      x509encodedkeyspec();
      rsaprivatecrtkeyspec();
      rsaprivatekeyspec();
      rsapublickeyspec();
      algorithmparameterspec();
   }

   /*********************************************************************
    *                                                                   *
    * KeySpec:                                                          *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *********************************************************************/
   void keyspec() {
      String s;
      KeySpec ks;
      java.security.KeyPair kp;
      java.security.KeyPairGenerator kpg;
      java.security.KeyFactory kf;

      try {
         kpg = java.security.KeyPairGenerator.getInstance("DSA");
         kpg.initialize(512);
         kp = kpg.generateKeyPair();
         kf = java.security.KeyFactory.getInstance("DSA");
         ks = kf.getKeySpec(kp.getPrivate(), DSAPrivateKeySpec.class);

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

   /*********************************************************************
    *                                                                   *
    * DSAPrivateKeySpec:                                                *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Methods:                                                       *
    *       getG                 getQ                 getX              *
    *       getP                                                        *
    *                                                                   *
    *********************************************************************/
   void dsaprivatekeyspec() {
      String s;
      java.math.BigInteger g;
      java.math.BigInteger p;
      java.math.BigInteger q;
      java.math.BigInteger x;
      DSAPrivateKeySpec ks;
      java.security.KeyPair kp;
      java.security.KeyPairGenerator kpg;
      java.security.KeyFactory kf;

      try {
         kpg = java.security.KeyPairGenerator.getInstance("DSA");
         kpg.initialize(512);
         kp = kpg.generateKeyPair();
         kf = java.security.KeyFactory.getInstance("DSA");
         ks = (DSAPrivateKeySpec)kf.getKeySpec(kp.getPrivate(), DSAPrivateKeySpec.class);

         g = ks.getG();                 // returns the base g.
         p = ks.getP();                 // returns the prime p.
         q = ks.getQ();                 // returns the sub-prime q.
         x = ks.getX();                 // returns the private key x.

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

   /*********************************************************************
    *                                                                   *
    * DSAPublicKeySpec:                                                 *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Methods:                                                       *
    *       getG                 getQ                 getY              *
    *       getP                                                        *
    *                                                                   *
    *********************************************************************/
   void dsapublickeyspec() {
      String s;
      java.math.BigInteger g;
      java.math.BigInteger p;
      java.math.BigInteger q;
      java.math.BigInteger y;
      DSAPublicKeySpec ks;
      java.security.KeyPair kp;
      java.security.KeyPairGenerator kpg;
      java.security.KeyFactory kf;

      try {
         kpg = java.security.KeyPairGenerator.getInstance("DSA");
         kpg.initialize(512);
         kp = kpg.generateKeyPair();
         kf = java.security.KeyFactory.getInstance("DSA");
         ks = (DSAPublicKeySpec)kf.getKeySpec(kp.getPublic(), DSAPublicKeySpec.class);

         g = ks.getG();                 // returns the base g
         p = ks.getP();                 // returns the prime p
         q = ks.getQ();                 // returns the sub-prime q
         y = ks.getY();                 // returns the public key y

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

   /*********************************************************************
    *                                                                   *
    * DSAParameterSpec:                                                 *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Methods:                                                       *
    *       getG                 getP                 getQ              *
    *                                                                   *
    *********************************************************************/
   void dsaparameterspec() {
   /* TO BE DETERMINED
      BigInteger getG() // Returns the base g.
      BigInteger getP() // Returns the prime p.
      BigInteger getQ() // Returns the sub-prime q.
   */
   }

   /*********************************************************************
    *                                                                   *
    * EncodedKeySpec:                                                   *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Methods:                                                       *
    *       getEncoded           getFormat                              *
    *                                                                   *
    *********************************************************************/
   void encodedkeyspec() {
      String s;
      byte[] b;
      EncodedKeySpec ks;
      java.security.KeyPair kp;
      java.security.KeyPairGenerator kpg;
      java.security.Key k;

      try {
         kpg = java.security.KeyPairGenerator.getInstance("DSA");
         kpg.initialize(512);
         kp = kpg.generateKeyPair();
         k = kp.getPrivate();
         ks = new X509EncodedKeySpec(k.getEncoded());

         b = ks.getEncoded();           // encoded key
         s = ks.getFormat();            // name of the encoding format

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

   /*********************************************************************
    *                                                                   *
    * PKCS8EncodedKeySpec:                                              *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Methods:                                                       *
    *       getEncoded           getFormat                              *
    *                                                                   *
    *********************************************************************/
   void pkcs8encodedkeyspec() {
      String s;
      byte[] b;
      PKCS8EncodedKeySpec ks;
      java.security.KeyPair kp;
      java.security.KeyPairGenerator kpg;
      java.security.Key k;

      try {
         kpg = java.security.KeyPairGenerator.getInstance("DSA");
         kpg.initialize(512);
         kp = kpg.generateKeyPair();
         k = kp.getPublic();
         ks = new PKCS8EncodedKeySpec(k.getEncoded());

         b = ks.getEncoded();           // encoded key
         s = ks.getFormat();            // name of the encoding format

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

   /*********************************************************************
    *                                                                   *
    * X509EncodedKeySpec:                                               *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Methods:                                                       *
    *       getEncoded           getFormat                              *
    *                                                                   *
    *********************************************************************/
   void x509encodedkeyspec() {
      String s;
      byte[] b;
      X509EncodedKeySpec ks;
      java.security.KeyPair kp;
      java.security.KeyPairGenerator kpg;
      java.security.Key k;

      try {
         kpg = java.security.KeyPairGenerator.getInstance("DSA");
         kpg.initialize(512);
         kp = kpg.generateKeyPair();
         k = kp.getPrivate();
         ks = new X509EncodedKeySpec(k.getEncoded());

         b = ks.getEncoded();           // encoded key
         s = ks.getFormat();            // name of the encoding format

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

   /*********************************************************************
    *                                                                   *
    * RSAPrivateCrtKeySpec:                                             *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Methods:                                                       *
    *       getCrtCoefficient    getPrimeExponentQ    getPrimeQ         *
    *       getPrimeExponentP    getPrimeP            getPublicExponent *
    *                                                                   *
    *********************************************************************/
   void rsaprivatecrtkeyspec() {
   /* TO BE DETERMINED
      BigInteger getCrtCoefficient() // Returns the crtCoefficient.
      BigInteger getPrimeExponentP() // Returns the primeExponentP.
      BigInteger getPrimeExponentQ() // Returns the primeExponentQ.
      BigInteger getPrimeP() // Returns the primeP.
      BigInteger getPrimeQ() // Returns the primeQ.
      BigInteger getPublicExponent() // Returns the public exponent.
   */
   }

   /*********************************************************************
    *                                                                   *
    * RSAPrivateKeySpec:                                                *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Methods:                                                       *
    *       getModulus           getPrivateExponent                     *
    *                                                                   *
    *********************************************************************/
   void rsaprivatekeyspec() {
   /* TO BE DETERMINED
      BigInteger getModulus() // Returns the modulus.
      BigInteger getPrivateExponent() // Returns the private exponent.
   */
   }

   /*********************************************************************
    *                                                                   *
    * RSAPublicKeySpec:                                                 *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Methods:                                                       *
    *       getModulus           getPublicExponent                      *
    *                                                                   *
    *********************************************************************/
   void rsapublickeyspec() {
   /* TO BE DETERMINED
      BigInteger getModulus() // Returns the modulus.
      BigInteger getPublicExponent() // Returns the public exponent.
   */
   }

   /*********************************************************************
    *                                                                   *
    * AlgorithmParameterSpec:                                           *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *********************************************************************/
   void algorithmparameterspec() {
   /* TO BE DETERMINED
   */
   }
}

Chris Rathman / Chris.Rathman@tx.rr.com