Decrypt using a non-exportable private key with CryptoAPI

Viewed 941

I created RSA key pair in windows key store.

I encrypted data (a symmetric key) successfully:

HCERTSTORE hstore = ::CertOpenSystemStore(NULL, L"TestStore");
PCCERT_CONTEXT pctxt = ::CertFindCertificateInStore(hstore, X509_ASN_ENCODING, NULL, 

CERT_FIND_SUBJECT_STR, L"My Test Keys", NULL);

HCRYPTPROV hprovider = NULL;
if(!::CryptAcquireContext(&hprovider,
            NULL,
            MS_ENHANCED_PROV,
            PROV_RSA_FULL,
                    NULL/*CRYPT_NEWKEYSET*/))
{
   DWORD err = ::GetLastError();
   return 0;
}

HCRYPTKEY hkey = NULL;
if(!::CryptImportPublicKeyInfo(hprovider, 
                X509_ASN_ENCODING,
                &pctxt->pCertInfo->SubjectPublicKeyInfo,
                &hkey
                ))
{
   return 0;
}

Now I used CryptEncrypt() with HCRYPTKEY.


Next I want to decrypt the data with the private key, but it is not exportable. All the examples I've seen include importing of the keys.

How can I decrypt the data without exporting the key?

1 Answers
Related