假设我有一个DSA公钥,如下所示:
int countCounter = 0;
CountDownTimer mCountDownTimer = new CountDownTimer() {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
if (countCounter < 3){
countCounter++;
// cancel the counter and restart it!
mCountDownTimer.cancel();
mCountDownTimer.start();
}
}
};
我通过-----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-----
我希望能够在Java中加载此密钥,但我不清楚如何。
我用RSA做的是......我将base64解码为一个字节数组,然后将该字节数组传递给openssl dsa -in dsa_priv.pem -pubout -out dsa_pub.pem
。此时,您可以将单个参数传递给org.bouncycastle.asn1.pkcs.RSAPrivateKey.getInstance()
,然后从中执行密钥。
但是我如何使用DSA做到这一点?我可以说,附近没有java.security.spec.RSAPrivateKeySpec
课程。这是我尝试导入时得到的错误:
org.bouncycastle.asn1.pkcs.RSAPrivateKey
有什么想法吗?
答案 0 :(得分:1)
充气城堡图书馆包含一个PEM阅读器,可以将您的密钥解析为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());
实施类显然是org.bouncycastle.jce.provider.JDKDSAPublicKey
完整示例:
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());
}
}