Question

Is there a way to convert an ASN.1 encoded private key of X509Certificate into Base64 format, using C# code?

Was it helpful?

Solution

On second thought - probably I didn't understood your question on first answer. If you wanted to convert key data to format similar to .pem files you need to do something similar as in this article about generating .pem files

OTHER TIPS

You should try to combine together X509Certificate2.PrivateKey Property and RSA.FromXmlString Method.

I think that you can load data from ASN.1 with RSA.FromXmlString and then set X509Certificate2.PrivateKey property to resulting RSA object. After that you can use one of X509Certificate2 Export() methods to export your key.

If you just need to convert ASN.1 DER encoded key to Base64 (.pem) format, you just need to encode binary byte array to Base64 (with System.Convert.ToBase64String()) and add header/footer for result:

-----BEGIN RSA PRIVATE KEY-----
encoded key data goes here
-----END RSA PRIVATE KEY-----
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top