NCryptEncrypt function (ncrypt.h)

The NCryptEncrypt function encrypts a block of data.


  [in]           NCRYPT_KEY_HANDLE hKey,
  [in]           PBYTE             pbInput,
  [in]           DWORD             cbInput,
  [in, optional] VOID              *pPaddingInfo,
  [out]          PBYTE             pbOutput,
  [in]           DWORD             cbOutput,
  [out]          DWORD             *pcbResult,
  [in]           DWORD             dwFlags


[in] hKey

The handle of the key to use to encrypt the data.

[in] pbInput

The address of a buffer that contains the data to be encrypted. The cbInput parameter contains the size of the data to encrypt. For more information, see Remarks.

[in] cbInput

The number of bytes in the pbInput buffer to encrypt.

[in, optional] pPaddingInfo

A pointer to a structure that contains padding information. The actual type of structure this parameter points to depends on the value of the dwFlags parameter. This parameter is only used with asymmetric keys and must be NULL otherwise.

[out] pbOutput

The address of a buffer that will receive the encrypted data produced by this function. The cbOutput parameter contains the size of this buffer. For more information, see Remarks.

If this parameter is NULL, this function will calculate the size needed for the encrypted data and return the size in the location pointed to by the pcbResult parameter.

[in] cbOutput

The size, in bytes, of the pbOutput buffer. This parameter is ignored if the pbOutput parameter is NULL.

[out] pcbResult

A pointer to a DWORD variable that receives the number of bytes copied to the pbOutput buffer. If pbOutput is NULL, this receives the size, in bytes, required for the ciphertext.

[in] dwFlags

Flags that modify function behavior. The allowed set of flags depends on the type of key specified by the hKey parameter.

If the key is an asymmetric key, this can be one of the following values.

Value Meaning
Do not use any padding. The pPaddingInfo parameter is not used.

If you specify the NCRYPT_NO_PADDING_FLAG, then the NCryptEncrypt function only encrypts the first N bits, where N is the length of the key that was passed as the hKey parameter. Any bits after the first N bits are ignored.

Use the Optimal Asymmetric Encryption Padding (OAEP) scheme. The pPaddingInfo parameter is a pointer to a BCRYPT_OAEP_PADDING_INFO structure.
The data will be padded with a random number to round out the block size. The pPaddingInfo parameter is not used.
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 dwFlags parameter contains a value that is not valid.
The key identified by the hKey parameter has not been finalized or is incomplete.
The size specified by the cbOutput parameter is not large enough to hold the encrypted data.
The hKey parameter is not valid.
One or more parameters are not valid.


The pbInput and pbOutput parameters can point to the same buffer. In this case, this function will perform the encryption in place. It is possible that the encrypted data size will be larger than the unencrypted data size, so the buffer must be large enough to hold the encrypted data.

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.


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