Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SHA1不包含RSA算法异常_Java_Android_Rsa_Sha1 - Fatal编程技术网

Java SHA1不包含RSA算法异常

Java SHA1不包含RSA算法异常,java,android,rsa,sha1,Java,Android,Rsa,Sha1,您好,我有以下功能将应用程序中的字符串转换为私钥: 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)); Stri

您好,我有以下功能将应用程序中的字符串转换为私钥:

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.nosuchagorithmexception:SHA1withRSA-KeyFactory不可用 W/System.err:at java.security.KeyFactory.(KeyFactory.java:161) 位于java.security.KeyFactory.getInstance(KeyFactory.java:195)

因此,我试图通过以下代码找到我可以使用的所有AlghoritM:

        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);
TreeSet算法=新的TreeSet();
对于(提供程序:Security.getProviders())
对于(Provider.Service服务:Provider.getServices())
if(service.getType().equals(“签名”))
add(service.getAlgorithm());
for(字符串算法:算法)
System.out.println(算法);

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

SHA1withRSA是一种签名类型,您可以在列表中看到,因为您

if (service.getType().equals("Signature"))
如果你把它编辑成

if (service.getType().equals("KeyFactory"))
您应该看到一个类似这样的列表

DSA
EC
RSA
RSASSA-PSS
X25519
X448
XDH

您发布的代码中没有KeyFactory,您只发布了堆栈跟踪的一部分。发布整个stacktrace。对不起,我已经更新了我的问题