Skip to content

Instantly share code, notes, and snippets.

@mockpit
mockpit / RsaToDerConverter.cs
Created July 25, 2017 09:58
Convert RSACryptoServiceProvider key parameters to PEM format
public class RsaToDerConverter
{
public static String ExportPrivateKey(RSACryptoServiceProvider csp)
{
TextWriter outputStream = new StringWriter();
if (csp.PublicOnly) throw new ArgumentException("CSP does not contain a private key", "csp");
var parameters = csp.ExportParameters(true);
using (var stream = new MemoryStream())
{
var writer = new BinaryWriter(stream);