Search code examples
c#x509certificatepem

x509certificate2 to PEM in NET FR. 4.7.2


I cant seem to find any info on how to create a PEM from x509certificate2 in .NET FRAMEWORK 4.7.2. There is no PemEncoding class in the legacy framework.

I have a legacy project, where migrating to .net CORE or .NET5 is not an option right now. Could somebody point me in right direction?

Thanks.


Solution

  • You can construct it manually:

    -----BEGIN CERTIFICATE-----
    <Base64-encoded content with 64 chars on every line>
    -----END CERTIFICATE-----
    

    Flat base64 string is created easily:

    String b64 = Convert.ToBas64String(cert.RawData);
    

    then insert a new line character after every 64 chars in string and insert resulting string between PEM header and footer.