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]

e201. Creating Key Objects from a Set of Digital Signature Algorithm (DSA) Parameters

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 PrivateKey and PublicKey from a set of DSA parameters.

    try {
        // Obtain the DSA parameters;
        // see e200 Getting the Digital Signature Algorithm (DSA) Parameters of a Key Pair
        BigInteger p = ...;
        BigInteger q = ...;
        BigInteger g = ...;
        BigInteger x = ...;
        BigInteger y = ...;
    
        // Create the DSA key factory
        KeyFactory keyFactory = KeyFactory.getInstance("DSA");
    
        // Create the DSA private key
        KeySpec privateKeySpec = new DSAPrivateKeySpec(x, p, q, g);
        PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
    
        // Create the DSA public key
        KeySpec publicKeySpec = new DSAPublicKeySpec(y, p, q, g);
        PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
    } catch (InvalidKeySpecException e) {
    } 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
e200. Getting the Digital Signature Algorithm (DSA) Parameters of a Key Pair

See also: Key Store    Message Digests    Permissions    Policy Files    Signatures   


© 2002 Addison-Wesley.