Read RSA Private Key Java: algid parse error, not a sequence 终极解决方案

# 1

···

        KeyFactory keyFactory = KeyFactory.getInstance("RSA");

        byte[] encodeByte = Base64.getDecoder().decode(privateKeyString);

        RSAPrivateKey privateKey = (RSAPrivateKey) keyFactory.generatePrivate(parsePKCS1( encodeByte ) );

        Algorithm algorithm = Algorithm.RSA256(null, privateKey );

···


``` 

private static RSAPrivateCrtKeySpecparsePKCS1(byte[] source) {

Asn1Parser p =new Asn1Parser(source);

    // https://en.wikipedia.org/wiki/X.690#BER_encoding

// https://tools.ietf.org/html/rfc8017#page-55

// Type (sequence)

    p.parseTag(0x30);

    // Length

    p.parseFullLength();

    BigInteger version = p.parseInt();

    if (version.intValue() ==1) {

// JRE doesn't provide a suitable constructor for multi-prime

// keys

        log.error("parse error" );

    }

return new RSAPrivateCrtKeySpec(p.parseInt(), p.parseInt(), p.parseInt(), p.parseInt(),

            p.parseInt(), p.parseInt(), p.parseInt(), p.parseInt());

}

``` 

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容