how to get private key from PEM file?

后端 未结 6 764
迷失自我
迷失自我 2020-11-27 02:51

i have a .PEM file that includes public key and a private key for SSL data transfer like this:

-----BEGIN RSA PRIVATE KEY-----
      private key data
-----EN         


        
6条回答
  •  臣服心动
    2020-11-27 03:20

    AFAIK the .NET framework does not support PEM anywhere.

    You can hack around this easily for the X509Certificate part since you can extract the base64 string between the -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- lines, convert it into a byte[] and create the X509Certificate from it.

    An easy solution is to copy-paste code from Mono.Security's X509Certificate.cs to do this.

    Getting the private key is a bit tricky since getting the byte[] won't be of much help to reconstruct the RSA instance (which we can assume since the PEM header states it's RSA).

    This time you better copy-paste from Mono.Security's PKCS8.cs file and sioply call the decode method.

    Disclaimer: I'm the main author of the Mono code discussed above and it is all available under the MIT.X11 license

提交回复
热议问题