Platform SDK: Cryptography

CPDuplicateKey

The CPDuplicateKey function makes an exact copy of a key. Some keys have an associated state such as an initialization vector or a salt value. If a key with an associated state is duplicated, the associated state is also copied.

BOOL CPDuplicateKey(
  HCRYPTPROV hUID,     // in
  HCRYPTKEY hKey,      // in
  DWORD *pdwReserved,  // in
  DWORD dwFlags,       // in
  HCRYPTKEY *phKey     // out
);

Parameters

hUID
Handle to a particular key container within the CSP. This handle is obtained with a call to CPAcquireContext.
hKey
Handle of the key to be duplicated.
pdwReserved
This parameter is reserved for future use and must be NULL.
dwFlags
This parameter is reserved for future use and must be zero.
phKey
Address for the handle to the duplicated key.

Return Values

If the function succeeds, the return value is TRUE.

If the function fails, the return value is FALSE, and the appropriate error code from the following table must be set using SetLastError.

Error Description
ERROR_CALL_
NOT_IMPLEMENTED
Because this is a new function, existing CSPs might not implement it. This error is returned if the CSP does not support this function.
ERROR_INVALID_
PARAMETER
One of the parameters contains an invalid value. This is most often an illegal pointer.
NTE_BAD_KEY The handle to the original key is not valid.

Remarks

The CPDuplicateKey function is used to make a copy of a key and the exact state of that key. For example, a caller can encrypt two separate messages with the same key, but with different salt values. A key is generated, a duplicate is made with the CPDuplicateKey function, and then different appropriate salt values are set on each key with the CPSetKeyParam function.

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

Requirements

  Windows NT/2000: Requires Windows NT 4.0 or later.
  Windows 95/98: Requires Windows 95 OSR2 or later (or Windows 95 with Internet Explorer 3.02 or later).
  Header: Declared in Wincrypt.h.

See Also

CPDestroyKey, CryptDuplicateKey