Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Security.Cryptography;
- using System.Threading;
- using System.Threading.Tasks;
- class Program
- {
- static void Main(string[] args)
- {
- // Set the path to the directory to encrypt
- string directoryPath = @"C:\MyFolder";
- // Generate a new RSA key pair for the master key
- using (var rsa = new RSACryptoServiceProvider())
- {
- // Save the public key to a file
- File.WriteAllText("public.key", rsa.ToXmlString(false));
- // Save the encrypted private key to a file
- byte[] encryptedPrivateKey = rsa.ExportEncryptedPkcs8PrivateKey(args[0], new PbeParameters(PbeEncryptionAlgorithm.Aes256Cbc, HashAlgorithmName.SHA512, 100000));
- File.WriteAllBytes("private.key", encryptedPrivateKey);
- }
- // Create a thread pool to encrypt files
- int workerThreads, completionPortThreads;
- ThreadPool.GetMaxThreads(out workerThreads, out completionPortThreads);
- ThreadPool.SetMaxThreads(workerThreads, completionPortThreads);
- // Recursively encrypt files in the directory and its subdirectories
- EncryptDirectory(directoryPath);
- Console.WriteLine("Encryption complete.");
- Console.ReadKey();
- }
- static void EncryptDirectory(string directoryPath)
- {
- // Get the files in the directory
- string[] fileNames = Directory.GetFiles(directoryPath);
- // Process each file in a thread pool thread
- foreach (string fileName in fileNames)
- {
- ThreadPool.QueueUserWorkItem(new WaitCallback(EncryptFile), fileName);
- }
- // Recursively process subdirectories in a thread pool thread
- string[] subdirectoryPaths = Directory.GetDirectories(directoryPath);
- foreach (string subdirectoryPath in subdirectoryPaths)
- {
- ThreadPool.QueueUserWorkItem(new WaitCallback(EncryptDirectory), subdirectoryPath);
- }
- }
- static void EncryptFile(object fileNameObject)
- {
- string fileName = (string)fileNameObject;
- // Generate a new AES session key
- using (var aes = new AesCryptoServiceProvider())
- {
- aes.GenerateKey();
- // Encrypt the file with the session key
- using (var inputStream = new FileStream(fileName, FileMode.Open))
- {
- string encryptedFileName = fileName + ".encrypted";
- using (var outputStream = new FileStream(encryptedFileName, FileMode.Create))
- {
- // Write the encrypted session key to the output stream
- byte[] encryptedSessionKey = EncryptSessionKey(aes.Key);
- outputStream.Write(encryptedSessionKey, 0, encryptedSessionKey.Length);
- // Encrypt the file contents with the session key
- using (var encryptor = aes.CreateEncryptor())
- {
- using (var cryptoStream = new CryptoStream(outputStream, encryptor, CryptoStreamMode.Write))
- {
- inputStream.CopyTo(cryptoStream);
- }
- }
- }
- }
- }
- }
- static byte[] EncryptSessionKey(byte[] sessionKey)
- {
- // Load the master public key
- string publicKeyXml = File.ReadAllText("public.key");
- using (var rsa = new RSACryptoServiceProvider())
- {
- rsa.FromXmlString(publicKeyXml);
- // Encrypt the session key with the master public key
- return rsa.Encrypt(sessionKey, true);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement