Windows我们使用BitLocker对磁盘进行加密,公共PC以及硬盘存在插拔移动使用,可以考虑这个方案

比如右键盘符E-启用BitLocker,在下面窗口内输入密码对E盘进行加密

image

加密后,每次开机以及插入,使用E盘均需要输入密钥进行解密。

下面我们使用.NET实现对Bitlocker的操作

启用BitLocker

Windows点击启用BitLocker,这个操作

image

使用Powershell命令,可以拆解为以下操作:

// 1. 添加用户密码(相当于用户输入的解锁密码)
Add-BitLockerKeyProtector -PasswordProtector -Password $pwd

// 2. 启用加密 + 生成恢复密钥(相当于系统强制要求备份恢复密钥)
Enable-BitLocker -RecoveryPasswordProtector

虽然Windows 安全策略要求:在启用 BitLocker 加密之前,必须至少有一个恢复机制。这是为了防止用户忘记密码后完全无法访问数据。

但我试了下,先启用BitLocker、再添加密码保护器,也是可以的。有需要的可以参考下,

 1     public OperateResult EnableBitLocker(string mountPath, string password)
 2     {
 3         if (mountPath.EndsWith("\\"))
 4         {
 5             mountPath = mountPath.TrimEnd('\\');
 6         }
 7 
 8         // 添加密码保护器
 9         var addPasswordScript = $@"
10             $securePassword = ConvertTo-SecureString '{password}' -AsPlainText -Force
11             Add-BitLockerKeyProtector -MountPoint '{mountPath}' -PasswordProtector -Password $securePassword
12         ";
13         var addPasswordResult = PowerShellUtils.ExecScript(addPasswordScript);
14         if (!addPasswordResult.Success && addPasswordResult.Message.Contains(password))
15         {
16             return addPasswordResult;
17         }
18 
19         // 使用恢复密码保护器启用BitLocker
20         var enableScript = $@"Enable-BitLocker -MountPoint '{mountPath}' -EncryptionMethod XtsAes256 -UsedSpaceOnly -RecoveryPasswordProtector -Confirm:$false";
21         var enableResult = PowerShellUtils.ExecScript(enableScript);
22         if (!enableResult.Success)
23         {
24             return enableResult;
25         }
26 
27         return OperateResult.ToSuccess();
28     }

以上mountPath可以是盘符O:,也可以是VHDX的挂载目录如E:\test\vhdx0209

启用BitLocker相当于给你的设备设置了一个密码,后续需要使用时通过这个密码解锁才能使用

值得一提的是,GPT5.2_codex给了我一版错误的。。。折腾了半个小时,大家看看有什么区别?

 1     public OperateResult EnableBitLocker(string mountPath, string password)
 2     {
 3         if (mountPath.EndsWith("\\"))
 4         {
 5             mountPath = mountPath.TrimEnd('\\');
 6         }
 7 
 8         // 使用密码保护器启用BitLocker(需要组策略允许)
 9         var enableScript = $@"
10     $securePassword = ConvertTo-SecureString '{password}' -AsPlainText -Force
11     Enable-BitLocker -MountPoint '{mountPath}' -EncryptionMethod XtsAes256 -UsedSpaceOnly -PasswordProtector -Password $securePassword -Confirm:$false
12 ";
13         var enableResult = PowerShellUtils.ExecScript(enableScript);
14         if (!enableResult.Success)
15         {
16             return enableResult;
17         }
18 
19         // 添加恢复密码保护器
20         var addRecoveryScript = $@"Add-BitLockerKeyProtector -MountPoint '{mountPath}' -RecoveryPasswordProtector";
21         var addRecoveryResult = PowerShellUtils.ExecScript(addRecoveryScript);
22 
23         return addRecoveryResult;
24     }

解锁BitLocker

每次设备加载(开机、拔插、Vhdx加载等)后,均需要进行BitLocker解锁才能打开设备

先查询锁定状态,

image

Powershell查询,

 1     public OperateResult GetBitLocker(string mountPath)
 2     {
 3         if (mountPath.EndsWith("\\"))
 4         {
 5             mountPath = mountPath.TrimEnd('\\');
 6         }
 7         var script = $"Get-BitLockerVolume -MountPoint '{mountPath}'";
 8         var result = PowerShellUtils.ExecScriptToDataList(script);
 9         var operateResult = result.ToResult();
10         operateResult.Data = result.Data?.FirstOrDefault();
11         return operateResult;
12     }

LocalBitLockerVolume.LockStatus属性,0表示已解锁,1表示锁定

盘符被锁定的状态:

image

 解锁BitLocker,有需要的可以参考:

 1     public OperateResult> UnlockBitLocker(string mountPath, string password)
 2     {
 3         if (mountPath.EndsWith("\\"))
 4         {
 5             mountPath = mountPath.TrimEnd('\\');
 6         }
 7         var script = $"Unlock-BitLocker -MountPoint '{mountPath}' -Password (ConvertTo-SecureString '{password}' -AsPlainText -Force)";
 8         var result = PowerShellUtils.ExecScriptToDataList(script);
 9         return result;
10     }

已解锁的状态:

image

 以上是常用的几个BitLocker操作命令,更多的可以看看官网文档 BitLocker Module | Microsoft Learn


原文地址: https://www.cveoy.top/t/topic/qFVM 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录