需要下载的DLL和驱动
介于公司项目为C#项目进行的加密开发,用前几张文章所学到的TrueCrypt项目开启4个重要的入口点:
- 创建加密卷
- 加载加密卷
- 卸载加密卷
- 修改密码
- 加载驱动
- 安装驱动
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.ServiceProcess;
using System.Text;
namespace ICT.NetHandleLibrary
{
public class TrueCryptHelper
{
Logger<TrueCryptHelper> log = new Logger<TrueCryptHelper>();
[DllImport("TrueCryptFormat.dll", EntryPoint = "FormatVolumeC", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
private extern static int FormatVolumeC(string fileUrl, string pwd, Int64 size);
[DllImport("TrueCrypt.dll", EntryPoint = "MountVolumeC", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
private extern static int MountVolumeC(string fileUrl, string pwd, int driveNo);
[DllImport("TrueCrypt.dll", EntryPoint = "UnmountVolumeC", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
private extern static int UnmountVolumeC(int driveNo);
[DllImport("TrueCrypt.dll", EntryPoint = "ChangePasswordC", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
private extern static int ChangePasswordC(string szFile, string szoldPassword, string sznewPassowrd, IntPtr hwndDlg);
[DllImport("TrueCrypt.dll", EntryPoint = "DriverLoadC", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
private extern static int DriverLoadC();
[DllImport("TrueCrypt.dll", EntryPoint = "DriverInstallC", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
private extern static bool DriverInstallC();
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);
public int Format(string fileUrl, string pwd, Int64 size)
{
return FormatVolumeC(fileUrl, pwd, size);
}
public int Mount(string fileUrl, string pwd, int driveNo)
{
return MountVolumeC(fileUrl, pwd, driveNo);
}
public int Unmount(int driveNo)
{
return UnmountVolumeC(driveNo);
}
public int ChangePwd(string szFile, string szoldPassword, string sznewPassword, IntPtr intPtr)
{
return ChangePasswordC(szFile, szoldPassword, sznewPassword, intPtr);
}
public int StartDrvice()
{
return DriverLoadC();
}
public bool InstallDrvice()
{
return DriverInstallC();
}
public static bool CheckRegedit()
{
string isUrl = @"SYSTEM\CurrentControlSet\Services";
string itemName = "truecrypt";
RegistryKey subKey = Registry.LocalMachine.OpenSubKey(isUrl);
string[] keyNames = subKey.GetSubKeyNames();
subKey.Close();
bool result = false;
foreach (var keyName in keyNames)
{
if (itemName.Equals(keyName))
{
result = true;
break;
}
}
return result;
}
public static bool executeRegedit()
{
try
{
executeReg(@"SYSTEM\CurrentControlSet\Services", "truecrypt", "DisplayName", "truecrypt", RegistryValueKind.String);
executeReg(@"SYSTEM\CurrentControlSet\Services", "truecrypt", "ErrorControl", "1", RegistryValueKind.DWord);
executeReg(@"SYSTEM\CurrentControlSet\Services", "truecrypt", "ImagePath", @"System32\drivers\truecrypt.sys", RegistryValueKind.String);
executeReg(@"SYSTEM\CurrentControlSet\Services", "truecrypt", "Start", "1", RegistryValueKind.DWord);
executeReg(@"SYSTEM\CurrentControlSet\Services", "truecrypt", "Type", "1", RegistryValueKind.DWord);
executeReg(@"SYSTEM\CurrentControlSet\Services", "truecrypt", "WOW64", "1", RegistryValueKind.DWord);
}
catch
{
return false;
}
return true;
}
/// <summary>
/// 操作注册表(HKEY_LOCAL_MACHINE)
/// </summary>
/// <param name="isUrl">判断是否有某项的上一级路径</param>
/// <param name="itemName">项名称</param>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <returns></returns>
public static bool executeReg(string isUrl, string itemName, string key, string value, RegistryValueKind type)
{
RegistryKey subKey = Registry.LocalMachine.OpenSubKey(isUrl);
string[] keyNames = subKey.GetSubKeyNames();
subKey.Close();
bool result = false;
foreach (var keyName in keyNames)
{
if (itemName.Equals(keyName))
{
result = true;
break;
}
}
if (!result)
{
RegistryKey regkey = Registry.LocalMachine;
RegistryKey software = regkey.CreateSubKey(isUrl + "\\" + itemName);
software.Close();
result = true;
}
RegistryKey regKey = Registry.LocalMachine;
RegistryKey openKey = regKey.OpenSubKey(isUrl + "\\" + itemName, true);
openKey.SetValue(key, value, type);
regKey.Close();
return result;
}
public bool copySysDrive()
{
try
{
IntPtr Wow64value = IntPtr.Zero;
Wow64DisableWow64FsRedirection(ref Wow64value);
if (!File.Exists(@"C:\Windows\System32\drivers\truecrypt.sys"))
{
log.Info("驱动不存在");
string pLocalFilePath = AppDomain.CurrentDomain.BaseDirectory + "truecrypt.sys";//要复制的文件路径
string pLocalFilePath64 = AppDomain.CurrentDomain.BaseDirectory + "truecrypt-x64.sys";//要复制的文件路径
string pSaveFilePath = @"C:\Windows\System32\drivers";//指定存储的路径
if (File.Exists(pLocalFilePath))//必须判断要复制的文件是否存在
{
log.Info("准备复制" + pLocalFilePath + "," + pSaveFilePath);
File.Copy(pLocalFilePath, pSaveFilePath + "\\truecrypt.sys", true);//三个参数分别是源文件路径,存储路径,若存储路径有相同文件是否替换
}
if (File.Exists(pLocalFilePath64))//必须判断要复制的文件是否存在
{
log.Info("准备复制" + pLocalFilePath64+ "," + pSaveFilePath);
File.Copy(pLocalFilePath64, pSaveFilePath + "\\truecrypt-x64.sys", true);//三个参数分别是源文件路径,存储路径,若存储路径有相同文件是否替换
}
}
Wow64RevertWow64FsRedirection(Wow64value);
}
catch (Exception ex)
{
log.Error(ex.ToString());
return false;
}
return true;
}
public bool SysDriveState()
{
try
{
var server = System.ServiceProcess.ServiceController.GetDevices().FirstOrDefault(service => service.ServiceName == "truecrypt");
return server.Status != ServiceControllerStatus.Running ? false : true;
}
catch
{
return false;
}
}
public int GetDriveToMount()
{
const int ASCII_OFFSET = 65;
int driveToMount = -1;
DriveInfo[] allDrives = DriveInfo.GetDrives();
List<char> driveLetters = new List<char>();
List<int> trueCryptDriveLetter = new List<int>();
foreach (DriveInfo d in allDrives)
{
// Retrieve just the drive letter
char filteredName = d.Name.ElementAt<char>(0);
driveLetters.Add(filteredName);
}
foreach (char driveChar in driveLetters)
{
trueCryptDriveLetter.Add((int)driveChar - ASCII_OFFSET);
}
// Chose random drive which is available
// Chose a number between 0 and 25 excluding the drives already in use
Random random = new Random();
do
{
driveToMount = random.Next(15, 25);
} while (trueCryptDriveLetter.Contains(driveToMount));
return driveToMount;
}
}
}