gpt4 book ai didi

java - SHA1withRSA NoSuchAlgorithmException

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

您好,我有以下函数可以在我的应用程序中将字符串转换为私钥:

public static PrivateKey main() throws Exception {
// Read in the key into a String
StringBuilder pkcs8Lines = new StringBuilder();
BufferedReader rdr = new BufferedReader(new StringReader(PRIVATE_KEY));
String line;
while ((line = rdr.readLine()) != null) {
pkcs8Lines.append(line);
}

// Remove the "BEGIN" and "END" lines, as well as any whitespace

String pkcs8Pem = pkcs8Lines.toString();
pkcs8Pem = pkcs8Pem.replaceAll("\\n+","");

// Base64 decode the result

byte [] pkcs8EncodedBytes = Base64.decode(pkcs8Pem, Base64.DEFAULT);

// extract the private key

PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pkcs8EncodedBytes);
KeyFactory kf = KeyFactory.getInstance("SHA1WITHRSA");
PrivateKey privKey = kf.generatePrivate(keySpec);
return privKey;
}

我得到以下异常:

W/System.err: java.security.NoSuchAlgorithmException: SHA1withRSA KeyFactory not available W/System.err: at java.security.KeyFactory.(KeyFactory.java:161) at java.security.KeyFactory.getInstance(KeyFactory.java:195)

所以我尝试找到可以通过此代码使用的所有算法:

        TreeSet<String> algorithms = new TreeSet<>();
for (Provider provider : Security.getProviders())
for (Provider.Service service : provider.getServices())
if (service.getType().equals("Signature"))
algorithms.add(service.getAlgorithm());
for (String algorithm : algorithms)
System.out.println(algorithm);

响应中包含“SHA1withRSA”,您知道问题出在哪里吗?

最佳答案

SHA1withRSA 是一种签名类型,您会在列表中看到它,因为您有

if (service.getType().equals("Signature"))

如果您将其编辑为

if (service.getType().equals("KeyFactory"))

您应该看到一个类似这样的列表

DSA
EC
RSA
RSASSA-PSS
X25519
X448
XDH

关于java - SHA1withRSA NoSuchAlgorithmException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58923401/

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