The CryptImportKey function is used to transfer a cryptographic key from a key blob to the CSP.
BOOL CRYPTFUNC CryptImportKey(
HCRYPTPROV hProv, | |
BYTE *pbData, | |
DWORD dwDataLen, | |
HCRYPTKEY hImpKey, | |
DWORD dwFlags, | |
HCRYPTKEY *phKey | |
); |
This key blob consists of a standard header followed by the encrypted key.
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.
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.
Error |
Description |
ERROR_INVALID_HANDLE |
One of the parameters specifies an invalid handle. |
ERROR_INVALID_PARAMETER |
One of the parameters contains an invalid value. This is most often an illegal pointer. |
NTE_BAD_ALGID |
The simple key blob you are trying to import is not encrypted with the expected key exchange algorithm. |
NTE_BAD_DATA |
The algorithm that works with the public key you are trying to import is not supported by this CSP. |
NTE_BAD_FLAGS |
The dwFlags parameter is nonzero. |
NTE_BAD_TYPE |
The key blob type is not supported by this CSP and is possibly invalid. |
NTE_BAD_UID |
The hProv parameter does not contain a valid context handle. |
NTE_BAD_VER |
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; HCRYPTPROV hProv = 0; HCRYPTKEY hKey = 0; 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()); free(pbKeyBlob); goto done; } // Free memory. free(pbKeyBlob); // Use ‘hKey’ to perform cryptographic operations. ... done: // Destroy session key. if(hKey) CryptDestroyKey(hKey); // Release provider handle. if(hProv) CryptReleaseContext(hProv, 0);
CryptAcquireContext, CryptDestroyKey, CryptExportKey
file: /Techref/os/win/api/win32/func/src/f12_13.htm, 7KB, , updated: 2000/4/7 11:19, local time: 2024/11/27 10:31,
3.140.185.194:LOG IN
|
©2024 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? <A HREF="http://sxlist.com/techref/os/win/api/win32/func/src/f12_13.htm"> CryptImportKey Release 2]</A> |
Did you find what you needed? |
Welcome to sxlist.com!sales, advertizing, & kind contributors just like you! Please don't rip/copy (here's why Copies of the site on CD are available at minimal cost. |
Welcome to sxlist.com! |
.