【问题标题】:Powershell SecureString Encrypt/Decrypt To Plain Text Not WorkingPowershell SecureString 加密/解密为纯文本不起作用
【发布时间】:2014-07-24 05:44:07
【问题描述】:

我们正在尝试将用户密码作为安全字符串存储在注册表中,但我们似乎无法找到将其转换回纯文本的方法。 SecureString 可以做到这一点吗?

这是我们尝试使用的简单测试脚本...

Write-Host "Test Start..."
$PlainPassword = "@SomethingStupid" | ConvertTo-SecureString -AsPlainText -Force

$BSTR = ` [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($PlainPassword)
$PlainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
Write-Host "Password is: " $PlainPassword
Read-Host

这是我们得到的错误...

The term ' [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR'
is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\test.ps1:4 char:71
+ $BSTR = ` [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR
<<<< ($PlainPassword)
    + CategoryInfo          : ObjectNotFound: (
[System.Runtim...ureStringToBSTR:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Cannot find an overload for "PtrToStringAuto" and the argument count: "1".
At C:\test.ps1:5 char:75
+ $PlainPassword =
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto <<<< ($BSTR)
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

【问题讨论】:

  • 尝试不要用反勾号包裹 $BTSR 行
  • 给我一个语法错误... "Unexpected Token"
  • 好的,首先尝试将明文放入自己的字符串变量中,就像我认为您正在查看的文章中一样 - social.technet.microsoft.com/wiki/contents/articles/…
  • 嗯 - 您运行的是什么操作系统、.NET 和 Powershell 版本?
  • 对不起,你是对的,后面的勾号是问题所在。当我最初删除它时,我从命令中删除了第一个字符,这就是意外的令牌错误。

标签: powershell encryption


【解决方案1】:

$BSTR = ... 行中的反引号是什么意思?我同意上面的格雷厄姆。如果我删除反引号,它就可以正常工作:

$PlainPassword = "@SomethingStupid" | ConvertTo-SecureString -AsPlainText -Force
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($PlainPassword)
$PlainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
Write-Host "Password is: " $PlainPassword

输出:

Password is:  @SomethingStupid

您不会尝试在 Windows RT 或其他语言受限的 PowerShell 配置上运行它 - 是吗?

【讨论】:

    【解决方案2】:

    这是一种解密安全字符串的笨拙但更简单的方法,利用 PSCredential class 具有接受密码作为安全字符串和方法的构造函数 (GetNetworkCredential)以纯文本形式返回该密码:

    (New-Object System.Management.Automation.PSCredential 'N/A', $secure_string).GetNetworkCredential().Password
    

    虽然它旨在与凭据一起使用,但没有什么可以阻止您使用它来解密任何安全字符串*,无论出于何种目的,为用户名提供一个虚拟参数(用户名参数不能为 null 或空字符串,但任何无意义的字符串都可以)。


    * 当然,在加密安全字符串开头的帐户的上下文中

    【讨论】:

    • 完美。它允许我检索以前存储在文件中的 ConvertTo-SecureString 密码,然后将其传递给 Invoke-SqlCmd。
    • 这有什么“笨拙”?我可以将 PS 安全字符串存储在数据库字段中,检索它,将其放入 PSCredential 对象并使用此方法对其进行解密。太棒了!
    • 它不仅是特定于帐户的,还包括机器,除非您使用 ConvertFrom-SecureString 中的 -Key-SecureKey 选项将其移过。我喜欢这个,因为它是单线的。
    猜你喜欢
    • 1970-01-01
    • 2016-11-07
    • 2015-07-19
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多