SymmetricStreamAlgorithmExtensions Class
Definition
- Namespace
- Bodu.Security.Cryptography.Extensions
- Assembly
- Bodu.Security.Cryptography.dll
- Package
- Bodu.Security.Cryptography 1.2.0
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
algorithmSymmetricStreamAlgorithmThe stream cipher to use. Must not be null.
arraybyte[]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
algorithmorarrayis 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
algorithmSymmetricStreamAlgorithmThe stream cipher to use. Must not be null.
arraybyte[]The input byte array to decrypt. Must not be null.
offsetintThe zero-based byte offset in
arrayat which to begin reading.
Returns
- byte[]
A new byte array containing the decrypted output.
Exceptions
- ArgumentNullException
algorithmorarrayis null.- ArgumentOutOfRangeException
offsetis negative or exceeds the length ofarray.
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
algorithmSymmetricStreamAlgorithmThe stream cipher to use. Must not be null.
arraybyte[]The input byte array to decrypt. Must not be null.
offsetintThe zero-based byte offset in
arrayat which to begin reading.countintThe number of bytes to decrypt.
Returns
- byte[]
A new byte array containing the decrypted output.
Exceptions
- ArgumentNullException
algorithmorarrayis null.- ArgumentOutOfRangeException
offsetorcountis negative, or the range defined byoffsetandcountexceeds the bounds ofarray.
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
algorithmSymmetricStreamAlgorithmThe stream cipher to use. Must not be null.
sourceStreamStreamThe stream to read ciphertext from. Must not be null.
targetStreamStreamThe 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, ortargetStreamis 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
algorithmSymmetricStreamAlgorithmThe stream cipher to use. Must not be null.
sourceStreamStreamThe stream to read ciphertext from. Must not be null.
targetStreamStreamThe stream to write the decrypted output to. Must not be null.
bufferSizeintThe size, in bytes, of the read buffer.
Returns
- int
The total number of ciphertext bytes read from
sourceStream.
Exceptions
- ArgumentNullException
algorithm,sourceStream, ortargetStreamis null.- ArgumentOutOfRangeException
bufferSizeis 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
algorithmSymmetricStreamAlgorithmThe stream cipher to use. Must not be null.
inputReadOnlyMemory<byte>The memory region containing the bytes to decrypt.
Returns
- byte[]
A new byte array containing the decrypted output.
Remarks
Delegates to Decrypt(SymmetricStreamAlgorithm, ReadOnlySpan<byte>).
Exceptions
- ArgumentNullException
algorithmis 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
algorithmSymmetricStreamAlgorithmThe stream cipher to use. Must not be null.
inputReadOnlySpan<byte>The span of input bytes to decrypt.
Returns
- byte[]
A new byte array containing the decrypted output.
Exceptions
- ArgumentNullException
algorithmis 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
algorithmSymmetricStreamAlgorithmThe stream cipher to use. Must not be null.
arraybyte[]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
algorithmorarrayis 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
algorithmSymmetricStreamAlgorithmThe stream cipher to use. Must not be null.
arraybyte[]The input byte array to encrypt. Must not be null.
offsetintThe zero-based byte offset in
arrayat which to begin reading.
Returns
- byte[]
A new byte array containing the encrypted output.
Exceptions
- ArgumentNullException
algorithmorarrayis null.- ArgumentOutOfRangeException
offsetis negative or exceeds the length ofarray.
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
algorithmSymmetricStreamAlgorithmThe stream cipher to use. Must not be null.
arraybyte[]The input byte array to encrypt. Must not be null.
offsetintThe zero-based byte offset in
arrayat which to begin reading.countintThe number of bytes to encrypt.
Returns
- byte[]
A new byte array containing the encrypted output.
Exceptions
- ArgumentNullException
algorithmorarrayis null.- ArgumentOutOfRangeException
offsetorcountis negative, or the range defined byoffsetandcountexceeds the bounds ofarray.
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
algorithmSymmetricStreamAlgorithmThe stream cipher to use. Must not be null.
sourceStreamStreamThe stream to read plaintext from. Must not be null.
targetStreamStreamThe 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, ortargetStreamis 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
algorithmSymmetricStreamAlgorithmThe stream cipher to use. Must not be null.
sourceStreamStreamThe stream to read plaintext from. Must not be null.
targetStreamStreamThe stream to write the encrypted output to. Must not be null.
bufferSizeintThe size, in bytes, of the read buffer.
Returns
- int
The total number of plaintext bytes read from
sourceStream.
Exceptions
- ArgumentNullException
algorithm,sourceStream, ortargetStreamis null.- ArgumentOutOfRangeException
bufferSizeis 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
algorithmSymmetricStreamAlgorithmThe stream cipher to use. Must not be null.
inputReadOnlyMemory<byte>The memory region containing the bytes to encrypt.
Returns
- byte[]
A new byte array containing the encrypted output.
Remarks
Delegates to Encrypt(SymmetricStreamAlgorithm, ReadOnlySpan<byte>).
Exceptions
- ArgumentNullException
algorithmis 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
algorithmSymmetricStreamAlgorithmThe stream cipher to use. Must not be null.
inputReadOnlySpan<byte>The span of input bytes to encrypt.
Returns
- byte[]
A new byte array containing the encrypted output.
Exceptions
- ArgumentNullException
algorithmis null.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |