I have imported a crt file to Personal Certificates in Local Computer.
I can extract the same public key using base 64 encoded X509 .CER option using MMC.
My goal is to programmatically get this information using win32 apis.
So far I have tried below but the data stored in base64Cert does not match the public key.
Any ideas on what I can try to get the correct data ?
HCERTSTORE hCertStore;
PCCERT_CONTEXT pCertContext = NULL;
if (!(hCertStore = CertOpenStore(
CERT_STORE_PROV_SYSTEM,
MY_ENCODING_TYPE,
NULL,
CERT_SYSTEM_STORE_LOCAL_MACHINE,
L"MY")))
{
std::cout<<"The MY system store did not open."<< std::endl;
}
while (pCertContext = CertEnumCertificatesInStore(
hCertStore,
pCertContext))
{
LPTSTR pszString;
LPTSTR pszName;
DWORD cbSize;
CERT_BLOB blobEncodedName;
PBYTE pbPKEY = NULL;
DWORD iPKEYSize;
bool ret = CryptDecodeObjectEx(X509_ASN_ENCODING,
RSA_CSP_PUBLICKEYBLOB,
pCertContext->pCertInfo->SubjectPublicKeyInfo.PublicKey.pbData,
pCertContext->pCertInfo->SubjectPublicKeyInfo.PublicKey.cbData,
CRYPT_ENCODE_ALLOC_FLAG,
NULL,
&pbPKEY,
&iPKEYSize);
CERT_CONTEXT certcontext;
certcontext.pbCertEncoded = pbPKEY;
certcontext.cbCertEncoded = iPKEYSize;
DWORD dwData;
if (!CryptBinaryToStringA(certcontext.pbCertEncoded, certcontext.cbCertEncoded, CRYPT_STRING_BASE64HEADER, nullptr, &dwData))
{
std::cout << "error n";
}
std::string base64Cert(dwData, '');
CryptBinaryToStringA(certcontext.pbCertEncoded, certcontext.cbCertEncoded, CRYPT_STRING_BASE64HEADER, &base64Cert[0], &dwData);
std::cout << base64Cert << "n";
}