loading a DSA public key in Java

1k views Asked by At

Let's say I have a DSA public key that looks like this:

-----BEGIN PUBLIC KEY-----
MIIBuDCCASwGByqGSM44BAEwggEfAoGBAOwYAcAzXpuw+XCXuNp5zhAzKdhrRguI
uI5kLia8fhRb+1EnFPNpXt4fUS2c/0P0nvzH/TvApizzMkRYJea6rRSW5B+MDjv6
lvrxv+5xBM15kdug033mgSL7wEJIrTLwbe5/djz2oe+pr1KLqs/fvgyKcQyttUWb
5SmwZ+UVx3zfAhUAu0kA2L6VgbvEwpD9sTj5tLyB6Y0CgYEA5GjC+KsPsAH3HZKl
2IwTjX47iNVHyuzr4ZcyXceJ/pi3WR6bQJ6tpf1I2jIE0DOMPlNUwYh0aWBGvoY2
t4d5cwZaW90OS8IAIRFkQS0ywpmJyb7KXqRHwAYdMID88GW0d/KsVB3if0j/9QOo
jhGOrO+kJcZBxUSxINgIIEYFAlEDgYUAAoGBALnHTAZlpoLJZuSBVtnMuRM3cSX4
3IkE9w9FveDV1jX5mmfK7yBVpQFV8eVJfk91ERQ4Dn6ePLUv2dRIt4a0S0qHqadg
zyoFyqkmmUi1kNLyixtRqh+m2gXx0t63HEpZDbEPppdpnlppZquVQh7TyrKSXW9M
TzUkQjFI9UY7kZeK
-----END PUBLIC KEY-----

I generated this key by doing openssl dsa -in dsa_priv.pem -pubout -out dsa_pub.pem

I would like to be able to load this key in Java but it is not clear to me how.

What I'd do with RSA is... I'd base64 decode the string to a byte array and then pass that byte array to org.bouncycastle.asn1.pkcs.RSAPrivateKey.getInstance(). At that point you could pass the individual paramteres to java.security.spec.RSAPrivateKeySpec and do stuff with the key from there.

But how do I do that with DSA? There's not a org.bouncycastle.asn1.pkcs.RSAPrivateKey class, near as I can tell. Here's the error I get when I try to import it:

import org.bouncycastle.asn1.pkcs.DSAPublicKey;
                                 ^
  symbol:   class DSAPublicKey
  location: package org.bouncycastle.asn1.pkcs
1 error

Any ideas?

1

There are 1 answers

1
teppic On BEST ANSWER

The bouncy castle library includes a PEM reader that will parse your key to a java.security.interfaces.DSAPublicKey:

PEMReader reader = new PEMReader(new FileReader("id_dsa.pub"));
DSAPublicKey publicKey = (DSAPublicKey) reader.readObject();
// What implementation are we dealing with?
System.out.println(publicKey.getClass());

The implementing class is apparently org.bouncycastle.jce.provider.JDKDSAPublicKey

Full example:

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openssl.PEMReader;

import java.io.StringReader;
import java.security.Security;
import java.security.interfaces.DSAPublicKey;

public class DSA {
    public static void main(String[] args) throws Exception {
        String key =
            "-----BEGIN PUBLIC KEY-----\n" +
            "MIIBuDCCASwGByqGSM44BAEwggEfAoGBAOwYAcAzXpuw+XCXuNp5zhAzKdhrRguI\n" +
            "uI5kLia8fhRb+1EnFPNpXt4fUS2c/0P0nvzH/TvApizzMkRYJea6rRSW5B+MDjv6\n" +
            "lvrxv+5xBM15kdug033mgSL7wEJIrTLwbe5/djz2oe+pr1KLqs/fvgyKcQyttUWb\n" +
            "5SmwZ+UVx3zfAhUAu0kA2L6VgbvEwpD9sTj5tLyB6Y0CgYEA5GjC+KsPsAH3HZKl\n" +
            "2IwTjX47iNVHyuzr4ZcyXceJ/pi3WR6bQJ6tpf1I2jIE0DOMPlNUwYh0aWBGvoY2\n" +
            "t4d5cwZaW90OS8IAIRFkQS0ywpmJyb7KXqRHwAYdMID88GW0d/KsVB3if0j/9QOo\n" +
            "jhGOrO+kJcZBxUSxINgIIEYFAlEDgYUAAoGBALnHTAZlpoLJZuSBVtnMuRM3cSX4\n" +
            "3IkE9w9FveDV1jX5mmfK7yBVpQFV8eVJfk91ERQ4Dn6ePLUv2dRIt4a0S0qHqadg\n" +
            "zyoFyqkmmUi1kNLyixtRqh+m2gXx0t63HEpZDbEPppdpnlppZquVQh7TyrKSXW9M\n" +
            "TzUkQjFI9UY7kZeK\n" +
            "-----END PUBLIC KEY-----";

        Security.addProvider(new BouncyCastleProvider());
        PEMReader reader = new PEMReader(new StringReader(key));
        DSAPublicKey decoded = (DSAPublicKey) reader.readObject();
        System.out.println(decoded.getClass());
    }
}