You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PowerShell实现BitLocker 9位非连续数字PIN验证

符合要求的BitLocker PIN设置脚本

要实现9位数字且非连续序列的BitLocker PIN验证,需要在原代码基础上添加输入校验逻辑,确保用户输入符合规则后再执行加密操作。以下是完善后的脚本:

do {
    # 读取明文PIN用于验证(验证通过后再转为安全字符串)
    $PIN = Read-Host -Prompt '请输入9位BitLocker PIN(不能是连续递增/递减数字序列)'
    
    # 验证1:是否为9位纯数字
    $isValidLengthAndDigit = $PIN -match '^\d{9}$'
    
    if (-not $isValidLengthAndDigit) {
        Write-Host "错误:PIN必须是9位纯数字,请重新输入。" -ForegroundColor Red
        continue
    }
    
    # 验证2:是否为连续递增/递减序列(如123456789、987654321)
    $isSequential = $false
    # 检查递增序列
    $ascending = 1..8 | ForEach-Object { [int]$PIN[$_] - [int]$PIN[$_-1] -eq 1 }
    if ($ascending -notcontains $false) {
        $isSequential = $true
    }
    # 检查递减序列
    $descending = 1..8 | ForEach-Object { [int]$PIN[$_-1] - [int]$PIN[$_] -eq 1 }
    if ($descending -notcontains $false) {
        $isSequential = $true
    }
    
    if ($isSequential) {
        Write-Host "错误:PIN不能是连续递增或递减的数字序列,请重新输入。" -ForegroundColor Red
        continue
    }

    # 所有验证通过,退出循环
    $validPIN = $true
} while (-not $validPIN)

# 将验证后的PIN转为安全字符串
$SecureString = ConvertTo-SecureString $PIN -AsPlainText -Force

# 启用BitLocker加密
Enable-BitLocker -MountPoint "C:" -EncryptionMethod Aes256 -UsedSpaceOnly -Pin $SecureString -TPMandPinProtector

关键逻辑说明:

  • 循环输入校验:用do-while循环确保用户输入达标后才执行后续加密操作
  • 格式验证:通过正则表达式^\d{9}$强制校验PIN为9位纯数字
  • 连续序列验证:分别检查数字是否为连续递增/递减序列,只要符合其中一种就判定为无效PIN
  • 安全字符串转换:仅在验证通过后才将明文PIN转为安全字符串,兼顾安全性与校验需求

内容的提问来源于stack exchange,提问作者Suraj Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 01:25:18