NCryptSecretAgreement function (ncrypt.h)

The NCryptSecretAgreement function creates a secret agreement value from a private and a public key.


SECURITY_STATUS NCryptSecretAgreement(
  [in]  NCRYPT_KEY_HANDLE    hPrivKey,
  [in]  NCRYPT_KEY_HANDLE    hPubKey,
  [out] NCRYPT_SECRET_HANDLE *phAgreedSecret,
  [in]  DWORD                dwFlags


[in] hPrivKey

The handle of the private key to use to create the secret agreement value. This key and the hPubKey key must come from the same key storage provider.

[in] hPubKey

The handle of the public key to use to create the secret agreement value. This key and the hPrivKey key must come from the same key storage provider.

[out] phAgreedSecret

A pointer to an NCRYPT_SECRET_HANDLE variable that receives a handle that represents the secret agreement value. When this handle is no longer needed, release it by passing it to the NCryptFreeObject function.

[in] dwFlags

Flags that modify function behavior. This can be zero or a combination of one or more of the following values. The set of valid flags is specific to each key storage provider. The following flag applies to all providers.

Value Meaning
Requests that the key service provider (KSP) not display any user interface. If the provider must display the UI to operate, the call fails and the KSP should set the NTE_SILENT_CONTEXT error code as the last error.

Return value

Returns a status code that indicates the success or failure of the function.

Possible return codes include, but are not limited to, the following.

Return code Description
The function was successful.
The hPrivKey or the hPubKey parameter is not valid.
One or more parameters are not valid.
A memory allocation failure occurred.


A service must not call this function from its StartService Function. If a service calls this function from its StartService function, a deadlock can occur, and the service may stop responding.


Requirement Value
Minimum supported client Windows Vista [desktop apps | UWP apps]
Minimum supported server Windows Server 2008 [desktop apps | UWP apps]
Target Platform Windows
Header ncrypt.h
Library Ncrypt.lib
DLL Ncrypt.dll

See also