Table of Contents

SymmetricStreamAlgorithmExtensions Class

Definition

Namespace
Bodu.Security.Cryptography.Extensions
Assembly
Bodu.Security.Cryptography.dll
Package
Bodu.Security.Cryptography 1.2.0
Source
SymmetricStreamAlgorithmExtensions.Decrypt.cs

Extends SymmetricStreamAlgorithm with one-shot encrypt/decrypt of buffers and stream-to-stream pipelines, mirroring the convenience surface offered for block ciphers.

public static class SymmetricStreamAlgorithmExtensions
Inheritance
SymmetricStreamAlgorithmExtensions
Inherited Members

Remarks

SymmetricStreamAlgorithm exposes the building blocks - CreateEncryptor, CreateDecryptor , and CryptoStream interop - but stops short of the operations callers actually invoke: "encrypt this byte array" or "decrypt this stream into that one". This class collapses the usual setup (instantiate the transform, drain the source, dispose in order) into a single call per scenario, with overloads aligned to the input shape.

Because an additive stream cipher is self-inverse, Encrypt and Decrypt perform the identical XOR operation; the two names exist purely to make calling code read naturally. Every overload uses the algorithm's current Key and Nonce. Streams are not disposed by these methods, and SymmetricStreamAlgorithm instances are not thread-safe; share them only behind explicit synchronization.

using Bodu.Security.Cryptography;
using Bodu.Security.Cryptography.Extensions;

using var chacha = new ChaCha20();
chacha.GenerateKey();
chacha.GenerateNonce();

byte[] ciphertext = chacha.Encrypt(plaintext);
byte[] roundTrip  = chacha.Decrypt(ciphertext);

Methods

Decrypt(SymmetricStreamAlgorithm, byte[])

Decrypts the entire contents of a byte array using the specified stream cipher.

public static byte[] Decrypt(this SymmetricStreamAlgorithm algorithm, byte[] array)

Parameters

algorithm SymmetricStreamAlgorithm

The stream cipher to use. Must not be null.

array byte[]

The input byte array to decrypt. Must not be null.

Returns

byte[]

A new byte array containing the decrypted output.

Remarks

Because an additive stream cipher is self-inverse, this performs the identical XOR operation as Encrypt(SymmetricStreamAlgorithm, byte[]). Equivalent to calling Decrypt(array, 0, array.Length).

Exceptions

ArgumentNullException

algorithm or array is null.

Decrypt(SymmetricStreamAlgorithm, byte[], int)

Decrypts a portion of a byte array beginning at the specified offset and continuing to the end of the array.

public static byte[] Decrypt(this SymmetricStreamAlgorithm algorithm, byte[] array, int offset)

Parameters

algorithm SymmetricStreamAlgorithm

The stream cipher to use. Must not be null.

array byte[]

The input byte array to decrypt. Must not be null.

offset int

The zero-based byte offset in array at which to begin reading.

Returns

byte[]

A new byte array containing the decrypted output.

Exceptions

ArgumentNullException

algorithm or array is null.

ArgumentOutOfRangeException

offset is negative or exceeds the length of array.

Decrypt(SymmetricStreamAlgorithm, byte[], int, int)

Decrypts a contiguous region of a byte array using the specified stream cipher.

public static byte[] Decrypt(this SymmetricStreamAlgorithm algorithm, byte[] array, int offset, int count)

Parameters

algorithm SymmetricStreamAlgorithm

The stream cipher to use. Must not be null.

array byte[]

The input byte array to decrypt. Must not be null.

offset int

The zero-based byte offset in array at which to begin reading.

count int

The number of bytes to decrypt.

Returns

byte[]

A new byte array containing the decrypted output.

Exceptions

ArgumentNullException

algorithm or array is null.

ArgumentOutOfRangeException

offset or count is negative, or the range defined by offset and count exceeds the bounds of array.

Decrypt(SymmetricStreamAlgorithm, Stream, Stream)

Decrypts data read from a source stream and writes the decrypted output to a target stream, using the default buffer size.

public static int Decrypt(this SymmetricStreamAlgorithm algorithm, Stream sourceStream, Stream targetStream)

Parameters

algorithm SymmetricStreamAlgorithm

The stream cipher to use. Must not be null.

sourceStream Stream

The stream to read ciphertext from. Must not be null.

targetStream Stream

The stream to write the decrypted output to. Must not be null.

Returns

int

The total number of ciphertext bytes read from sourceStream.

Exceptions

ArgumentNullException

algorithm, sourceStream, or targetStream is null.

Decrypt(SymmetricStreamAlgorithm, Stream, Stream, int)

Decrypts data read from a source stream and writes the decrypted output to a target stream, using the specified buffer size.

public static int Decrypt(this SymmetricStreamAlgorithm algorithm, Stream sourceStream, Stream targetStream, int bufferSize)

Parameters

algorithm SymmetricStreamAlgorithm

The stream cipher to use. Must not be null.

sourceStream Stream

The stream to read ciphertext from. Must not be null.

targetStream Stream

The stream to write the decrypted output to. Must not be null.

bufferSize int

The size, in bytes, of the read buffer.

Returns

int

The total number of ciphertext bytes read from sourceStream.

Exceptions

ArgumentNullException

algorithm, sourceStream, or targetStream is null.

ArgumentOutOfRangeException

bufferSize is less than or equal to zero.

Decrypt(SymmetricStreamAlgorithm, ReadOnlyMemory<byte>)

Decrypts a read-only memory region using the specified stream cipher.

public static byte[] Decrypt(this SymmetricStreamAlgorithm algorithm, ReadOnlyMemory<byte> input)

Parameters

algorithm SymmetricStreamAlgorithm

The stream cipher to use. Must not be null.

input ReadOnlyMemory<byte>

The memory region containing the bytes to decrypt.

Returns

byte[]

A new byte array containing the decrypted output.

Remarks

Exceptions

ArgumentNullException

algorithm is null.

Decrypt(SymmetricStreamAlgorithm, ReadOnlySpan<byte>)

Decrypts a read-only span of bytes using the specified stream cipher.

public static byte[] Decrypt(this SymmetricStreamAlgorithm algorithm, ReadOnlySpan<byte> input)

Parameters

algorithm SymmetricStreamAlgorithm

The stream cipher to use. Must not be null.

input ReadOnlySpan<byte>

The span of input bytes to decrypt.

Returns

byte[]

A new byte array containing the decrypted output.

Exceptions

ArgumentNullException

algorithm is null.

Encrypt(SymmetricStreamAlgorithm, byte[])

Encrypts the entire contents of a byte array using the specified stream cipher.

public static byte[] Encrypt(this SymmetricStreamAlgorithm algorithm, byte[] array)

Parameters

algorithm SymmetricStreamAlgorithm

The stream cipher to use. Must not be null.

array byte[]

The input byte array to encrypt. Must not be null.

Returns

byte[]

A new byte array containing the encrypted output.

Remarks

Equivalent to calling Encrypt(array, 0, array.Length).

Exceptions

ArgumentNullException

algorithm or array is null.

Encrypt(SymmetricStreamAlgorithm, byte[], int)

Encrypts a portion of a byte array beginning at the specified offset and continuing to the end of the array.

public static byte[] Encrypt(this SymmetricStreamAlgorithm algorithm, byte[] array, int offset)

Parameters

algorithm SymmetricStreamAlgorithm

The stream cipher to use. Must not be null.

array byte[]

The input byte array to encrypt. Must not be null.

offset int

The zero-based byte offset in array at which to begin reading.

Returns

byte[]

A new byte array containing the encrypted output.

Exceptions

ArgumentNullException

algorithm or array is null.

ArgumentOutOfRangeException

offset is negative or exceeds the length of array.

Encrypt(SymmetricStreamAlgorithm, byte[], int, int)

Encrypts a contiguous region of a byte array using the specified stream cipher.

public static byte[] Encrypt(this SymmetricStreamAlgorithm algorithm, byte[] array, int offset, int count)

Parameters

algorithm SymmetricStreamAlgorithm

The stream cipher to use. Must not be null.

array byte[]

The input byte array to encrypt. Must not be null.

offset int

The zero-based byte offset in array at which to begin reading.

count int

The number of bytes to encrypt.

Returns

byte[]

A new byte array containing the encrypted output.

Exceptions

ArgumentNullException

algorithm or array is null.

ArgumentOutOfRangeException

offset or count is negative, or the range defined by offset and count exceeds the bounds of array.

Encrypt(SymmetricStreamAlgorithm, Stream, Stream)

Encrypts data read from a source stream and writes the encrypted output to a target stream, using the default buffer size.

public static int Encrypt(this SymmetricStreamAlgorithm algorithm, Stream sourceStream, Stream targetStream)

Parameters

algorithm SymmetricStreamAlgorithm

The stream cipher to use. Must not be null.

sourceStream Stream

The stream to read plaintext from. Must not be null.

targetStream Stream

The stream to write the encrypted output to. Must not be null.

Returns

int

The total number of plaintext bytes read from sourceStream.

Exceptions

ArgumentNullException

algorithm, sourceStream, or targetStream is null.

Encrypt(SymmetricStreamAlgorithm, Stream, Stream, int)

Encrypts data read from a source stream and writes the encrypted output to a target stream, using the specified buffer size.

public static int Encrypt(this SymmetricStreamAlgorithm algorithm, Stream sourceStream, Stream targetStream, int bufferSize)

Parameters

algorithm SymmetricStreamAlgorithm

The stream cipher to use. Must not be null.

sourceStream Stream

The stream to read plaintext from. Must not be null.

targetStream Stream

The stream to write the encrypted output to. Must not be null.

bufferSize int

The size, in bytes, of the read buffer.

Returns

int

The total number of plaintext bytes read from sourceStream.

Exceptions

ArgumentNullException

algorithm, sourceStream, or targetStream is null.

ArgumentOutOfRangeException

bufferSize is less than or equal to zero.

Encrypt(SymmetricStreamAlgorithm, ReadOnlyMemory<byte>)

Encrypts a read-only memory region using the specified stream cipher.

public static byte[] Encrypt(this SymmetricStreamAlgorithm algorithm, ReadOnlyMemory<byte> input)

Parameters

algorithm SymmetricStreamAlgorithm

The stream cipher to use. Must not be null.

input ReadOnlyMemory<byte>

The memory region containing the bytes to encrypt.

Returns

byte[]

A new byte array containing the encrypted output.

Remarks

Exceptions

ArgumentNullException

algorithm is null.

Encrypt(SymmetricStreamAlgorithm, ReadOnlySpan<byte>)

Encrypts a read-only span of bytes using the specified stream cipher.

public static byte[] Encrypt(this SymmetricStreamAlgorithm algorithm, ReadOnlySpan<byte> input)

Parameters

algorithm SymmetricStreamAlgorithm

The stream cipher to use. Must not be null.

input ReadOnlySpan<byte>

The span of input bytes to encrypt.

Returns

byte[]

A new byte array containing the encrypted output.

Exceptions

ArgumentNullException

algorithm is null.

Applies to

ProductVersions
.NET8, 10