Java Algid 解析错误,不是序列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6559272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:44:25  来源:igfitidea点击:

Algid parse error, not a sequence

javasecurityrsa

提问by Dmitrii Pisarenko

When trying to read a RSA private key from a file using the method

尝试使用该方法从文件中读取 RSA 私钥时

public PrivateKey getPrivateKey()
        throws NoSuchAlgorithmException,
        InvalidKeySpecException, IOException {

    final InputStream inputStream = getClass().getClassLoader()
                    .getResourceAsStream("privatekey");
    byte[] privKeyBytes = null;
    try {
        privKeyBytes = IOUtils.toByteArray(inputStream);
    } catch (final IOException exception) {
        LOGGER.error("", exception);
        IOUtils.closeQuietly(inputStream);
    }

    LOGGER.debug("privKeyBytes: {}", privKeyBytes);

    String BEGIN = "-----BEGIN RSA PRIVATE KEY-----";
    String END = "-----END RSA PRIVATE KEY-----";
    String str = new String(privKeyBytes);
    if (str.contains(BEGIN) && str.contains(END)) {
        str = str.substring(BEGIN.length(), str.lastIndexOf(END));
    }

    KeyFactory fac = KeyFactory.getInstance("RSA");
    EncodedKeySpec privKeySpec =
            new PKCS8EncodedKeySpec(Base64.decode(str.getBytes()));
    return fac.generatePrivate(privKeySpec);
}

I get the exception

我得到了例外

java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException : algid parse error, not a sequence
    at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:200) ~[na:1.6.0_23]
    at java.security.KeyFactory.generatePrivate(KeyFactory.java:342) ~[na:1.6.0_23]

at the fac.generatePrivate(privKeySpec) call.

在 fac.generatePrivate(privKeySpec) 调用中。

What does this error mean?

这个错误是什么意思?

Thanks

谢谢

Dmitri

德米特里

采纳答案by President James K. Polk

It means your key is not in PKCS#8 format. The easiest thing to do is to use the openssl pkcs8 -topk8 <...other options...>command to convert the key once. Alternatively you can use the PEMReaderclass of the Bouncycastle lightweight API.

这意味着您的密钥不是 PKCS#8 格式。最简单的方法是使用该openssl pkcs8 -topk8 <...other options...>命令将密钥转换一次。或者,您可以使用Bouncycastle 轻量级 APIPEMReader类。

回答by Dimitris

I was having this same issue, and the format of the key was NOT the actual problem.
All I had to do to get rid of that exception was to call

我遇到了同样的问题,密钥的格式不是实际问题。
为了摆脱那个异常,我所要做的就是调用

java.security.Security.addProvider(
         new org.bouncycastle.jce.provider.BouncyCastleProvider()
);


and everything worked


一切正常

回答by Pasha GR

You must make your PCKS8 file from your private key!

你必须用你的私钥制作你的 PCKS8 文件!

private.pem => name of private key file

private.pem => 私钥文件名

openssl genrsa -out private.pem 1024

public_key.pem => name of public key file

public_key.pem => 公钥文件名

openssl rsa -in private.pem -pubout -outform PEM -out public_key.pem

??private_key.pem?? => name of private key with PCKS8 format! you can just read this format in java

??private_key.pem?? => PCKS8 格式的私钥名称!你可以在java中读取这种格式

openssl pkcs8 -topk8 -inform PEM -in private.pem -out private_key.pem -nocrypt