Advertisement
EddyCZ

Untitled

Apr 14th, 2023
640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.70 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Threading.Tasks;
  5.  
  6. namespace FileEncryption
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             string inputDirectoryPath = "path/to/input/directory";
  13.             string outputDirectoryPath = "path/to/output/directory";
  14.             string password = "mysecret";
  15.  
  16.             EncryptDirectory(inputDirectoryPath, outputDirectoryPath, password);
  17.         }
  18.  
  19.         static void EncryptDirectory(string inputDirectoryPath, string outputDirectoryPath, string password)
  20.         {
  21.             // Get all files in the input directory
  22.             string[] inputFilePaths = Directory.GetFiles(inputDirectoryPath);
  23.  
  24.             // Create the output directory if it doesn't exist
  25.             if (!Directory.Exists(outputDirectoryPath))
  26.             {
  27.                 Directory.CreateDirectory(outputDirectoryPath);
  28.             }
  29.  
  30.             // Create an array of tasks to encrypt each file in parallel
  31.             Task[] tasks = new Task[inputFilePaths.Length];
  32.             for (int i = 0; i < inputFilePaths.Length; i++)
  33.             {
  34.                 string inputFilePath = inputFilePaths[i];
  35.                 string outputFilePath = Path.Combine(outputDirectoryPath, Path.GetFileName(inputFilePath));
  36.                 tasks[i] = Task.Run(() => EncryptFile(inputFilePath, outputFilePath, password));
  37.             }
  38.  
  39.             // Wait for all tasks to complete
  40.             Task.WaitAll(tasks);
  41.         }
  42.  
  43.         static void EncryptFile(string inputFilePath, string outputFilePath, string password)
  44.         {
  45.             using (FileStream inputStream = File.OpenRead(inputFilePath))
  46.             using (FileStream outputStream = File.OpenWrite(outputFilePath))
  47.             using (Aes aes = Aes.Create())
  48.             {
  49.                 aes.KeySize = 256;
  50.                 aes.BlockSize = 128;
  51.                 aes.Mode = CipherMode.CBC;
  52.                 aes.Padding = PaddingMode.PKCS7;
  53.  
  54.                 byte[] salt = new byte[16];
  55.                 using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
  56.                 {
  57.                     rng.GetBytes(salt);
  58.                 }
  59.                 aes.GenerateIV();
  60.                 byte[] key = new Rfc2898DeriveBytes(password, salt, 10000).GetBytes(32);
  61.  
  62.                 outputStream.Write(salt, 0, salt.Length);
  63.                 outputStream.Write(aes.IV, 0, aes.IV.Length);
  64.  
  65.                 using (CryptoStream cryptoStream = new CryptoStream(outputStream, aes.CreateEncryptor(key, aes.IV), CryptoStreamMode.Write))
  66.                 {
  67.                     inputStream.CopyTo(cryptoStream);
  68.                 }
  69.             }
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement