UID | title | description | helpviewer_keywords | old-location | tech.root | ms.assetid | ms.date | ms.keywords | req.header | req.include-header | req.target-type | req.target-min-winverclnt | req.target-min-winversvr | req.kmdf-ver | req.umdf-ver | req.ddi-compliance | req.unicode-ansi | req.idl | req.max-support | req.namespace | req.assembly | req.type-library | req.lib | req.dll | req.irql | targetos | req.typenames | req.redist | ms.custom | f1_keywords | dev_langs | topic_type | api_type | api_location | api_name | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
NF:wincrypt.CryptDuplicateKey |
CryptDuplicateKey function (wincrypt.h) |
Makes an exact copy of a key and the state of the key. |
|
security\cryptduplicatekey.htm |
security |
c5658008-7c92-4877-871a-a764884efd79 |
12/05/2018 |
CryptDuplicateKey, CryptDuplicateKey function [Security], _crypto2_cryptduplicatekey, security.cryptduplicatekey, wincrypt/CryptDuplicateKey |
wincrypt.h |
Windows |
Windows XP [desktop apps only] |
Windows Server 2003 [desktop apps only] |
Advapi32.lib |
Advapi32.dll |
Windows |
19H1 |
|
|
|
|
|
|
A handle to the key to be duplicated.
Reserved for future use and must be NULL.
Reserved for future use and must be zero.
Address of the handle to the duplicated key. When you have finished using the key, release the handle by calling the CryptDestroyKey function.
If the function succeeds, the return value is nonzero (TRUE).
If the function fails, the return value is zero (FALSE). For extended error information, call GetLastError.
The error code prefaced by "NTE" is generated by the particular CSP being used. Some possible error codes are listed in the following table.
Return code | Description |
---|---|
|
Because this is a new function, existing CSPs might not implement it. This error is returned if the CSP does not support this function. |
|
One of the parameters contains a value that is not valid. This is most often a pointer that is not valid. |
|
A handle to the original key is not valid. |
CryptDuplicateKey makes a copy of a key and the exact state of the key. One scenario when this function can be used is when an application needs to encrypt two separate messages with the same key but with different salt values. The original key is generated and then a duplicate key is made by using the CryptDuplicateKey function. The different salt values are then set on the original and duplicate keys with separate calls to the CryptSetKeyParam function.
CryptDestroyKey must be called to destroy any keys that are created by using CryptDuplicateKey. Destroying the original key does not cause the duplicate key to be destroyed. After a duplicate key is made, it is separate from the original key. There is no shared state between the two keys.
The following example shows the creation of a session key that is a duplicate of an existing session key. For an example that includes the complete context for this example, see Example C Program: Duplicating a Session Key.
//--------------------------------------------------------------------
// Declare and initialize variables.
HCRYPTKEY hDuplicateKey;
// Duplicate the key. hOriginalKey is a previously
// assigned HCRYPTKEY variable.
if (CryptDuplicateKey(
hOriginalKey,
NULL,
0,
&hDuplicateKey))
{
printf("The session key has been duplicated. \n");
}
else
{
printf("Error using CryptDuplicateKey.\n");
exit(1);
}
// Insert code that uses the duplicate key here.
// When you have finished using the key, the handle must be released.
if (CryptDestroyKey(hDuplicateKey))
{
printf("The handle has been released.\n");
}
else
{
printf("The handle could not be released.\n");
}