Advertisement
EddyCZ

Untitled

Apr 14th, 2023
689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.53 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using VeraCrypt;
  5.  
  6. class Program
  7. {
  8.     static void Main(string[] args)
  9.     {
  10.         // Get the drive letter of the drive to encrypt
  11.         Console.Write("Enter the drive letter to encrypt (e.g. C): ");
  12.         string driveLetter = Console.ReadLine().ToUpper() + ":";
  13.  
  14.         // Get the password for the encryption
  15.         Console.Write("Enter the password for the encryption: ");
  16.         string password = Console.ReadLine();
  17.  
  18.         // Create a new VeraCrypt driver object
  19.         using (VeraCryptDriver driver = new VeraCryptDriver())
  20.         {
  21.             // Mount the VeraCrypt volume to the specified drive letter
  22.             driver.Mount(volumeFile: null, password: password, driveLetter: driveLetter);
  23.  
  24.             // Get the mounted volume's device path
  25.             string devicePath = driver.GetDevicePath(driveLetter);
  26.  
  27.             // Get the size of the device in bytes
  28.             long deviceSize = new FileInfo(devicePath).Length;
  29.  
  30.             // Unmount the VeraCrypt volume
  31.             driver.Unmount(driveLetter);
  32.  
  33.             // Create a new VeraCrypt volume with the same size as the device
  34.             using (VeraCryptVolume volume = new VeraCryptVolume())
  35.             {
  36.                 // Create the new volume with a random keyfile and no hidden volume
  37.                 volume.Create(password: password, volumeFile: null, keyfiles: null, fileSystem: FileSystemFormat.NTFS, size: deviceSize,
  38.                     clusterSize: 0, encryption: EncryptionAlgorithm.AES, hash: HashAlgorithm.SHA256, hiddenVolumeSize: 0,
  39.                     hiddenVolumeProtection: false, nonInteractive: true, verbose: false);
  40.  
  41.                 // Mount the new VeraCrypt volume to the specified drive letter
  42.                 volume.Mount(password: password, keyfiles: null, driveLetter: driveLetter, readOnly: false);
  43.  
  44.                 // Copy the contents of the unencrypted device to the VeraCrypt volume
  45.                 using (FileStream deviceStream = new FileStream(devicePath, FileMode.Open, FileAccess.Read))
  46.                 {
  47.                     using (FileStream volumeStream = new FileStream(@"\\.\" + driveLetter, FileMode.Open, FileAccess.Write))
  48.                     {
  49.                         deviceStream.CopyTo(volumeStream);
  50.                     }
  51.                 }
  52.  
  53.                 // Unmount the VeraCrypt volume
  54.                 volume.Unmount(forceDismount: true);
  55.             }
  56.         }
  57.  
  58.         Console.WriteLine("Drive encryption complete.");
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement