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

/************************************************************************
 *                                                                      *
 * java.security.interfaces                                             *
 *                                                                      *
 *    Interfaces:                                                       *
 *       DSAKey                          DSAPrivateKey                  *
 *       DSAKeyPairGenerator             DSAPublicKey                   *
 *       DSAParams                                                      *
 *                                                                      *
 *      #RSAPrivateCrtKey               #RSAPublicKey                   *
 *      #RSAPrivateKey                                                  *
 *                                                                      *
 ************************************************************************/
package Test.Chris;
import java.security.interfaces.*;

public class Java_security_interfaces {

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

   public void exercise() {
      dsakey();
      dsakeypairgenerator();
      dsaparams();
      dsaprivatekey();
      dsapublickey();
      rsaprivatecrtkey();
      rsaprivatekey();
      rsapublickey();
   }

   /*********************************************************************
    *                                                                   *
    * DSAKey Interface:                                                 *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Methods:                                                       *
    *       getParams                                                   *
    *                                                                   *
    *********************************************************************/
   void dsakey() {
      java.security.KeyPairGenerator kpg;
      DSAParams parm;
      DSAKey k;

      try {
         kpg = java.security.KeyPairGenerator.getInstance("DSA");
         kpg.initialize(512);
         k = (DSAKey)kpg.genKeyPair().getPublic();
         parm = k.getParams();          // DSA-specific key parameters

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

   /*********************************************************************
    *                                                                   *
    * DSAKeyPairGenerator Interface:                                    *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Methods:                                                       *
    *       initialize                                                  *
    *                                                                   *
    *********************************************************************/
   void dsakeypairgenerator() {
      java.security.SecureRandom rand;
      DSAParams parm;
      DSAKeyPairGenerator kpg;

      try {
         rand = java.security.SecureRandom.getInstance("SHA1PRNG");
         kpg = (DSAKeyPairGenerator)java.security.KeyPairGenerator.getInstance("DSA");
         kpg.initialize(512, true, rand);  // initialize key pair generator
         parm = ((DSAPublicKey)(((java.security.KeyPairGenerator)kpg).genKeyPair().getPublic())).getParams();
         kpg.initialize(parm, rand);       // initialize key pair generator - with parameters

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

   /*********************************************************************
    *                                                                   *
    * DSAParams Interface:                                              *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Methods:                                                       *
    *       getG                 getP                 getQ              *
    *                                                                   *
    *********************************************************************/
   void dsaparams() {
      java.security.KeyPairGenerator kpg;
      java.math.BigInteger g;
      java.math.BigInteger q;
      java.math.BigInteger p;
      DSAParams parm;

      try {
         kpg = java.security.KeyPairGenerator.getInstance("DSA");
         kpg.initialize(512);
         parm = ((DSAPublicKey)(kpg.genKeyPair().getPublic())).getParams();

         g = parm.getG();               // returns the base, g
         p = parm.getP();               // returns the prime, p
         q = parm.getQ();               // returns the subprime, q

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

   /*********************************************************************
    *                                                                   *
    * DSAPrivateKey Interface:                                          *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Fields:                                                        *
    *       serialVersionUID                                            *
    *                                                                   *
    *    Methods:                                                       *
    *       getX                                                        *
    *                                                                   *
    *********************************************************************/
   void dsaprivatekey() {
      long n;
      java.security.KeyPairGenerator kpg;
      java.math.BigInteger x;
      DSAPrivateKey k;

      try {
         kpg = java.security.KeyPairGenerator.getInstance("DSA");
         kpg.initialize(512);
         k = (DSAPrivateKey)kpg.genKeyPair().getPrivate();

         n = DSAPrivateKey.serialVersionUID;
         x = k.getX();                  // value of the private key, x

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

   /*********************************************************************
    *                                                                   *
    * DSAPublicKey Interface:                                           *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Fields:                                                        *
    *       serialVersionUID                                            *
    *                                                                   *
    *    Methods:                                                       *
    *       getY                                                        *
    *                                                                   *
    *********************************************************************/
   void dsapublickey() {
      long n;
      java.security.KeyPairGenerator kpg;
      java.math.BigInteger y;
      DSAPublicKey k;

      try {
         kpg = java.security.KeyPairGenerator.getInstance("DSA");
         kpg.initialize(512);
         k = (DSAPublicKey)kpg.genKeyPair().getPublic();

         n = DSAPublicKey.serialVersionUID;
         y = k.getY();                  // value of the public key, y

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

   /*********************************************************************
    *                                                                   *
    * RSAPrivateCrtKey Interface:                                       *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Methods:                                                       *
    *       getCrtCoefficient    getPrimeExponentQ    getPrimeQ         *
    *       getPrimeExponentP    getPrimeP            getPublicExponent *
    *                                                                   *
    *********************************************************************/
   void rsaprivatecrtkey() {
   /* 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.
   */
   }

   /*********************************************************************
    *                                                                   *
    * RSAPrivateKey Interface:                                          *
    *                                                                   *
    *    Desc:                                                          *
    *                                                                   *
    *    Methods:                                                       *
    *       getModulus           getPrivateExponent                     *
    *                                                                   *
    *********************************************************************/
   void rsaprivatekey() {
      long n;
      java.security.KeyPairGenerator kpg;
      java.math.BigInteger mod;
      java.math.BigInteger exp;
      RSAPrivateKey k;

      try {
         kpg = java.security.KeyPairGenerator.getInstance("DSA");
         kpg.initialize(512);

   /* TO BE DETERMINED
         k = (RSAPrivateKey)kpg.genKeyPair().getPrivate();
         mod = k.getModulus();          // modulus
         exp = k.getPrivateExponent();  // private exponent
   */

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

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

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