惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
V
V2EX
博客园_首页
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Recent Announcements
Recent Announcements
博客园 - 司徒正美
Microsoft Security Blog
Microsoft Security Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Latest news
Latest news
Vercel News
Vercel News
The Register - Security
The Register - Security
T
The Exploit Database - CXSecurity.com
S
Schneier on Security
N
Netflix TechBlog - Medium
WordPress大学
WordPress大学
小众软件
小众软件
L
Lohrmann on Cybersecurity
GbyAI
GbyAI
P
Privacy & Cybersecurity Law Blog
T
Tor Project blog
AWS News Blog
AWS News Blog
美团技术团队
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
K
Kaspersky official blog
B
Blog RSS Feed
G
Google Developers Blog
量子位
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
Scott Helme
Scott Helme
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
I
Intezer
雷峰网
雷峰网
Martin Fowler
Martin Fowler
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Blog — PlanetScale
Blog — PlanetScale
IT之家
IT之家
F
Full Disclosure
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 【当耐特】
The Hacker News
The Hacker News
U
Unit 42
S
SegmentFault 最新的问题
I
InfoQ
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
宝玉的分享
宝玉的分享
罗磊的独立博客
Spread Privacy
Spread Privacy
C
CERT Recently Published Vulnerability Notes

博客园 - 唐宋元明清2188

Electron 桌面客户端 ASAR 热更新:替换一个文件完成版本切换 .NET Win32设置只读未对齐,导致NTFS文件系统识别异常 SDD-skills执行遗漏问题 SDD基于规范编程-OpenSpec及SuperPowers .NET 磁盘BitLocker加密-技术选型 .NET Win32磁盘动态卷触发“函数不正确”问题排查 .NET SqlSugar多线程下SqlSugarClient 的线程安全陷阱 .NET 本地Db数据库-技术方案选型 .NET 磁盘管理-技术方案选型 .NET 记录Amazon上传S3异常问题 .NET 记录多框架下的Json序列化属性标记问题 .NET 阻止Windows关机以及阻止失败的一些原因 网络虚拟存储 Iscsi实现方案 Windows 网络存储ISCSI介绍 Windows 本地虚拟磁盘Vhdx .NET 数据拷贝方案选择 .NET 窗口置于最顶层 如何做好软件架构师 .NET开发一些书箱推荐 Windows应用开发-常用工具 WPF 记录鼠标、触摸多设备混合输入场景问题 .NET Bios相关数据读写
.NET 磁盘Bitlocker加密-Powershell操作
唐宋元明清2188 · 2026-02-09 · via 博客园 - 唐宋元明清2188

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     }

So我的理解是,密码要放在Add-BitLockerKeyProtector命令中,Enable-BitLocker只负责启用BitLocker

解锁BitLocker

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

先查询锁定状态,

image

Powershell查询,

 1     public OperateResult<LocalBitLockerVolume> 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<LocalBitLockerVolume>(script);
 9         var operateResult = result.ToResult<LocalBitLockerVolume>();
10         operateResult.Data = result.Data?.FirstOrDefault();
11         return operateResult;
12     }

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

盘符被锁定的状态:

image

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

 1     public OperateResult<List<LocalBitLockerVolume>> 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<LocalBitLockerVolume>(script);
 9         return result;
10     }

已解锁的状态:

image

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