The CryptExportKey function is used to export cryptographic keys out of a cryptographic service provider in a secure manner.
Declare Function CryptExportKey Lib "advapi32.dll" (ByVal hKey As Long, ByVal hExpKey As Long, ByVal dwBlobType As Long, ByVal dwFlags As Long, ByVal pbData As String, pdwDataLen As Long) As Long
|Operating Systems Supported|
|Windows NT 4.0 or later; Windows 95 OSR2 or later (or Windows 95 with Internet Explorer 3.02 or later)|
[in] A handle to the key to be exported.
[in] A handle to a cryptographic key belonging to the destination user. The key data within the key blob created is encrypted using this key. This ensures that only the destination user will be able to make use of the key blob.
Most often, this will be the key exchange public key of the destination user. However, certain protocols require that a session key belonging to the destination user be used for this purpose.
If the key blob type specified by dwBlobType is PUBLICKEYBLOB, then this parameter is unused and should be set to zero.
If the key blob specified by dwBlobType is PRIVATEKEYBLOB, then this is typically a handle to a session key that is to be used to encrypt the key blob. Some CSPs allow this parameter to be zero, in which case the application should encrypt the private key blob manually so as to protect it.
[in] The type of key blob to be exported. This must currently be one of the following constants. These constants are discussed in the section Exchanging Cryptographic Keys.
[in] The flag values. This parameter is reserved for future use and should always be zero.
[out] The buffer that the function places the key blob in. The required size for this buffer can be determined by calling CryptExportKey with NULL for this parameter.
As a rule, SIMPLEBLOBs will be 256 bytes or less, PUBLICKEYBLOBs will be 1000 bytes or less, and PRIVATEKEYBLOBS will be 5000 bytes or less.
[in/out] The address of the key blob data length. Before calling this function, the caller should set this parameter to the length, in bytes, of the pbData buffer. Upon return, this address will contain the number of bytes taken up by the key blob.
If the buffer specified by pbData is not large enough to hold the data, the function returns the ERROR_MORE_DATA error code (through GetLastError) and stores the required buffer size, in bytes, into the variable pointed to by pdwDataLen.
If pbData is NULL, then no error is returned and the function stores the size of the data, in bytes, in the variable pointed to by pdwDataLen.
|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 dwFlags parameter is nonzero.
One or both of the keys specified by hKey and hExpKey are invalid.
You do not have permission to export the key. That is, when the hKey key was created, the CRYPT_EXPORTABLE flag was not specified.
The key blob type specified by dwBlobType is PUBLICKEYBLOB, but hExpKey does not contain a public key handle.
The dwBlobType parameter specifies an unknown blob type.
The CSP context that was specified when the hKey key was created cannot be found.
A session key is being exported and the hExpKey parameter does not specify a public key.