如何从商店读取私钥

时间:2016-08-02 13:07:59

标签: c# rsa x509certificate2

我的本​​地电脑商店里有一台x509。我怎么读C#? 我需要使用这种方式获取私钥

RSACryptoServiceProvider rsa = (RSACryptoServiceProvider).cert.PrivateKey()

2 个答案:

答案 0 :(得分:1)

这将从“我的”(个人)商店获得证书。

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属性。

答案 1 :(得分:0)

 public void AddCertificate()
    {
        if (this.ClientCertificates == null)
        {
            this.ClientCertificates = new X509CertificateCollection();
        }
        X509Certificate cert = new X509Certificate(path, pass, X509KeyStorageFlags.MachineKeySet);
        this.ClientCertificates.Add(cert);
    }