---
title: "Convert a DER certificate to PEM in C#"
url: https://recursiveai.org/onecrypto/cookbook/convert-der-certificate-to-pem/
description: "Convert a binary DER X.509 certificate to PEM in C#. Write a CERTIFICATE block and read it back to check that the DER bytes match."
version: "0.4.0-preview.2"
---

# 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneCrypto) · [GitHub](https://github.com/recursive-ai-oss/onecrypto-csharp) · [Docs](https://recursiveai.org/onecrypto/docs/) · [Download example (.zip)](https://recursiveai.org/onecrypto/cookbook/convert-der-certificate-to-pem/convert-der-certificate-to-pem.zip)

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

```shell
dotnet add package RecursiveAI.OneCrypto --prerelease
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onecrypto/cookbook/convert-der-certificate-to-pem/convert-der-certificate-to-pem.zip), 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`:

```csharp
using RecursiveAI.OneCrypto;

var pem = File.ReadAllText("sample.pem");
var cert = Certificate.FromPem(pem);
File.WriteAllBytes("roundtrip.der",
    cert.Encoded.ToArray());
```

## Related recipes

- [Get X.509 subject alternative names in C#](https://recursiveai.org/onecrypto/cookbook/read-x509-subject-alternative-names/)
- [Convert a PEM certificate and key to PFX in C#](https://recursiveai.org/onecrypto/cookbook/convert-pem-to-pfx/)

---

## 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](https://www.rfc-editor.org/rfc/rfc7468#section-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](https://recursiveai.org/onecrypto/docs/load/) 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`.
