This topic has not yet been rated - Rate this topic

CryptDestroyKey function

Applies to: desktop apps only

The CryptDestroyKey function releases the handle referenced by the hKey parameter. After a key handle has been released, it is no longer valid and cannot be used again.

If the handle refers to a session key, or to a public key that has been imported into the cryptographic service provider (CSP) through CryptImportKey, this function destroys the key and frees the memory that the key used. Many CSPs overwrite the memory where the key was held before freeing it. However, the underlying public/private key pair is not destroyed by this function. Only the handle is destroyed.

Syntax

BOOL WINAPI CryptDestroyKey(
  __in  HCRYPTKEY hKey
);

Parameters

hKey [in]

The handle of the key to be destroyed.

Return value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. For extended error information, call GetLastError.

The error codes prefaced by "NTE" are generated by the particular CSP being used. Some possible error codes are listed in the following table.

Return codeDescription
ERROR_BUSY

The key object specified by hKey is currently being used and cannot be destroyed.

ERROR_INVALID_HANDLE

The hKey parameter specifies a handle that is not valid.

ERROR_INVALID_PARAMETER

The hKey parameter contains a value that is not valid.

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.

 

Remarks

Keys take up both operating system's memory space and the CSP's memory space. Some CSPs are implemented in hardware with limited memory resources. Applications must destroy all keys with the CryptDestroyKey function when they are finished with them.

All key handles that have been created or imported by using a specific CSP must be destroyed before that CSP handle is released with the CryptReleaseContext function.

Examples

For an example that uses the CryptDestroyKey function, see Example C Program: Creating and Hashing a Session Key.

Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Wincrypt.h

Library

Advapi32.lib

DLL

Advapi32.dll

See also

Key Generation and Exchange Functions
CryptDeriveKey
CryptGenKey
CryptGetUserKey
CryptImportKey

 

 

Send comments about this topic to Microsoft

Build date: 3/6/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ