【问题标题】:How Createx509certificate2 given certificate bytes and private key - dnx50Createx509certificate2 如何给定证书字节和私钥 - dnx50
【发布时间】:2016-07-17 13:58:15
【问题描述】:

基本上,我使用 LetsEncrypt 服务来获取证书字节 [],我可以将其转换为 X509Certificate2,但随后它丢失了私钥,然后在 SSLStream 上使用它。我将私钥作为 RSAParameters,但也可以将其转换为字节 [],但我似乎无法找到将 2 放在同一个 X509Certificate2 中的方法,因此我可以将其用于 SSLStream 上的 AuthenticateAsServer。据我所知,您用于 dotnet 4 的方法似乎不适用于 dnx50。我的工作示例将是完美的,我想将解决方案保留在 dnx50 中,因为我想将它部署到 linux 和 windows 盒子。

基本上尝试做类似于Convert Certificate and Private Key to .PFX programatically in C# 的事情,但只使用私钥创建 X509,尽管保存将是我的下一个任务。

据我目前所知,我认为 dnx50 不允许您创建证书对象,然后像 dotnet 4 那样向其添加私钥。相反,我认为我需要传入一个包含两者的文件或字节 [] 才能正常工作,但我不知道如何将我的 2 字节数组合并在一起或格式化它们。

【问题讨论】:

    标签: x509certificate2 dnx50


    【解决方案1】:

    终于找到了解决办法。不理想,但它有效。基本上,它使用 bouncyCastle 创建一个 pfx 流,然后您可以将其读入以加载带有证书的私钥。为此,我在 CoreCLR 上使用了 nuget 包 Portable.BouncyCastle:1.8.1,并将以下代码放入帮助程序类中。

    public X509Certificate2 CreateX509Certificate2(RSAParameters keys, byte[] certificateBytes, string friendlyName)
        {
    
            if (string.IsNullOrWhiteSpace(friendlyName))
            {
                friendlyName = "default";
            }
    
            var store = new Pkcs12Store();
            var convertedKeys = GetRsaKeyPair(keys);
            var certificate = new X509CertificateParser().ReadCertificate(certificateBytes);
    
            store.SetKeyEntry(friendlyName, new AsymmetricKeyEntry(convertedKeys.Private), new X509CertificateEntry[] { new X509CertificateEntry(certificate)});
            using (MemoryStream ms = new MemoryStream())
            {
                var random = new SecureRandom();
                string password = random.Next().ToString() + random.Next().ToString() + random.Next().ToString();
                store.Save(ms, password.ToCharArray(), random);
                var cert = new X509Certificate2(ms.ToArray(), password, X509KeyStorageFlags.Exportable);
                return cert;
            }
        }
    
    
        private AsymmetricCipherKeyPair GetRsaKeyPair(
           RSAParameters rp)
        {
            BigInteger modulus = new BigInteger(1, rp.Modulus);
            BigInteger pubExp = new BigInteger(1, rp.Exponent);
    
            RsaKeyParameters pubKey = new RsaKeyParameters(
                false,
                modulus,
                pubExp);
    
            RsaPrivateCrtKeyParameters privKey = new RsaPrivateCrtKeyParameters(
                modulus,
                pubExp,
                new BigInteger(1, rp.D),
                new BigInteger(1, rp.P),
                new BigInteger(1, rp.Q),
                new BigInteger(1, rp.DP),
                new BigInteger(1, rp.DQ),
                new BigInteger(1, rp.InverseQ));
    
            return new AsymmetricCipherKeyPair(pubKey, privKey);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-26
      • 1970-01-01
      • 2021-06-08
      相关资源
      最近更新 更多