Security Importing DSA keys

0

In our last article, we worked on exporting DSA keys to files. In this article we will see how to import the DSA keys back from the files.

In order to generate the security keys, we first need to read the four parameter values which define the key and build the key specification.

DSAPublicKeySpec ks = new DSAPublicKeySpec(
     (BigInteger) ois.readObject(),
     (BigInteger) ois.readObject(),
     (BigInteger) ois.readObject(),
     (BigInteger) ois.readObject());

Next we would need to get a KeyFactory instance based on our security algorithm that will take these parameter values and in turn convert it to the security key.

KeyFactory kf = KeyFactory.getInstance("DSA");
pubKey = kf.generatePublic(ks);

ImportDSAKeyExample:

package com.javarticles.security;

import java.security.interfaces.DSAPrivateKey;
import java.security.interfaces.DSAPublicKey;

public class ImportDSAKeyExample {

    public static void main(String[] args) {
        ImportDSAKey importKeys = new ImportDSAKey("sec.priv", "sec.pub");
        System.out.println(((DSAPrivateKey) importKeys.getPrivKey()).getX());
        System.out.println(((DSAPublicKey) importKeys.getPubKey()).getY());
    }
}

ImportDSAKey:

package com.javarticles.security;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.DSAPrivateKeySpec;
import java.security.spec.DSAPublicKeySpec;

public class ImportDSAKey {
    private PrivateKey privKey;
    private PublicKey pubKey;

    public ImportDSAKey(String privateKeyFile, String publicKeyFile) {
        importPrivateKeys(privateKeyFile);
        importPublicKeys(publicKeyFile);
    }

    private void importPublicKeys(String publicKeyFile) {
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream(publicKeyFile));
            DSAPublicKeySpec ks = new DSAPublicKeySpec(
                    (BigInteger) ois.readObject(),
                    (BigInteger) ois.readObject(),
                    (BigInteger) ois.readObject(),
                    (BigInteger) ois.readObject());
            KeyFactory kf = KeyFactory.getInstance("DSA");
            pubKey = kf.generatePublic(ks);
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    System.out.println(e);
                }
            }
        }
    }

    private void importPrivateKeys(String privateKeyFile) {
        ObjectInputStream ois = null;
        try {

            ois = new ObjectInputStream(
                    new FileInputStream(privateKeyFile));
            DSAPrivateKeySpec ks = new DSAPrivateKeySpec(
                    (BigInteger) ois.readObject(),
                    (BigInteger) ois.readObject(),
                    (BigInteger) ois.readObject(),
                    (BigInteger) ois.readObject());
            KeyFactory kf = KeyFactory.getInstance("DSA");
            privKey = kf.generatePrivate(ks);
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    System.out.println(e);
                }
            }
        }
    }

    public PrivateKey getPrivKey() {
        return privKey;
    }

    public PublicKey getPubKey() {
        return pubKey;
    }

}

Download the source code

This was an example about importing DSA keys from files.

You can download the example here: javaImportDSAKeys.zip
Share.

Comments are closed.