In this article, we are going to discuss Encrypt and decrypting a folder in C#
Here's an example implementation of how to encrypt a folder using the AES algorithm:
using System;
using System.IO;
using System.Security.Cryptography;
namespace EncryptClassInCSharp
{
public class EncryptClass
{
public static void EncryptFolder(string sourceFolder, string destinationFolder, string password)
{
// Generate a random salt value
byte[] salt = new byte[8];
using (var rng = new RNGCryptoServiceProvider())
{
rng.GetBytes(salt);
}
// Create an AES encryption algorithm with the specified password and salt
using (var aes = new AesManaged())
{
aes.KeySize = 256;
aes.BlockSize = 128;
var key = new Rfc2898DeriveBytes(password, salt, 1000).GetBytes(32);
aes.Key = key;
aes.IV = new byte[16];
aes.Padding = PaddingMode.PKCS7;
aes.Mode = CipherMode.CBC;
// Create the destination folder if it doesn't exist
if (!Directory.Exists(destinationFolder))
{
Directory.CreateDirectory(destinationFolder);
}
// Encrypt each file in the source folder and copy it to the destination folder
foreach (string filePath in Directory.GetFiles(sourceFolder))
{
using (var fsIn = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
using (var fsOut = new FileStream(Path.Combine(destinationFolder, Path.GetFileName(filePath)), FileMode.Create, FileAccess.Write))
{
// Write the salt value to the beginning of the output file
fsOut.Write(salt, 0, salt.Length);
// Encrypt the file using the AES algorithm
using (var cryptoStream = new CryptoStream(fsOut, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
fsIn.CopyTo(cryptoStream);
}
}
}
}
}
}
}
}
This implementation takes in the path of the source folder, the destination folder, and a password to use for the encryption. It generates a random salt value, creates an AES encryption algorithm with the specified password and salt, and then loops through each file in the source folder. For each file, it writes the salt value to the beginning of the output file, encrypts the file using the AES algorithm, and then writes the encrypted data to the output file in the destination folder.
Note that this implementation does not encrypt any subfolders within the specified source folder. If you need to encrypt subfolders as well, you will need to modify the implementation to recurse through all subfolders in the source folder.
To decrypt a folder that has been encrypted using the AES algorithm in C#, you can use the following implementation:
using System;
using System.IO;
using System.Security.Cryptography;
namespace EncryptClassInCSharp
{
public class DecryptFolder
{
public static void DecryptFolder(string sourceFolder, string destinationFolder, string password)
{
// Loop through each file in the source folder
foreach (string filePath in Directory.GetFiles(sourceFolder))
{
// Read the salt value from the beginning of the input file
byte[] salt = new byte[8];
using (var fsIn = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
fsIn.Read(salt, 0, salt.Length);
}
// Create an AES decryption algorithm with the specified password and salt
using (var aes = new AesManaged())
{
aes.KeySize = 256;
aes.BlockSize = 128;
var key = new Rfc2898DeriveBytes(password, salt, 1000).GetBytes(32);
aes.Key = key;
aes.IV = new byte[16];
aes.Padding = PaddingMode.PKCS7;
aes.Mode = CipherMode.CBC;
// Decrypt the input file using the AES algorithm and copy it to the destination folder
using (var fsIn = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
using (var cryptoStream = new CryptoStream(fsIn, aes.CreateDecryptor(), CryptoStreamMode.Read))
{
using (var fsOut = new FileStream(Path.Combine(destinationFolder, Path.GetFileName(filePath)), FileMode.Create, FileAccess.Write))
{
cryptoStream.CopyTo(fsOut);
}
}
}
}
}
}
}
}