有人知道我如何使用非托管 c++执行以下c#代码的等效操作,即通过拇指指纹从X509证书存储查询证书吗?
var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
var allCerts = store.Certificates;
foreach (var certificate in from X509Certificate2 certificate in allCerts
where certificate.Thumbprint != null
&& certificate.Thumbprint.Equals(thumbprint, StringComparison.OrdinalIgnoreCase)
select certificate)
{
return certificate;
}
Thanks in advance
戴夫为了完成您想要的,您必须查看Win32 CryptAPI库。它不会像。net那样简单。查看CertOpenStore和certfindcertificatestore
您需要打开一个证书存储并将其传递给CertFindCertificateStore,创建一个结构来保存您想要用于查找证书的任何标准。您可以使用序列号、签名等。
HCERTSTORE hSysStore = NULL;
PCCERT_CONTEXT pDesiredCert = NULL;
if(hSysStore = CertOpenStore(
CERT_STORE_PROV_SYSTEM, // The store provider type
0, // The encoding type is
// not needed
NULL, // Use the default HCRYPTPROV
CERT_SYSTEM_STORE_CURRENT_USER, // Set the store location in a
// registry location
L"MY" // The store name as a Unicode
// string
))
{
//We have our store, let's do stuff with it
if (pDesiredCert = CertFindCertificateInStore(.....) { ..... }
}
else
{
//Error stuff
}
您需要#include <Wincrypt.h>
和#include <windows.h>
来自certfindcertificatestore的代码。将不能在较新的系统上工作。在较新的Windows版本中,证书的名称或主题采用Unicode格式,每个字符使用2个字节。下一行:
LPCSTR lpszCertSubject = (LPCSTR) "Cert_subject_1";
必须改为:
LPCWSTR lpszCertSubject = (LPCWSTR ) L"Cert_subject_1";
或
LPCTSTR lpszCertSubject = (LPCTSTR ) _T"Cert_subject_1"; // add #include <tchar.h>