Security generating and exporting DSA keys

0

In this example we will see how to generate DSA keys and export them to files.

Generating DSA Keys

We will first get the KeyPairGenerator for DSA algorithm. Next would be to initialize the generator with a key size and randomness. Finally call generateKeyPair() to generate the key pair.

DSA Keys

In the below example, we generate the key pair and print the private/public data elements.

package com.javarticles.security;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.interfaces.DSAPrivateKey;
import java.security.interfaces.DSAPublicKey;

public class ExportDSAKeyExample {
    public static void main(String[] args) {
        try {
            KeyPairGenerator keyPairGenerator = KeyPairGenerator
                    .getInstance("DSA");
            keyPairGenerator.initialize(1024, new SecureRandom());
            KeyPair kp = keyPairGenerator.generateKeyPair();
            
            DSAPrivateKey dsaPrivKey = (DSAPrivateKey) kp.getPrivate();
            printPrivate(dsaPrivKey);
                        
            DSAPublicKey dsaPubKey = (DSAPublicKey) kp.getPublic();
            printPublic(dsaPubKey);            
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    private static void printPrivate(DSAPrivateKey dsaPrivKey) {
        System.out.println("---------------Private Key---------------");
        System.out.println("X: " + dsaPrivKey.getX());
        System.out.println("P: " + dsaPrivKey.getParams().getP());
        System.out.println("Q: " + dsaPrivKey.getParams().getQ()); 
        System.out.println("G: " + dsaPrivKey.getParams().getG());       
    }
    
    private static void printPublic(DSAPublicKey dsaPubKey) {
        System.out.println("---------------Public Key---------------");
        System.out.println("Y: " + dsaPubKey.getY());
        System.out.println("P: " + dsaPubKey.getParams().getP());
        System.out.println("Q: " + dsaPubKey.getParams().getQ());     
        System.out.println("G: " + dsaPubKey.getParams().getG());
    }
}

Output:

---------------Private Key---------------
X: 73850598995...
P: 17801190547...
Q: 86420549560...
G: 17406820753...
---------------Public Key---------------
Y:247189112459...
P: 17801190547...
Q: 86420549560...
G: 17406820753...

Exporting DSA Keys

package com.javarticles.security;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.interfaces.DSAPrivateKey;
import java.security.interfaces.DSAPublicKey;
import java.security.spec.DSAPrivateKeySpec;
import java.security.spec.DSAPublicKeySpec;

public class ExportDSAKeyExample {
    public static void main(String[] args) {
        try {
            KeyPairGenerator keyPairGenerator = KeyPairGenerator
                    .getInstance("DSA");
            keyPairGenerator.initialize(1024, new SecureRandom());
            KeyPair kp = keyPairGenerator.generateKeyPair();
            DSAPrivateKey dsaPrivKey = (DSAPrivateKey) kp.getPrivate();
            printPrivate(dsaPrivKey);
                        
            DSAPublicKey dsaPubKey = (DSAPublicKey) kp.getPublic();
            printPublic(dsaPubKey);

            KeyFactory kf = KeyFactory.getInstance("DSA");

            // private key
            Class privSpec = (Class) Class
                    .forName("java.security.spec.DSAPrivateKeySpec");
            DSAPrivateKeySpec ks = kf.getKeySpec(kp.getPrivate(), privSpec);
            FileOutputStream fos = new FileOutputStream("sec.priv");
            ObjectOutputStream oos = new ObjectOutputStream(fos);            
            
            oos.writeObject(ks.getX());
            oos.writeObject(ks.getP());
            oos.writeObject(ks.getQ());
            oos.writeObject(ks.getG());
            
            printPrivateKeySec(ks);

            // public key
            Class pubSpec = (Class) Class
                    .forName("java.security.spec.DSAPublicKeySpec");
            DSAPublicKeySpec pks = kf.getKeySpec(kp.getPublic(), pubSpec);
            fos = new FileOutputStream("sec.pub");
            oos = new ObjectOutputStream(fos);
            
            oos.writeObject(pks.getY());
            oos.writeObject(pks.getP());
            oos.writeObject(pks.getQ());
            oos.writeObject(pks.getG());

            printPublicSec(pks);
            
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    private static void printPrivateKeySec(DSAPrivateKeySpec ks) {
        System.out.println("---------------Private Key using KeySpec ---------------");
        System.out.println("X: " + ks.getX());
        System.out.println("P: " + ks.getP());
        System.out.println("Q: " + ks.getQ());
        System.out.println("G: " + ks.getG());
    }
    
    private static void printPublicSec(DSAPublicKeySpec ks) {
        System.out.println("---------------Public Key using KeySpec ---------------");        
        System.out.println("Y: " + ks.getY());
        System.out.println("P: " + ks.getP());
        System.out.println("Q: " + ks.getQ());
        System.out.println("G: " + ks.getG());
    }

    private static void printPrivate(DSAPrivateKey dsaPrivKey) {
        System.out.println("---------------Private Key---------------");
        System.out.println("X: " + dsaPrivKey.getX());
        System.out.println("P: " + dsaPrivKey.getParams().getP());
        System.out.println("Q: " + dsaPrivKey.getParams().getQ()); 
        System.out.println("G: " + dsaPrivKey.getParams().getG());       
    }
    
    private static void printPublic(DSAPublicKey dsaPubKey) {
        System.out.println("---------------Public Key---------------");
        System.out.println("Y: " + dsaPubKey.getY());
        System.out.println("P: " + dsaPubKey.getParams().getP());
        System.out.println("Q: " + dsaPubKey.getParams().getQ());     
        System.out.println("G: " + dsaPubKey.getParams().getG());
    }
}

Download the source code

This was an example about using DSA algorithm to generate private/public keys.

You can download the source code here: javaExportDSAKey.zip
Share.

Comments are closed.