.NET 6 で試したら、標準ライブラリだけで自己署名証明書を生成できた。
using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; var ecdsa = ECDsa.Create(); var request = new CertificateRequest( subjectName: "cn=hogefuga", key: ecdsa, hashAlgorithm: HashAlgorithmName.SHA256); // 自己署名証明書を生成 var certificate = request.CreateSelfSigned( notBefore: DateTimeOffset.Now, notAfter: DateTimeOffset.Now.AddYears(1)); // PFX を出力 var exportedPfx = certificate.Export(X509ContentType.Pfx, "P@ssw0rd"); var filePath = Path.Combine(AppContext.BaseDirectory, "mycert.pfx"); File.WriteAllBytes(filePath, exportedPfx); // 公開鍵のみを Base64 エンコードして出力 var exportedCert = certificate.Export(X509ContentType.Cert); var base64EncodedCert = Convert.ToBase64String( exportedCert, Base64FormattingOptions.InsertLineBreaks); Console.WriteLine("-----BEGIN CERTIFICATE-----"); Console.WriteLine(base64EncodedCert); Console.WriteLine("-----END CERTIFICATE-----");