Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

CryptDuplicateKey (Windows CE 5.0)

Windows CE 5.0
Send Feedback

This function makes an exact copy of a key and the state the key is in. Some keys have an associated state, for example, an initialization vector and/or a salt value.

BOOL WINAPI CryptDuplicateHash( 
  DWORD* pdwReserved, 
  DWORD dwFlags, 


[in] HCRYPTKEY handle to the key to be duplicated.
[in] Reserved for future use and must be set to NULL.
[in] Reserved for future use and must be set to zero.
[out] Pointer to the HCRYPTKEY handle to the duplicated key.

Return Values

TRUE indicates success. FALSE indicates failure. To get extended error information, call the GetLastError function.

The following table describes the common values for the GetLastError function. The error values prefaced by NTE are generated by the particular cryptographic service provider (CSP) you are using.

ERROR_CALL_NOT_IMPLEMENTEDBecause this is a new function, existing CSPs may not implement it. This error is returned if the CSP does not support this function.
ERROR_INVALID_PARAMETEROne of the parameters contains an invalid value. This is most often an illegal pointer.
NTE_BAD_KEYThe handle to the original key is not valid.


This function makes copies of keys and the exact state of the key. For example, a caller may want to encrypt two separate messages with the same key, but with different salt values. The key could be generated, a duplicate would be made with the CryptDuplicateKey function, and then the appropriate salt value would be set on each key with the CryptSetKeyParam function.

The CryptDestroyKey function must be called to destroy any keys that are created with the CryptDuplicateKey function. Destroying the original key does not cause the duplicate key to be destroyed. Once a duplicate key is made, it is separate from the original key. There is no shared state between the two keys.

Example Code

HCRYPTKEY hOriginalKey = 0;
HCRYPTKEY hDuplicateKey = 0;
DWORD dwErr;

// Generate a key.
if (!CryptGenKey(hProv, CALG_RC4, 0, &hOriginalKey))
 {printf("ERROR - CryptGenKey: %X\n", GetLastError());

// Duplicate the key.
if (!CryptDuplicateKey(hOriginalKey, NULL, 0, &hDuplicateKey))
 {printf("ERROR - CryptDuplicateKey: %X\n", GetLastError());


// Destroy the original key.
if (!CryptDestroyKey(hOriginalKey))
 {printf("ERROR - CryptDestroyKey: %X\n", GetLastError());

// Destroy the duplicate key.
if (!CryptDestroyKey(hDuplicateKey))
 {printf("ERROR - CryptDestroyKey: %X\n", GetLastError());


OS Versions: Windows CE 2.10 and later.
Header: Wincrypt.h.
Link Library: Coredll.lib.

See Also

CryptDestroyKey | CryptSetKeyParam | HCRYPTKEY

Send Feedback on this topic to the authors

Feedback FAQs

© 2006 Microsoft Corporation. All rights reserved.

© 2015 Microsoft