Windows API - Win32
一组适用于桌面和服务器应用程序的核心 Windows 应用程序编程接口 (API)。 以前称为 Win32 API。
105 个问题
我使用Below程序检索存储在LOCAL_MACHINE下的“1-ksp-digitalid-MSKSP 27th June”证书的私钥。它给了我错误0x80090016即键集不存在错误。在“属性”上,您可以看到对话框,上面写着“您有与此证书对应的私钥”
//Open Local Machine store
NCRYPT_KEY_HANDLE hKey = 0;
PCCERT_CONTEXT pCertContext = NULL;
HCERTSTORE hCertStore = NULL;
DWORD dwKeySpec;
BOOL bCallerFreeProv;
hCertStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, NULL, CERT_SYSTEM_STORE_LOCAL_MACHINE, L"My");
// Enumerate all certificates.
while (pCertContext = CertFindCertificateInStore(hCertStore, X509_ASN_ENCODING, 0, CERT_FIND_ANY, NULL, pCertContext))
{
std::cout << "\n=====================================================================================================\n";
// Print the certificate's subject name.
wchar_t szName[256];
if (CertGetNameString(pCertContext, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL, szName, 256))
{
}
if (lstrcmpiW(szName, L"1-ksp-digitalid-MSKSP 27th June") != 0)
continue;
std::wcout << "Certificate Subject Name: " << szName << std::endl;
// Get the private key
if (!CryptAcquireCertificatePrivateKey(pCertContext, CRYPT_ACQUIRE_PREFER_NCRYPT_KEY_FLAG, NULL, &hKey, &dwKeySpec, &bCallerFreeProv))
{
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessageW(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPWSTR)&lpMsgBuf,
0, NULL);
wprintf(L"Error acquiring private key for %s. Error: %lu %s\n", szName, dw, lpMsgBuf);
//goto Cleanup;
}
}