gpt4 book ai didi

java - 在 Java 中加载 DSA 公钥

转载 作者:行者123 更新时间:2023-12-01 09:04:12 24 4
gpt4 key购买 nike

假设我有一个如下所示的 DSA 公钥:

-----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-----

我通过openssl dsa -in dsa_priv.pem -pubout -out dsa_pub.pem生成了这个 key

我希望能够在 Java 中加载此 key ,但我不清楚如何加载。

我对 RSA 的做法是...我将字符串进行 base64 解码为字节数组,然后将该字节数组传递给 org.bouncycastle.asn1.pkcs.RSAPrivateKey.getInstance() 。此时,您可以将各个参数传递给 java.security.spec.RSAPrivateKeySpec并用那里的 key 做事。

但是我如何使用 DSA 做到这一点?没有org.bouncycastle.asn1.pkcs.RSAPrivateKey类,据我所知接近。这是我尝试导入它时遇到的错误:

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

有什么想法吗?

最佳答案

充气城堡库包含一个 PEM 读取器,它将把您的 key 解析为 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());
}
}

关于java - 在 Java 中加载 DSA 公钥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41420211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com