Powershell SecureString加密/解密为纯文本无效

Powershell SecureString加密/解密为纯文本无效,powershell,encryption,Powershell,Encryption,我们试图将用户密码作为安全字符串存储在注册表中,但似乎找不到将其转换回纯文本的方法。这在SecureString中可能吗 下面是我们尝试使用的简单测试脚本 Write-Host "Test Start..." $PlainPassword = "@SomethingStupid" | ConvertTo-SecureString -AsPlainText -Force $BSTR = ` [System.Runtime.InteropServices.Marshal]::SecureStrin

我们试图将用户密码作为安全字符串存储在注册表中,但似乎找不到将其转换回纯文本的方法。这在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
术语“[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR” 无法识别为cmdlet、函数、脚本文件或可操作程序的名称。请检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。 在C:\test.ps1:4 char:71 +$BSTR=`[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR
$BSTR=…
行中的反勾号是什么?我同意上面格雷厄姆的观点。如果我删除了backtick,它就可以正常工作:

$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配置上运行此操作,因为该语言受到限制,是吗?

这里有一种困难但简单得多的方法来解密安全字符串,它利用了以下事实,即有一个构造函数将密码作为安全字符串和方法接受(GetNetworkCredential)以纯文本形式返回密码:

(新对象System.Management.Automation.PSCredential'N/A',$secure\u string)。GetNetworkCredential()。密码
尽管它旨在与凭据一起使用,但没有任何东西可以阻止您使用它来解密任何安全字符串*,不管目的如何,为用户名提供一个伪参数(用户名参数不能为null或空字符串,但任何无意义的字符串都可以)。

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

请尝试不要将$BTSR行包装为背面勾选符给我一个语法错误…“意外令牌”好的,首先尝试将明文放在它自己的字符串变量中,就像在文章中我想你看到的那样-嗯-你运行的是什么OS、.NET和Powershell版本?很抱歉,你是对的,回勾是问题所在。当我最初删除它时,我去掉了命令的第一个字符,这是意外的令牌错误。Perfect、 它允许我检索存储在文件中的先前ConvertToSecureString密码,然后将其传递给InvokeSqlcmd关于此?我可以将PS securestring存储在数据库字段中,检索它,将其放入PSCredential对象中,然后使用此方法对其进行解密。太棒了!它不仅是特定于帐户的,而且是机器,除非您使用
ConvertFrom securestring
中的
-Key
-SecureKey
选项将其移动过来。我喜欢它这是因为这是一条单行线。