How to load X509Certificate2 from a DER Certificate with Private Key?

267 Views Asked by At

When using PEM format for both certificate and its private key, it is easy to load them into .NET program by calling:

X509Certificate2 Certificate = X509Certificate2.CreateFromPemFile(SslCertificate, SslPrivateKey);

It returns a correct object, suitable for SslStream.AuthenticateAsServer method, and other encrypting stuff as well. But I'm have only DER-formatted files, and loading of them results in a exception:

System.Security.Cryptography.CryptographicException: The certificate contents do not contain a PEM with a CERTIFICATE label, or the content is malformed.

Attempt to load via Certificate = new X509Certificate2(SslCertificate) gives a X509Certificate2 object without Private Key, making it useless for SslStream.

How to properly load DER files (a certificate and a private key) as X509Certificate2 object in C#?

0

There are 0 best solutions below