Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Security.Cryptography;
- using VeraCrypt;
- class Program
- {
- static void Main(string[] args)
- {
- // Get the drive letter of the drive to encrypt
- Console.Write("Enter the drive letter to encrypt (e.g. C): ");
- string driveLetter = Console.ReadLine().ToUpper() + ":";
- // Get the password for the encryption
- Console.Write("Enter the password for the encryption: ");
- string password = Console.ReadLine();
- // Create a new VeraCrypt driver object
- using (VeraCryptDriver driver = new VeraCryptDriver())
- {
- // Mount the VeraCrypt volume to the specified drive letter
- driver.Mount(volumeFile: null, password: password, driveLetter: driveLetter);
- // Get the mounted volume's device path
- string devicePath = driver.GetDevicePath(driveLetter);
- // Get the size of the device in bytes
- long deviceSize = new FileInfo(devicePath).Length;
- // Unmount the VeraCrypt volume
- driver.Unmount(driveLetter);
- // Create a new VeraCrypt volume with the same size as the device
- using (VeraCryptVolume volume = new VeraCryptVolume())
- {
- // Create the new volume with a random keyfile and no hidden volume
- volume.Create(password: password, volumeFile: null, keyfiles: null, fileSystem: FileSystemFormat.NTFS, size: deviceSize,
- clusterSize: 0, encryption: EncryptionAlgorithm.AES, hash: HashAlgorithm.SHA256, hiddenVolumeSize: 0,
- hiddenVolumeProtection: false, nonInteractive: true, verbose: false);
- // Mount the new VeraCrypt volume to the specified drive letter
- volume.Mount(password: password, keyfiles: null, driveLetter: driveLetter, readOnly: false);
- // Copy the contents of the unencrypted device to the VeraCrypt volume
- using (FileStream deviceStream = new FileStream(devicePath, FileMode.Open, FileAccess.Read))
- {
- using (FileStream volumeStream = new FileStream(@"\\.\" + driveLetter, FileMode.Open, FileAccess.Write))
- {
- deviceStream.CopyTo(volumeStream);
- }
- }
- // Unmount the VeraCrypt volume
- volume.Unmount(forceDismount: true);
- }
- }
- Console.WriteLine("Drive encryption complete.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement