Advertisement
EddyCZ

Untitled

Apr 14th, 2023 (edited)
625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.69 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6.  
  7. class Program
  8. {
  9.     static void Main(string[] args)
  10.     {
  11.         // Set the path to the directory to encrypt
  12.         string directoryPath = @"C:\MyFolder";
  13.  
  14.         // Generate a new RSA key pair for the master key
  15.         using (var rsa = new RSACryptoServiceProvider())
  16.         {
  17.             // Save the public key to a file
  18.             File.WriteAllText("public.key", rsa.ToXmlString(false));
  19.  
  20.             // Save the encrypted private key to a file
  21.             byte[] encryptedPrivateKey = rsa.ExportEncryptedPkcs8PrivateKey(args[0], new PbeParameters(PbeEncryptionAlgorithm.Aes256Cbc, HashAlgorithmName.SHA512, 100000));
  22.             File.WriteAllBytes("private.key", encryptedPrivateKey);
  23.         }
  24.  
  25.         // Create a thread pool to encrypt files
  26.         int workerThreads, completionPortThreads;
  27.         ThreadPool.GetMaxThreads(out workerThreads, out completionPortThreads);
  28.         ThreadPool.SetMaxThreads(workerThreads, completionPortThreads);
  29.  
  30.         // Recursively encrypt files in the directory and its subdirectories
  31.         EncryptDirectory(directoryPath);
  32.  
  33.         Console.WriteLine("Encryption complete.");
  34.         Console.ReadKey();
  35.     }
  36.  
  37.     static void EncryptDirectory(string directoryPath)
  38.     {
  39.         // Get the files in the directory
  40.         string[] fileNames = Directory.GetFiles(directoryPath);
  41.  
  42.         // Process each file in a thread pool thread
  43.         foreach (string fileName in fileNames)
  44.         {
  45.             ThreadPool.QueueUserWorkItem(new WaitCallback(EncryptFile), fileName);
  46.         }
  47.  
  48.         // Recursively process subdirectories in a thread pool thread
  49.         string[] subdirectoryPaths = Directory.GetDirectories(directoryPath);
  50.         foreach (string subdirectoryPath in subdirectoryPaths)
  51.         {
  52.             ThreadPool.QueueUserWorkItem(new WaitCallback(EncryptDirectory), subdirectoryPath);
  53.         }
  54.     }
  55.  
  56.     static void EncryptFile(object fileNameObject)
  57.     {
  58.         string fileName = (string)fileNameObject;
  59.  
  60.         // Generate a new AES session key
  61.         using (var aes = new AesCryptoServiceProvider())
  62.         {
  63.             aes.GenerateKey();
  64.  
  65.             // Encrypt the file with the session key
  66.             using (var inputStream = new FileStream(fileName, FileMode.Open))
  67.             {
  68.                 string encryptedFileName = fileName + ".encrypted";
  69.                 using (var outputStream = new FileStream(encryptedFileName, FileMode.Create))
  70.                 {
  71.                     // Write the encrypted session key to the output stream
  72.                     byte[] encryptedSessionKey = EncryptSessionKey(aes.Key);
  73.                     outputStream.Write(encryptedSessionKey, 0, encryptedSessionKey.Length);
  74.  
  75.                     // Encrypt the file contents with the session key
  76.                     using (var encryptor = aes.CreateEncryptor())
  77.                     {
  78.                         using (var cryptoStream = new CryptoStream(outputStream, encryptor, CryptoStreamMode.Write))
  79.                         {
  80.                             inputStream.CopyTo(cryptoStream);
  81.                         }
  82.                     }
  83.                 }
  84.             }
  85.         }
  86.     }
  87.  
  88.     static byte[] EncryptSessionKey(byte[] sessionKey)
  89.     {
  90.         // Load the master public key
  91.         string publicKeyXml = File.ReadAllText("public.key");
  92.         using (var rsa = new RSACryptoServiceProvider())
  93.         {
  94.             rsa.FromXmlString(publicKeyXml);
  95.  
  96.             // Encrypt the session key with the master public key
  97.             return rsa.Encrypt(sessionKey, true);
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement