X509Certificate2 p12 是否需要存储? [英] X509Certificate2 p12 is store required?
问题描述
运行以下代码时的问题:
Question when running the following code:
X509Certificate2 cert = new X509Certificate2(@"C:\file.p12", "password", X509KeyStorageFlags.Exportable);
RSACryptoServiceProvider crypt = (RSACryptoServiceProvider)cert.PrivateKey;
我收到以下错误:Keyset does not exist
.
我没有将证书添加到商店,这是访问私钥的必要条件吗?
I have not added the certificate to a store, is this required to be able to access the private key?
推荐答案
添加 X509KeyStorageFlags.PersistKeySet 选项到 X509Certificate2 构造函数的最后一个参数.否则,当它加载 p12 文件时,它不会加载私钥.具体:
Add the X509KeyStorageFlags.PersistKeySet option to the last argument of the X509Certificate2 constructor. Otherwise, when it loads the p12 file, it will not load the private key. Specifically:
X509Certificate2 cert = new X509Certificate2(@"C:\file.p12", "password",
X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
RSACryptoServiceProvider crypt = (RSACryptoServiceProvider)cert.PrivateKey;
如果失败,则可能是密钥存储位置的文件权限问题.有关说明和示例,请参阅 X509Certificate - 密钥集不存在.
If that fails, it may be a file permission issue on where the key is stored. See X509Certificate - Keyset does not exist for an explanation and example.
这篇关于X509Certificate2 p12 是否需要存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!