zoukankan      html  css  js  c++  java
  • .Net Core 中X509Certificate2 私钥保存为 pem 的方法

    在自己签发CA证书和颁发X509证书时,私钥通过下面的方法保存为PEM 相关代码可以已经提交在了 https://github.com/q2g/q2g-helper-pem-nuget/pull/13

      public static void SavePem(this X509Certificate2 @this, out string cert, out   string privateKey)
            {
                cert = string.Empty;
                privateKey = string.Empty;
                try
                {
                    if (@this.HasPrivateKey)
                    {
    #if NET452
                        var p = (@this.PrivateKey as RSACryptoServiceProvider).ExportParameters(true);
    #else
    
                        var p = @this.GetRSAPrivateKey().ExportParameters(true);
    #endif
                        var key = new RsaPrivateCrtKeyParameters(
                            new Org.BouncyCastle.Math.BigInteger(1, p.Modulus), new Org.BouncyCastle.Math.BigInteger(1, p.Exponent), new Org.BouncyCastle.Math.BigInteger(1, p.D),
                            new Org.BouncyCastle.Math.BigInteger(1, p.P), new Org.BouncyCastle.Math.BigInteger(1, p.Q), new Org.BouncyCastle.Math.BigInteger(1, p.DP), new Org.BouncyCastle.Math.BigInteger(1, p.DQ),
                            new Org.BouncyCastle.Math.BigInteger(1, p.InverseQ));
                        using (var stringWriter = new StringWriter())
                        {
                            var pemWriter = new Org.BouncyCastle.OpenSsl.PemWriter(stringWriter);
                            pemWriter.WriteObject(key);
                            privateKey = stringWriter.GetStringBuilder().ToString();
                        }
                    }
                    cert = PemCertificateHelper.ExportCertificateToPEM(@this);
                }
                catch (Exception ex)
                {
                    throw new Exception($"Certificate could not be saved.  ", ex);
                }
            }
    
            public static void SavePem(this X509Certificate2 @this, string certFile, string privateKeyFile = null)
            {
                try
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(certFile));
                    if (!string.IsNullOrEmpty(privateKeyFile) && @this.HasPrivateKey)
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(privateKeyFile));
    #if NET452
                        var p = (@this.PrivateKey as RSACryptoServiceProvider).ExportParameters(true);
    #else
    
                        var p = @this.GetRSAPrivateKey().ExportParameters(true);
    #endif
                        var key = new RsaPrivateCrtKeyParameters(
                            new Org.BouncyCastle.Math.BigInteger(1, p.Modulus), new Org.BouncyCastle.Math.BigInteger(1, p.Exponent), new Org.BouncyCastle.Math.BigInteger(1, p.D),
                            new Org.BouncyCastle.Math.BigInteger(1, p.P), new Org.BouncyCastle.Math.BigInteger(1, p.Q), new Org.BouncyCastle.Math.BigInteger(1, p.DP), new Org.BouncyCastle.Math.BigInteger(1, p.DQ),
                            new Org.BouncyCastle.Math.BigInteger(1, p.InverseQ));
                        using (var sw = new StreamWriter(privateKeyFile))
                        {
                            var pemWriter = new Org.BouncyCastle.OpenSsl.PemWriter(sw);
                            pemWriter.WriteObject(key);
                        }
                    }
                    File.WriteAllText(certFile, PemCertificateHelper.ExportCertificateToPEM(@this));
                }
                catch (Exception ex)
                {
                    throw new Exception($"Certificate could not be saved. cert: {certFile} - key: {privateKeyFile}", ex);
                }
            }
  • 相关阅读:
    TVB西游记-观音的眼泪化作金河送唐僧回长安
    天下没有免费的午餐是什么意思
    什么样的经历、体验或者行为等能彻底的改变一个人
    看人先看什么
    python字符串中查找指定子字符串
    字符串的分隔及连接
    流媒体服务器音视频直播平台的开发为什么需要CDN?
    微信公众号小程序如何做流媒体视频直播?
    搭建专属于自己的视频流媒体直播/点播平台都需要注意哪些事项?
    音视频流媒体服务器的虚拟直播推流失败断流无法播放如何解决?
  • 原文地址:https://www.cnblogs.com/MysticBoy/p/9656096.html
Copyright © 2011-2022 走看看