Table of Contents

Class VideoEncryptor

Namespace
VisioForge.Core.VideoEncryption
Assembly
VisioForge.Core.dll

Provides cross-platform video encryption functionality using AES-128 encryption in CBC mode. This static class offers both synchronous and asynchronous methods for encrypting video files and streams using password-based encryption with optional salt values for key derivation.

public static class VideoEncryptor

Inheritance

Inherited Members

Remarks

This class implements AES-128 encryption in Cipher Block Chaining (CBC) mode with PKCS7 padding. The encryption key and initialization vector (IV) are derived from the password and salt using PBKDF2 with 10,000 iterations.

For .NET 6.0 and later, PBKDF2 uses SHA-256 as the hash algorithm. For earlier versions, the default hash algorithm of Rfc2898DeriveBytes is used.

If no salt is provided, a salt is automatically generated from the MD5 hash of the password. For maximum security, it is recommended to provide a unique, randomly generated 16-byte salt for each encryption operation.

The encrypted data can be decrypted using the VisioForge.Core.VideoEncryption.VideoDecryptor class with the same password and salt values.

Methods

Encrypt(string, string, string, byte[], EncryptionProgressCallback)

Synchronously encrypts a video file and saves the result to a new file. This method blocks the calling thread until the encryption operation is complete.

public static void Encrypt(string originalFileName, string encryptedFileName, string password, byte[] salt = null, EncryptionProgressCallback progress = null)

Parameters

originalFileName string

The path to the original unencrypted file. Must be a valid, accessible file path.

encryptedFileName string

The path where the encrypted file will be saved. Any existing file at this path will be overwritten.

password string

The password to use for encryption. This password will be required for decryption.

salt byte[]

Optional cryptographic salt for key derivation. If null, a salt is automatically derived from the password's MD5 hash. For maximum security, provide a unique, randomly generated 16-byte salt.

progress EncryptionProgressCallback

Optional callback delegate to receive progress updates during encryption, reported as percentage values from 0 to 100.

Remarks

The original file is not modified; only the output file is created or overwritten. Both file streams are properly disposed after the operation completes.

Encrypt(Stream, Stream, string, byte[], EncryptionProgressCallback)

Synchronously encrypts data from a stream and writes the result to an output stream. This method reads unencrypted data in chunks, encrypts it using AES-128 encryption in CBC mode, and writes the encrypted data to the output stream. This method blocks the calling thread until the encryption operation is complete.

public static void Encrypt(Stream originalStream, Stream encryptedStream, string password, byte[] salt = null, EncryptionProgressCallback progress = null)

Parameters

originalStream Stream

The input stream containing unencrypted data. The stream must support reading and have a defined length.

encryptedStream Stream

The output stream where encrypted data will be written. The stream must support writing.

password string

The password to use for encryption. This password will be required for decryption.

salt byte[]

Optional cryptographic salt for key derivation. If null, a salt is automatically derived from the password's MD5 hash. For maximum security, provide a unique, randomly generated 16-byte salt.

progress EncryptionProgressCallback

Optional callback delegate to receive progress updates during encryption, reported as percentage values from 0 to 100.

Remarks

The encryption is performed in chunks matching the AES block size (16 bytes). Data that doesn't fill a complete block is padded with zeros before encryption.

The streams are not disposed by this method; the caller is responsible for stream lifetime management. The cryptographic resources (AES cipher and encryptor) are properly disposed after the operation completes.

EncryptAsync(string, string, string, byte[], EncryptionProgressCallback)

Asynchronously encrypts a video file and saves the result to a new file using a specified salt.

public static Task EncryptAsync(string originalFileName, string encryptedFileName, string password, byte[] salt, EncryptionProgressCallback progress = null)

Parameters

originalFileName string

The path to the original unencrypted file. Must be a valid, accessible file path.

encryptedFileName string

The path where the encrypted file will be saved. Any existing file at this path will be overwritten.

password string

The password to use for encryption. This password will be required for decryption.

salt byte[]

The cryptographic salt for key derivation. Must be exactly 16 bytes. The same salt must be used for decryption.

progress EncryptionProgressCallback

Optional callback delegate to receive progress updates during encryption, reported as percentage values from 0 to 100.

Returns

Task

A Task representing the asynchronous encryption operation.

Remarks

This method runs the encryption operation on a background thread using Run(Action). The original file is not modified; only the output file is created or overwritten.

EncryptAsync(string, string, string, EncryptionProgressCallback)

Asynchronously encrypts a video file and saves the result to a new file with automatic salt generation.

public static Task EncryptAsync(string originalFileName, string encryptedFileName, string password, EncryptionProgressCallback progress = null)

Parameters

originalFileName string

The path to the original unencrypted file. Must be a valid, accessible file path.

encryptedFileName string

The path where the encrypted file will be saved. Any existing file at this path will be overwritten.

password string

The password to use for encryption. This password will be required for decryption.

progress EncryptionProgressCallback

Optional callback delegate to receive progress updates during encryption, reported as percentage values from 0 to 100.

Returns

Task

A Task representing the asynchronous encryption operation.

Remarks

This method automatically generates a salt from the MD5 hash of the password. The same password will generate the same salt, allowing for decryption without explicitly storing the salt.

This method runs the encryption operation on a background thread using Run(Action). The original file is not modified; only the output file is created or overwritten.

EncryptAsync(Stream, Stream, string, byte[], EncryptionProgressCallback)

Asynchronously encrypts data from a stream and writes the result to an output stream using a specified salt.

public static Task EncryptAsync(Stream originalStream, Stream encryptedStream, string password, byte[] salt, EncryptionProgressCallback progress = null)

Parameters

originalStream Stream

The input stream containing unencrypted data. The stream must support reading and have a defined length.

encryptedStream Stream

The output stream where encrypted data will be written. The stream must support writing.

password string

The password to use for encryption. This password will be required for decryption.

salt byte[]

The cryptographic salt for key derivation. Must be exactly 16 bytes. The same salt must be used for decryption.

progress EncryptionProgressCallback

Optional callback delegate to receive progress updates during encryption, reported as percentage values from 0 to 100.

Returns

Task

A Task representing the asynchronous encryption operation.

Remarks

This method runs the encryption operation on a background thread using Run(Action). The streams are not disposed by this method; the caller is responsible for stream lifetime management.

The input stream position is advanced as data is read, and the output stream receives the encrypted data.

EncryptAsync(Stream, Stream, string, EncryptionProgressCallback)

Asynchronously encrypts data from a stream and writes the result to an output stream with automatic salt generation.

public static Task EncryptAsync(Stream originalStream, Stream encryptedStream, string password, EncryptionProgressCallback progress = null)

Parameters

originalStream Stream

The input stream containing unencrypted data. The stream must support reading and have a defined length.

encryptedStream Stream

The output stream where encrypted data will be written. The stream must support writing.

password string

The password to use for encryption. This password will be required for decryption.

progress EncryptionProgressCallback

Optional callback delegate to receive progress updates during encryption, reported as percentage values from 0 to 100.

Returns

Task

A Task representing the asynchronous encryption operation.

Remarks

This method automatically generates a salt from the MD5 hash of the password. The same password will generate the same salt, allowing for decryption without explicitly storing the salt.

This method runs the encryption operation on a background thread using Run(Action). The streams are not disposed by this method; the caller is responsible for stream lifetime management.