我有一个密钥库,里面全是证书。另一个人告诉我证书有一个私钥,但当我签入我的代码时,我从来没有看到任何私钥。所有这些密钥都在vs6和cryptapi中使用,我使用的是c#。
我创建了自己的证书,并使用这里的http://www.source-code.biz/snippets/vbasic/3.htm方法确保它有一个私钥
当我将它导入到同一个密钥库中,然后运行我的代码时,证书显示它确实有一个私钥,而密钥库中的所有其他证书都没有(根据我的代码)。
为了能够使用此密钥库中证书中的私钥,是否需要设置某些标志或设置某些内容?
public void testForPrivateKey(string keystorename)
{
X509Store teststore = new X509Store(keystorename);
teststore.Open(OpenFlags.ReadOnly);
foreach (X509Certificate2 cert in teststore.Certificates)
{
if(cert.HasPrivateKey)
{
System.Diagnostics.Debug.WriteLine("Private key found certificate name: {0}", cert.Subject);
}
}
store.Close();
}发布于 2015-01-07 12:00:58
我得到了同样的问题,但在调查之后得到了解决:如果证书有私钥,那么它也有属性CERT_KEY_SPEC_PROP_ID。
DWORD keySpec;
DWORD len = 4;
BOOL ret = CertGetCertificateContextProperty(pCertContext, CERT_KEY_SPEC_PROP_ID, &keySpec, &len);
if ( !ret ){
//has no private key
}发布于 2012-02-11 06:41:58
密钥与证书相关联。你不能选择一个,这是由你使用的证书决定的。请参阅MSDN上的EncryptTo/DecryptTo: Encryption in .NET with CryptoAPI Certificate Stores文章。
https://stackoverflow.com/questions/9235596
复制相似问题