Exclusive Clothing Sale On Flipkart

Get up to 50% off on our latest collection.

Shop Now

Encrypt and decrypt a folder in C#

 In this article, we are going to discuss Encrypt and decrypting a folder in C#

Encrypt a folder in C#

Encrypting a folder in C# can be accomplished using a combination of the System.Security.Cryptography namespace and the System.IO namespace.

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);
                            }
                        }
                    }
                }
            }
        }
    }
}

This implementation takes in the path of the source folder, the destination folder, and the same password used to encrypt the folder. It loops through each file in the source folder and reads the salt value from the beginning of the input file. It then creates an AES decryption algorithm with the specified password and salt and decrypts the input file using the AES algorithm. Finally, it writes the decrypted data to the output file in the destination folder.

Note that this implementation assumes that all files in the source folder have been encrypted using the same password and salt value. If this is not the case, you will need to modify the implementation to read the salt value from each input file and use it to create the appropriate decryption algorithm.

C#

Share this

Related Posts

Previous
Next Post »

1 comments:

Write comments
XIU
May 8, 2023 at 6:24 PM delete

Why not create a unique salt per file, the decrypting part needs no changes for it, but at the moment each file is using the same salt so also the same AES key, this opens it up for oracle attacks.

Reply
avatar

Exclusive Clothing Sale On Flipkart

Get up to 50% off on our latest collection.

Shop Now