SymmetricAlgorithm.EncryptCbc Method

Definition

Overloads

EncryptCbc(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, PaddingMode)

Encrypts data into the specified buffer, using CBC mode with the specified padding mode.

EncryptCbc(Byte[], Byte[], PaddingMode)

Encrypts data using CBC mode with the specified padding mode.

EncryptCbc(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, PaddingMode)

Encrypts data using CBC mode with the specified padding mode.

EncryptCbc(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, PaddingMode)

Source:
SymmetricAlgorithm.cs
Source:
SymmetricAlgorithm.cs
Source:
SymmetricAlgorithm.cs

Encrypts data into the specified buffer, using CBC mode with the specified padding mode.

C#
public int EncryptCbc(ReadOnlySpan<byte> plaintext, ReadOnlySpan<byte> iv, Span<byte> destination, System.Security.Cryptography.PaddingMode paddingMode = System.Security.Cryptography.PaddingMode.PKCS7);

Parameters

plaintext
ReadOnlySpan<Byte>

The data to encrypt.

iv
ReadOnlySpan<Byte>

The initialization vector.

destination
Span<Byte>

The buffer to receive the ciphertext data.

paddingMode
PaddingMode

The padding mode used to produce the ciphertext and remove during decryption.

Returns

The total number of bytes written to destination.

Exceptions

paddingMode is not a valid padding mode.

iv is the incorrect length. Callers are expected to pass an initialization vector that is exactly BlockSize in length, converted to bytes (BlockSize / 8).

The plaintext could not be encrypted successfully.

The buffer in destination is too small to hold the ciphertext data.

Remarks

This method's behavior is defined by TryEncryptCbcCore(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32).

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

EncryptCbc(Byte[], Byte[], PaddingMode)

Source:
SymmetricAlgorithm.cs
Source:
SymmetricAlgorithm.cs
Source:
SymmetricAlgorithm.cs

Encrypts data using CBC mode with the specified padding mode.

C#
public byte[] EncryptCbc(byte[] plaintext, byte[] iv, System.Security.Cryptography.PaddingMode paddingMode = System.Security.Cryptography.PaddingMode.PKCS7);

Parameters

plaintext
Byte[]

The data to encrypt.

iv
Byte[]

The initialization vector.

paddingMode
PaddingMode

The padding mode used to produce the ciphertext and remove during decryption.

Returns

Byte[]

The encrypted ciphertext data.

Exceptions

plaintext or iv is null.

paddingMode is not a valid padding mode.

iv is the incorrect length. Callers are expected to pass an initialization vector that is exactly BlockSize in length, converted to bytes (BlockSize / 8).

The plaintext could not be encrypted successfully.

Remarks

This method's behavior is defined by TryEncryptCbcCore(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32).

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

EncryptCbc(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, PaddingMode)

Source:
SymmetricAlgorithm.cs
Source:
SymmetricAlgorithm.cs
Source:
SymmetricAlgorithm.cs

Encrypts data using CBC mode with the specified padding mode.

C#
public byte[] EncryptCbc(ReadOnlySpan<byte> plaintext, ReadOnlySpan<byte> iv, System.Security.Cryptography.PaddingMode paddingMode = System.Security.Cryptography.PaddingMode.PKCS7);

Parameters

plaintext
ReadOnlySpan<Byte>

The data to encrypt.

iv
ReadOnlySpan<Byte>

The initialization vector.

paddingMode
PaddingMode

The padding mode used to produce the ciphertext and remove during decryption.

Returns

Byte[]

The encrypted ciphertext data.

Exceptions

paddingMode is not a valid padding mode.

iv is the incorrect length. Callers are expected to pass an initialization vector that is exactly BlockSize in length, converted to bytes (BlockSize / 8).

The plaintext could not be encrypted successfully.

Remarks

This method's behavior is defined by TryEncryptCbcCore(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32).

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9