【问题标题】:Extract file .cer and file .key from a pfx certificate从 pfx 证书中提取文件 .cer 和文件 .key
【发布时间】:2020-09-16 14:17:38
【问题描述】:

我使用此代码从 .pfx 证书开始创建 .key 文件,一切正常。

            ////////////////////////////////////////////Create file .key

            string cParK = " pkcs12 -in certificate.pfx -out certificate.key -nocerts -nodes -nomacver -password pass:" + cPass;

            System.Diagnostics.ProcessStartInfo startInfo2 = new System.Diagnostics.ProcessStartInfo("openssl.exe", cParK);
            startInfo2.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            startInfo2.CreateNoWindow = true;

            System.Diagnostics.Process oProcess2 = System.Diagnostics.Process.Start(startInfo2);
            oProcess2.StartInfo.CreateNoWindow = true;
            oProcess2.WaitForExit();

有没有什么方法可以在不使用 openssl 而只使用 c# 的情况下创建相同的文件?

【问题讨论】:

  • 是的。 Open SSL 正在创建已签名的 xml 证书。如果您的 windows 版本支持加密模式,您可以在 c# 代码中创建相同的代码。

标签: c# openssl x509certificate


【解决方案1】:

关于第一个问题,我也找到了导出密钥的解决方案,也许对那些说不容易的人也有用,反而很容易。只需关注这个帖子。 Solution of the problem

【讨论】:

    【解决方案2】:

    目前我已经找到了一种提取 .cer 文件的方法:

            System.Security.Cryptography.X509Certificates.X509Certificate2 oCert =
            new System.Security.Cryptography.X509Certificates.X509Certificate2("certificate.pfx", "123456");
    
            Byte[] aCert = oCert.Export(System.Security.Cryptography.X509Certificates.X509ContentType.Cert);
            File.WriteAllBytes("certificato.cer", aCert);
    

    (我删除了原问题的这部分)

    【讨论】:

    • 投了反对票,因为这个答案不符合 OP 的要求。此答案仅提取公共证书,但 OP 需要获取 PKCS#1/8 私钥。在 .NET 中没有直接的方法可以做到这一点。
    • 在删除投票之前,也许最好阅读并理解:(我删除了原始问题的这一部分)是我的问题!
    • 然后你问了一个问题并回答了另一个问题。问题是关于 .key 文件,它代表私钥文件。代码 sn-p 证明了这一点。
    猜你喜欢
    • 2018-07-05
    • 2011-04-22
    • 1970-01-01
    • 2011-06-08
    • 2013-09-10
    • 2020-06-11
    • 1970-01-01
    • 2010-10-08
    相关资源
    最近更新 更多