Recursive AI

Convert a DER certificate to PEM in C#

OneCrypto is a free, open-source cryptography library for .NET from Recursive AI. MIT or Apache-2.0, preview, no native dependencies.

Call ToPem() to turn a binary X.509 certificate into text. The zip has a sample input, sample.der, with subject CN=fixture.example.

Terminal
dotnet add package RecursiveAI.OneCrypto --prerelease

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneCrypto;
 
var der = File.ReadAllBytes("sample.der");
var cert = Certificate.Parse(der.AsMemory());
var pem = cert.ToPem();
File.WriteAllText("sample.pem", pem);
var decoded = Certificate.FromPem(
    File.ReadAllText("sample.pem"));
Console.WriteLine(pem.Split('\n')[0]);
Console.WriteLine("DER bytes match: " +
    decoded.Encoded.Span.SequenceEqual(der));

Unzip the example, then dotnet run.

Text
-----BEGIN CERTIFICATE-----
DER bytes match: True

The program writes sample.pem, then reads back the written PEM file to compare the certificate bytes. If sample.der isn’t a certificate, Certificate.Parse throws X509ParseException before the output file is written.

Variations

Convert PEM back to DER. Use the sample.pem written by the snippet at the top. This writes the certificate bytes to roundtrip.der:

C#
using RecursiveAI.OneCrypto;
 
var pem = File.ReadAllText("sample.pem");
var cert = Certificate.FromPem(pem);
File.WriteAllBytes("roundtrip.der",
    cert.Encoded.ToArray());

The short answer

Read the DER file as bytes, parse it with Certificate.Parse, and save ToPem() as text. The result is one CERTIFICATE block. Round-trip that text through Certificate.FromPem and compare its Encoded bytes with the original to verify that changing the container didn’t change the certificate.

What is the difference between DER and PEM?

DER is a binary encoding. For an X.509 certificate, the PEM form specified by RFC 7468 §5.1 puts the Base64 representation of those bytes between -----BEGIN CERTIFICATE----- and -----END CERTIFICATE-----. Changing sample.der to sample.pem in the filename alone won’t convert it. The bytes in the middle of the PEM block still represent the same certificate; the conversion doesn’t sign a new certificate, change its expiry, or establish trust.

How do I convert a cert to PEM format?

Use File.ReadAllBytes, not File.ReadAllText: DER is binary. A .cer or .crt extension alone doesn’t tell you whether the file contains binary DER or text PEM.

This recipe handles one certificate, not a private key or a PKCS#12 bundle. A certificate holds public data even if its corresponding private key is stored elsewhere. A PEM PRIVATE KEY block isn’t interchangeable with a CERTIFICATE block. See loading key and certificate files if you need to distinguish those inputs.

How do I convert PEM to DER format?

Use the reverse conversion shown above: read the text, call Certificate.FromPem, and save Encoded as bytes. FromPem reads the first certificate block. If your input holds a chain, it won’t return all the certificates; handle each certificate block separately when you need to preserve the full chain. If there’s no certificate block, it throws X509ParseException.

All onecrypto recipes