如何从存储中读取私钥
本文关键字:读取 私钥 存储 | 更新日期: 2023-09-27 17:59:39
我在本地计算机商店里有一台x509。我如何在C#中阅读?我需要用这种方式获取私钥
RSACryptoServiceProvider rsa = (RSACryptoServiceProvider).cert.PrivateKey()
这将从"我的"(个人)存储中获得证书。
var store = new X509Store(StoreName.My);
store.Open(OpenFlags.ReadOnly);
var certificate = store.Certificates.Single(c => c.Thumbprint == "Whatever-Your-Thumbprint-Is");
store.Close();
到那时,您将拥有X509Certificate2,并且可以从中访问PrivateKey属性。
public void AddCertificate()
{
if (this.ClientCertificates == null)
{
this.ClientCertificates = new X509CertificateCollection();
}
X509Certificate cert = new X509Certificate(path, pass, X509KeyStorageFlags.MachineKeySet);
this.ClientCertificates.Add(cert);
}