Encryption is the process of translating plain text data (plaintext) into something that appears to be random and meaningless (cipher text). Decryption is the process of converting cipher text back to plaintext.
For smaller and simpler files (like Text files) we can use DES algorithm, but for other type of files such as a video, we’ll have to use AES or some other algorithm. For this example we’ll be using AES algorithm. AES is a new cryptographic algorithm that can be used to protect electronic data. Typically, AES is an iterative, symmetric-key block cipher that can use keys of 128, 192, and 256 bits, and encrypts and decrypts data in blocks of 128 bits (16 bytes). Unlike public-key ciphers, which use a pair of keys, symmetric-key ciphers use the same key to encrypt and decrypt data. Encrypted data returned by block ciphers have the same number of bits that the input data had. Iterative ciphers use a loop structure that repeatedly performs permutations and substitutions of the input data. Our aim in this article is to Encrypt/Decrypt Video files.
Encrypt File Code Snippet:
private const string SKey = "_?73^?dVT3st5har3";
private const string SaltKey = "!2S@LT&KT3st5har3EY";
private const int Iterations = 1042; // Recommendation is >= 1000
public static void EncryptFile(string srcFilename, string destFilename)
{
var aes = new AesManaged();
aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
aes.KeySize = aes.LegalKeySizes[0].MaxSize;
var salt = GetBytes(SaltKey);
var key = new Rfc2898DeriveBytes(SKey, salt, Iterations);
aes.Key = key.GetBytes(aes.KeySize / 8);
aes.IV = key.GetBytes(aes.BlockSize / 8);
aes.Mode = CipherMode.CBC;
ICryptoTransform transform = aes.CreateEncryptor(aes.Key, aes.IV);
using (var dest = new FileStream(destFilename, FileMode.CreateNew, FileAccess.Write, FileShare.None))
{
using (var cryptoStream = new CryptoStream(destination, transform, CryptoStreamMode.Write))
{
using (var source = new FileStream(srcFilename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
source.CopyTo(cryptoStream);
}
}
}
}
Decrypt File Code Snippet:
public static void DecryptFile(string srcFilename, string destFilename)
{
var aes = new AesManaged();
aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
aes.KeySize = aes.LegalKeySizes[0].MaxSize;
var salt = GetBytes(SaltKey);
var key = new Rfc2898DeriveBytes(SKey, salt, Iterations);
aes.Key = key.GetBytes(aes.KeySize / 8);
aes.IV = key.GetBytes(aes.BlockSize / 8);
aes.Mode = CipherMode.CBC;
ICryptoTransform transform = aes.CreateDecryptor(aes.Key, aes.IV);
using (var dest = new FileStream(destFilename, FileMode.CreateNew, FileAccess.Write, FileShare.None))
{
using (var cryptoStream = new CryptoStream(dest, transform, CryptoStreamMode.Write))
{
try
{
using (var source = new FileStream(srcFilename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
source.CopyTo(cryptoStream);
}
}
catch (CryptographicException exception)
{
throw new ApplicationException("Decryption failed.",exception);
}
}
}
}
This code snippet could be used for other file format as well, but my aim for writing this article is to use this for Video file encryption and it has worked well for .wmv, .mpg.
Please let me know if you face any issues with other file types as well.