title: How to encrypt files in a folder
body: <p>I'm making a program to encrypt all files in a folder to not encrypt files 1 by 1.</p>
  
  <p>So, here is my code for the AES 256 encryption</p>
  
  <pre><code>public class CoreEncryption
      {
          public static byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
          {
              byte[] encryptedBytes = null;
  
              // Set your salt here, change it to meet your flavor:
              // The salt bytes must be at least 8 bytes.
              byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
  
              using (MemoryStream ms = new MemoryStream())
              {
                  using (RijndaelManaged AES = new RijndaelManaged())
                  {
                      AES.KeySize = 256;
                      AES.BlockSize = 128;
  
                      var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
                      AES.Key = key.GetBytes(AES.KeySize / 8);
                      AES.IV = key.GetBytes(AES.BlockSize / 8);
  
                      AES.Mode = CipherMode.CBC;
  
                      using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
                      {
                          cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
                          cs.Close();
                      }
                      encryptedBytes = ms.ToArray();
                  }
              }
  
              return encryptedBytes;
          }
      }
  
      public class CoreDecryption
      {
          public static byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
          {
              byte[] decryptedBytes = null;
  
              // Set your salt here, change it to meet your flavor:
              // The salt bytes must be at least 8 bytes.
              byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
  
              using (MemoryStream ms = new MemoryStream())
              {
                  using (RijndaelManaged AES = new RijndaelManaged())
                  {
                      AES.KeySize = 256;
                      AES.BlockSize = 128;
  
                      var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
                      AES.Key = key.GetBytes(AES.KeySize / 8);
                      AES.IV = key.GetBytes(AES.BlockSize / 8);
  
                      AES.Mode = CipherMode.CBC;
  
                      using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
                      {
                          cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
                          cs.Close();
                      }
                      decryptedBytes = ms.ToArray();
                  }
              }
  
              return decryptedBytes;
          }
      }
  </code></pre>
  
  <p>And this to start encrypting and decrypting files</p>
  
  <pre><code>public class EncryptionFile
      {
          public void EncryptFile(string file, string password)
          {
  
              byte[] bytesToBeEncrypted = File.ReadAllBytes(file);
              byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
  
              // Hash the password with SHA256
              passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
  
              byte[] bytesEncrypted = CoreEncryption.AES_Encrypt(bytesToBeEncrypted, passwordBytes);
  
              string fileEncrypted = file;
  
              File.WriteAllBytes(fileEncrypted, bytesEncrypted);
          }
      }
  
      public class DecryptionFile
      {
          public void DecryptFile(string fileEncrypted, string password)
          {
  
              byte[] bytesToBeDecrypted = File.ReadAllBytes(fileEncrypted);
              byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
              passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
  
              byte[] bytesDecrypted = CoreDecryption.AES_Decrypt(bytesToBeDecrypted, passwordBytes);
  
              string file = fileEncrypted;
              File.WriteAllBytes(file, bytesDecrypted);
          }
      }
  </code></pre>
  
  <p>So now, I want to enter a folder path, and that my encryption process crypt every files in that chosen folder. How can I do that ?</p>