运行以下代码时的问题:
X509Certificate2 cert = new X509Certificate2(@"C:\file.p12", "password", X509KeyStorageFlags.Exportable);
RSACryptoServiceProvider crypt = (RSACryptoServiceProvider)cert.PrivateKey;
我收到以下错误:Keyset does not exist
。
我没有将证书添加到商店,是否需要能够访问私钥?
答案 0 :(得分:6)
将X509KeyStorageFlags.PersistKeySet选项添加到X509Certificate2构造函数的最后一个参数中。否则,当它加载p12文件时,它将不会加载私钥。具体做法是:
X509Certificate2 cert = new X509Certificate2(@"C:\file.p12", "password",
X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
RSACryptoServiceProvider crypt = (RSACryptoServiceProvider)cert.PrivateKey;
如果失败,则可能是存储密钥的文件权限问题。有关说明和示例,请参阅X509Certificate - Keyset does not exist。