Load a RSA private key in Java (algid parse error, not a sequence)

后端 未结 6 1637
夕颜
夕颜 2020-12-08 05:00

I\'m trying to load a private RSA key generated with ssl into java, my code is:

Generate the key:

openssl genrsa -out mykey.pem 1024         


        
6条回答
  •  伪装坚强ぢ
    2020-12-08 05:21

    You get this error because you are reading a PKCS#8 key file, but your file has a PKCS#1 format (PKCS#1 has the BEGIN RSA PRIVATE KEY header while PKCS#8 has the BEGIN PRIVATE KEY header).

    In order to read both PKCS#1 and PKCS#8 PEM files, I use the source code of Apache JMeter's org.apache.jmeter.protocol.oauth.sampler.PrivateKeyReader:

    PrivateKey pk = (new PrivateKeyReader("/path/to/myfile.der")).getPrivateKey();

提交回复
热议问题