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?
The bouncy castle library includes a PEM reader that will parse your key to a
java.security.interfaces.DSAPublicKey
:The implementing class is apparently
org.bouncycastle.jce.provider.JDKDSAPublicKey
Full example: