Prev Next

CryptDestroyKey info  Overview  Group

The CryptDestroyKey function releases the handle referenced by the hKey parameter. Once a key handle has been released, it becomes invalid and cannot be used again.

If the handle refers to a session key, or to a public key that has been imported into the CSP through CryptImportKey, this function destroys the key and frees the memory that the key occupied. Many CSPs will scrub the memory where the key was held before freeing it.

On the other hand, if the handle refers to a public/private key pair (obtained from CryptGetUserKey), the underlying key pair is not destroyed by this function. Only the handle is destroyed.

BOOL CRYPTFUNC CryptDestroyKey(

    HCRYPTKEY hKey

   );

Parameters

hKey
[in] A handle to the key to be destroyed.

Remarks

Keys take up memory in both the operating system’s memory space and the CSP’s memory space. Some CSPs will be implemented in hardware with very limited memory resources. For this reason, it is important that applications destroy all keys with the CryptDestroyKey function when they are finished with them.

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.

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_KEY

The hKey parameter does not contain a valid handle to a key.

NTE_BAD_UID

The CSP context that was specified when the key was created cannot be found.

Example

See the “Example” section in the CryptGenKey function.

See Also

CryptDeriveKey, CryptGenKey, CryptGetUserKey, CryptImportKey