using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
string privateKeyPEM = @"
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIDfkJ9UjKHSzEbw0smXwGlyOwsw/NT+y/0Ke7h+AseLkoAoGCCqGSM49
AwEHoUQDQgAER0V+sLtPEIU1hSkHszfbvwxnq7JYL4JQQOIe1yJXBly5ONzaD0aE
0OzoutaLXgfKi/WFoINS9REPVA2AtjW5bA==
-----END EC PRIVATE KEY-----
string subjectDN = "CN=test, C=Japan";
DateTime startDate = DateTime.Now;
DateTime endDate = startDate.AddDays(365);
ECDsa privateKey = CryptoHelper.GetECDsaFromPEM(privateKeyPEM);
X509Certificate2 certificate = CryptoHelper.GenerateSelfSignedCertificate(privateKey, subjectDN, startDate, endDate);
string base64Certificate = Convert.ToBase64String(certificate.Export(X509ContentType.Cert));
Console.WriteLine(base64Certificate + "\nFinished");
public static class CryptoHelper
public static ECDsa GetECDsaFromPEM(string privateKeyPEM)
ECDsa ecdsaPrivateKey = ECDsa.Create();
ecdsaPrivateKey.ImportFromPem(privateKeyPEM);
public static X509Certificate2 GenerateSelfSignedCertificate(ECDsa privateKey, string subjectDN, DateTime startDate, DateTime endDate)
var request = new CertificateRequest(subjectDN, privateKey, HashAlgorithmName.SHA256);
request.CertificateExtensions.Add(
new X509BasicConstraintsExtension(false, false, 0, false));
var certificate = request.CreateSelfSigned(startDate, endDate);
return new X509Certificate2(certificate.Export(X509ContentType.Pfx), "", X509KeyStorageFlags.Exportable);