没有以与获取解密密码相同的方式从注册表中获取解密的 OpenVPN 用户名

Not getting the decrypted OpenVPN Username from the Registry the same way that works for getting the decrypted Password

我有一个 Powershell 脚本,我成功地使用它来获取存储在注册表中的解密 OpenVPN 密码。

这是从注册表中成功获取存储的 OpenVPN 密码的脚本,该密码也是解密的字符串:

Add-Type -AssemblyName System.Core
Add-Type -AssemblyName System.Security
$keys = Get-ChildItem "HKCU:\Software\OpenVPN-GUI\configs"
$items = $keys | ForEach-Object {Get-ItemProperty $_.PsPath}

foreach ($item in $items)
{
  $encryptedbytes=$item.'auth-data'
  $entropy=$item.'entropy'
  $entropy=$entropy[0..(($entropy.Length)-2)]

  $decryptedbytes = [System.Security.Cryptography.ProtectedData]::Unprotect(
    $encryptedBytes,
    $entropy,
    [System.Security.Cryptography.DataProtectionScope]::CurrentUser)

  Write-Host ([System.Text.Encoding]::Unicode.GetString($decryptedbytes))
}

但是如果我对此进行一些修改以从相同的 OpenVPN 注册表部分获取解密的 username 并使用正确的密钥 username 如下所示,它不会获取解密的用户名:

Add-Type -AssemblyName System.Core
Add-Type -AssemblyName System.Security
$keys = Get-ChildItem "HKCU:\Software\OpenVPN-GUI\configs"
$items = $keys | ForEach-Object {Get-ItemProperty $_.PsPath}

foreach ($item in $items)
{
  $entropy=$item.'entropy'
  $username=$item.'username'
  $encryptedbytes=$item.'auth-data'
  $entropy=$entropy[0..(($entropy.Length)-2)]
  $username=$username[0..(($username.Length)-2)]

  $decryptedbytes = [System.Security.Cryptography.ProtectedData]::Unprotect(
    $encryptedBytes,
    $username,
    [System.Security.Cryptography.DataProtectionScope]::CurrentUser)

  Write-Host ([System.Text.Encoding]::Unicode.GetString($decryptedbytes))
}

任何人都可以帮忙知道我在这里做错了什么并解决这个问题吗?

目前用户名未加密存储。只需将其解码为 Unicode。这可能会在未来的版本中改变。对于加密数据,调用 Unprotect() 会将 $entropy 作为第二个参数,而不是 $username。