please dont rip this site Prev Next

CryptImportKey info  Overview  Group

The CryptImportKey function is used to transfer a cryptographic key from a key blob to the CSP.



    BYTE *pbData,

    DWORD dwDataLen,

    HCRYPTKEY hImpKey,

    DWORD dwFlags,

    HCRYPTKEY *phKey



[in] A handle to the application’s CSP. An application obtains this handle using the CryptAcquireContext function.
[in] The buffer containing the key blob. This key blob was generated by the CryptExportKey function, either by this same application or by another application running on a distant computer.

This key blob consists of a standard header followed by the encrypted key.

[in] The length, in bytes, of the key blob.
[in] The meaning of this parameter differs, depending on the CSP type and the type of key blob being imported.

If the key blob is not encrypted (for example, a PUBLICKEYBLOB) or if the key blob is encrypted with the key exchange key pair (for example, a SIMPLEBLOB), then this parameter is not used, and should be zero.

If a signed key blob is being imported, this key is used to validate the signature of the key blob. In this case, this parameter should contain a handle to the key exchange public key of the party that created the key blob.

If the key blob is encrypted with a session key (for example, an encrypted PRIVATEKEYBLOB), then this parameter should contain a handle to this session key.

[in] The flag values. This parameter is currently only used when a public/private key pair is being imported into the CSP (in the form of a PRIVATEKEYBLOB). In this case, if the CRYPT_EXPORTABLE flag is set then subsequent applications will be permitted to export the private key back out of the CSP.
[out] The address to which the function copies a handle to the key that was imported.

Return Values

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To retrieve extended error information, use the GetLastError function.

The following table lists the error codes most commonly returned by the GetLastError function. The error codes prefaced by “NTE” are generated by the particular CSP you are using.




One of the parameters specifies an invalid handle.


One of the parameters contains an invalid value. This is most often an illegal pointer.


The simple key blob you are trying to import is not encrypted with the expected key exchange algorithm.


The algorithm that works with the public key you are trying to import is not supported by this CSP.


The dwFlags parameter is nonzero.


The key blob type is not supported by this CSP and is possibly invalid.


The hProv parameter does not contain a valid context handle.


The key blob’s version number does not match the CSP version. This usually indicates that the CSP needs to be upgraded.


#include <wincrypt.h>

FILE *hSourceFile = NULL;



BYTE *pbKeyBlob = NULL;

DWORD dwBlobLen;

// Open file, getting file handle ‘hSourceFile’.


// Get handle to the default provider.

if(!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0)) {

    printf("Error %x during CryptAcquireContext!\n", GetLastError());

    goto done;


// Read key blob length from file and allocate memory.

fread(&dwBlobLen, sizeof(DWORD), 1, hSourceFile);

pbKeyBlob = malloc(dwBlobLen);

// Read key blob from file.

fread(pbKeyBlob, 1, dwBlobLen, hSourceFile);

// Import key blob into CSP.

if(!CryptImportKey(hProv, pbKeyBlob, dwBlobLen, 0, 0, &hKey)) {

    printf("Error %x during CryptImportKey!\n", GetLastError());


    goto done;


// Free memory.


// Use ‘hKey’ to perform cryptographic operations.



// Destroy session key.

if(hKey) CryptDestroyKey(hKey);

// Release provider handle.

if(hProv) CryptReleaseContext(hProv, 0);


See Also

CryptAcquireContext, CryptDestroyKey, CryptExportKey 

See also:

file: /Techref/os/win/api/win32/func/src/f12_13.htm, 7KB, , updated: 2000/4/7 12:19, local time: 2022/5/21 14:36,

 ©2022 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF=""> CryptImportKey Release 2]</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

  PICList 2022 contributors:
o List host: MIT, Site host, Top posters @20220521
* Page Editors: James Newton, David Cary, and YOU!
* Roman Black of Black Robotics donates from sales of Linistep stepper controller kits.
* Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters.
* Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated!
* Contributors: Richard Seriani, Sr.

Welcome to!