Search code examples
c#securitycertificatex509certificate2

Converting a PKCS12 certificate to PEM certificate in .NET and vice versa?


PKCS12 certificate is stored in binary format while PEM certificate is an ASCII file which can be opened and viewed in a text editor.

Q1. How can I convert a PEM ceritificate of the form below to a PKCS12 certificate programmatically?

-----BEGIN CERTIFICATE----- 
*****CERTIFICATE DATA*****
-----END CERTIFICATE-----

-----BEGIN RSA PRIVATE KEY----- 
*****PRIVATE KEY DATA******** 
-----END RSA PRIVATE KEY-----

Q2. Now I have read a PKCS12 file and I want to convert the PKCS12 data into a PEM format, again using a .NET program?

Do I need to use any of the encoding schemes supported in .NET?

Note: I am using CryptoAPI library


Solution

  • I believe the Mentalis.org Security Library can do this. I think they have an example that covers what you want to do; look for the Cert2PFX project in the download.