CPDuplicateKey

The CPDuplicateKey function is used to make an exact copy of a key and the state the key is in. Some keys have an associated state such as example, an initialization vector or a salt value.

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

Parameters

hProv
Handle to a particular key container (or "context") within the CSP. This handle is obtained via a call to CPAcquireContext.
hKey
The handle to the key to be duplicated.
pdwReserved
This parameter is reserved for future use and must be NULL.
dwFlags
Flag values. This parameter is reserved for future use and should always be zero.
phKey
Address of the handle to the duplicated key.

Return Values

If the function succeeds, the return value is TRUE; Otherwise, return FALSE. When FALSE is returned, the appropriate error code (see the following table) must be set via SetLastError.

Error Description
ERROR_CALL_NOT_IMPLEMENTED Because 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_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

CPDuplicateKey is used to make 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 CPDuplicateKey function, and then the appropriate salt value would be 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 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.

See Also

CPDestroyKey, CryptDuplicateKey