The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.security  [30 examples] > Public and Private Keys  [5 examples]

e200. Getting the Digital Signature Algorithm (DSA) Parameters of a Key Pair

The DSA requires three parameters to create a key pair --- the prime (P), the subprime (Q), and the base (G). These three values are used to create a private key (called X) and a public key (called Y).

This example creates a DSA key pair with provider-supplied default values for P, Q, and G, and then retrieves the default values.

    try {
        // Generate a 1024-bit Digital Signature Algorithm (DSA) key pair
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
        keyGen.initialize(1024);
        KeyPair keypair = keyGen.genKeyPair();
        DSAPrivateKey privateKey = (DSAPrivateKey)keypair.getPrivate();
        DSAPublicKey publicKey = (DSAPublicKey)keypair.getPublic();
    
        // Get p, q, g; they are the same for both private and public keys
        DSAParams dsaParams = privateKey.getParams();
        BigInteger p = dsaParams.getP();
        BigInteger q = dsaParams.getQ();
        BigInteger g = dsaParams.getG();
    
        // Get the private key's X
        BigInteger x = privateKey.getX();
    
        // Get the public key's Y
        BigInteger y = publicKey.getY();
    } catch (NoSuchAlgorithmException e) {
    }

 Related Examples
e197. Listing All Available Public/Private Key Generators
e198. Generating a Public/Private Key Pair
e199. Getting the Bytes of a Generated Key Pair
e201. Creating Key Objects from a Set of Digital Signature Algorithm (DSA) Parameters

See also: Key Store    Message Digests    Permissions    Policy Files    Signatures   


© 2002 Addison-Wesley.