简体   繁体   English

已添加公钥后,如何向 X509Certificate2 添加私钥?

[英]How can I add a private key to X509Certificate2 after I have already added the public key?

My steps are:我的步骤是:

  1. Create X509Certificate2 with public key:使用公钥创建X509Certificate2

     X509Certificate2 clientCertificate = new X509Certificate2("public key certificate blob as byte[]");
  2. How do I want to load the private key blob to clientCertificate ?我想如何将私钥 blob 加载到clientCertificate

It looks like you can not import RSA private key with just .net framework tools.看起来您无法仅使用 .net 框架工具导入 RSA 私钥。

Check out this thread How to read a PEM RSA private key from .NET .查看此线程如何从 .NET 读取 PEM RSA 私钥

Just in case anyone like me and up looking at this old post when searching for how to generate X509Certificate2 from pem fil/private key:以防万一有人像我一样在搜索如何从 pem fil/私钥生成 X509Certificate2 时查看这篇旧帖子:

The .Net 5.0 framework has a very simplified approach to this: .Net 5.0 框架对此有一个非常简化的方法:

var certPem = File.ReadAllText("cert.pem");
var eccPem = File.ReadAllText("ecc.pem");
var cert = X509Certificate2.CreateFromPem(certPem, eccPem);

(source: https://www.scottbrady91.com/C-Sharp/PEM-Loading-in-dotnet-core-and-dotnet ) (来源: https : //www.scottbrady91.com/C-Sharp/PEM-Loading-in-dotnet-core-and-dotnet

Also see: How to import PKCS#8 RSA privateKey (created by OpenSSL) in C#另请参阅: 如何在 C# 中导入 PKCS#8 RSA privateKey(由 OpenSSL 创建)

It includes a link to Mono's source code which can read PKCS#8 / PEM files and return an RSA instance from it.它包含指向 Mono 源代码的链接,该链接可以读取 PKCS#8 / PEM 文件并从中返回RSA实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM