CryptGetUserKey function (wincrypt.h)

Important  This API is deprecated. New and existing software should start using Cryptography Next Generation APIs. Microsoft may remove this API in future releases.
The CryptGetUserKey function retrieves a handle of one of a user's two public/private key pairs. This function is used only by the owner of the public/private key pairs and only when the handle of a cryptographic service provider (CSP) and its associated key container is available. If the CSP handle is not available and the user's certificate is, use CryptAcquireCertificatePrivateKey.


BOOL CryptGetUserKey(
  [in]  HCRYPTPROV hProv,
  [in]  DWORD      dwKeySpec,
  [out] HCRYPTKEY  *phUserKey


[in] hProv

HCRYPTPROV handle of a cryptographic service provider (CSP) created by a call to CryptAcquireContext.

[in] dwKeySpec

Identifies the private key to use from the key container. It can be AT_KEYEXCHANGE or AT_SIGNATURE.

Additionally, some providers allow access to other user-specific keys through this function. For details, see the documentation on the specific provider.

[out] phUserKey

A pointer to the HCRYPTKEY handle of the retrieved keys. When you have finished using the key, delete the handle by calling the CryptDestroyKey function.

Return value

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 codes prefaced by "NTE" are generated by the particular CSP being used. Some possible error codes follow.

Return code Description
One of the parameters specifies a handle that is not valid.
One of the parameters contains a value that is not valid. This is most often a pointer that is not valid.
The dwKeySpec parameter contains a value that is not valid.
The hProv parameter does not contain a valid context handle.
The key requested by the dwKeySpec parameter does not exist.


Requirement Value
Minimum supported client Windows XP [desktop apps only]
Minimum supported server Windows Server 2003 [desktop apps only]
Target Platform Windows
Header wincrypt.h
Library Advapi32.lib
DLL Advapi32.dll

See also




Key Generation and Exchange Functions