BCryptEncrypt function (bcrypt.h) - Win32 apps (2024)

  • Article

The BCryptEncrypt function encrypts a block of data.

Syntax

NTSTATUS BCryptEncrypt( [in, out] BCRYPT_KEY_HANDLE hKey, [in] PUCHAR pbInput, [in] ULONG cbInput, [in, optional] VOID *pPaddingInfo, [in, out, optional] PUCHAR pbIV, [in] ULONG cbIV, [out, optional] PUCHAR pbOutput, [in] ULONG cbOutput, [out] ULONG *pcbResult, [in] ULONG dwFlags);

Parameters

[in, out] hKey

The handle of the key to use to encrypt the data. This handle is obtained from one of the key creation functions, such as BCryptGenerateSymmetricKey, BCryptGenerateKeyPair, or BCryptImportKey.

[in] pbInput

The address of a buffer that contains the plaintext to be encrypted. The cbInput parameter contains the size of the plaintext 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. This parameter is only used with asymmetric keys and authenticated encryption modes. If an authenticated encryption mode is used, this parameter must point to a BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO structure. If asymmetric keys are used, the type of structure this parameter points to is determined by the value of the dwFlags parameter. Otherwise, the parameter must be set to NULL.

[in, out, optional] pbIV

The address of a buffer that contains the initialization vector (IV) to use during encryption. The cbIV parameter contains the size of this buffer. This function will modify the contents of this buffer. If you need to reuse the IV later, make sure you make a copy of this buffer before calling this function.

This parameter is optional and can be NULL if no IV is used.

The required size of the IV can be obtained by calling the BCryptGetProperty function to get the BCRYPT_BLOCK_LENGTH property. This will provide the size of a block for the algorithm, which is also the size of the IV.

[in] cbIV

The size, in bytes, of the pbIV buffer.

[out, optional] pbOutput

The address of the buffer that receives the ciphertext produced by this function. The cbOutput parameter contains the size of this buffer. For more information, see Remarks.

If this parameter is NULL, the BCryptEncrypt function calculates the size needed for the ciphertext of the data passed in the pbInput parameter. In this case, the location pointed to by the pcbResult parameter contains this size, and the function returns STATUS_SUCCESS. The pPaddingInfo parameter is not modified.

If the values of both the pbOutput and pbInput parameters are NULL, an error is returned unless an authenticated encryption algorithm is in use. In the latter case, the call is treated as an authenticated encryption call with zero length data, and the authentication tag is returned in the pPaddingInfo 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 ULONG 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

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

If the key is a symmetric key, this can be zero or the following value.

ValueMeaning
BCRYPT_BLOCK_PADDING
Allows the encryption algorithm to pad the data to the next block size. If this flag is not specified, the size of the plaintext specified in the cbInput parameter must be a multiple of the algorithm's block size.

The block size can be obtained by calling the BCryptGetProperty function to get the BCRYPT_BLOCK_LENGTH property for the key. This will provide the size of a block for the algorithm.

This flag must not be used with the authenticated encryption modes (AES-CCM and AES-GCM).

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

ValueMeaning
BCRYPT_PAD_NONE
Do not use any padding. The pPaddingInfo parameter is not used. The size of the plaintext specified in the cbInput parameter must be a multiple of the algorithm's block size.
BCRYPT_PAD_OAEP
Use the Optimal Asymmetric Encryption Padding (OAEP) scheme. The pPaddingInfo parameter is a pointer to a BCRYPT_OAEP_PADDING_INFO structure.
BCRYPT_PAD_PKCS1
The data will be padded with a random number to round out the block size. The pPaddingInfo parameter is not used.

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 codeDescription
STATUS_SUCCESS
The function was successful.
STATUS_BUFFER_TOO_SMALL
The size specified by the cbOutput parameter is not large enough to hold the ciphertext.
STATUS_INVALID_BUFFER_SIZE
The cbInput parameter is not a multiple of the algorithm's block size and the BCRYPT_BLOCK_PADDING or the BCRYPT_PAD_NONE flag was not specified in the dwFlags parameter.
STATUS_INVALID_HANDLE
The key handle in the hKey parameter is not valid.
STATUS_INVALID_PARAMETER
One or more parameters are not valid.
STATUS_NOT_SUPPORTED
The algorithm does not support encryption.

Remarks

The pbInput and pbOutput parameters can be equal. 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. If pbInput and pbOutput are not equal then the two buffers may not overlap.

Depending on what processor modes a provider supports, BCryptEncrypt can be called either from user mode or kernel mode. Kernel mode callers can execute either at PASSIVE_LEVEL IRQL or DISPATCH_LEVEL IRQL. If the current IRQL level is DISPATCH_LEVEL, the handle provided in the hKey parameter must be derived from an algorithm handle returned by a provider that was opened with the BCRYPT_PROV_DISPATCH flag, and any pointers passed to the BCryptEncrypt function must refer to nonpaged (or locked) memory.

To call this function in kernel mode, use Cng.lib, which is part of the Driver Development Kit (DDK). Windows Server2008 and WindowsVista:To call this function in kernel mode, use Ksecdd.lib.

Requirements

RequirementValue
Minimum supported clientWindowsVista [desktop apps | UWP apps]
Minimum supported serverWindows Server2008 [desktop apps | UWP apps]
Target PlatformWindows
Headerbcrypt.h
LibraryBcrypt.lib
DLLBcrypt.dll

See also

BCryptDecrypt

BCryptEncrypt function (bcrypt.h) - Win32 apps (2024)
Top Articles
Introducing ERC1155D: The most efficient non-fungible token contract in existence.
How to buy Matic without gas?
Netronline Taxes
Lorton Transfer Station
Hannaford Weekly Flyer Manchester Nh
Pieology Nutrition Calculator Mobile
Cad Calls Meriden Ct
Jeremy Corbell Twitter
Tyrunt
Mohawkind Docagent
Nordstrom Rack Glendale Photos
O'reilly's In Monroe Georgia
Select The Best Reagents For The Reaction Below.
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Socket Exception Dunkin
Babyrainbow Private
Fairy Liquid Near Me
Christina Khalil Forum
How Much Are Tb Tests At Cvs
Brett Cooper Wikifeet
Po Box 35691 Canton Oh
Google Flights Missoula
Why do rebates take so long to process?
Military life insurance and survivor benefits | USAGov
Wics News Springfield Il
Greenville Sc Greyhound
Rust Belt Revival Auctions
8000 Cranberry Springs Drive Suite 2M600
Marquette Gas Prices
Publix Near 12401 International Drive
Pulitzer And Tony Winning Play About A Mathematical Genius Crossword
Alternatieven - Acteamo - WebCatalog
49S Results Coral
417-990-0201
Club Keno Drawings
3473372961
Warn Notice Va
Boneyard Barbers
“Los nuevos desafíos socioculturales” Identidad, Educación, Mujeres Científicas, Política y Sustentabilidad
Fototour verlassener Fliegerhorst Schönwald [Lost Place Brandenburg]
Mixer grinder buying guide: Everything you need to know before choosing between a traditional and bullet mixer grinder
Google Flights Orlando
How Does The Common App Work? A Guide To The Common App
Improving curriculum alignment and achieving learning goals by making the curriculum visible | Semantic Scholar
Silive Obituary
Windshield Repair & Auto Glass Replacement in Texas| Safelite
'The Nun II' Ending Explained: Does the Immortal Valak Die This Time?
Avance Primary Care Morrisville
A Man Called Otto Showtimes Near Cinemark Greeley Mall
Glowforge Forum
Land of Samurai: One Piece’s Wano Kuni Arc Explained
Inloggen bij AH Sam - E-Overheid
Latest Posts
Article information

Author: Delena Feil

Last Updated:

Views: 5772

Rating: 4.4 / 5 (45 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Delena Feil

Birthday: 1998-08-29

Address: 747 Lubowitz Run, Sidmouth, HI 90646-5543

Phone: +99513241752844

Job: Design Supervisor

Hobby: Digital arts, Lacemaking, Air sports, Running, Scouting, Shooting, Puzzles

Introduction: My name is Delena Feil, I am a clean, splendid, calm, fancy, jolly, bright, faithful person who loves writing and wants to share my knowledge and understanding with you.