在PowerShell脚本中将AES加密密码解密为纯文本

时间:2017-02-24 14:35:32

标签: powershell encryption cryptography

我一直在关注许多解释如何解密AES加密密码的how-to文章。无论我如何尝试,密码都以$ password显示为System.Security.SecureString。我需要密码以纯文本形式回显,因为我正在调用不使用Windows权限(ADSI / LDAP)的命令行实用程序。这是我的剧本:

$PasswordFile = "$PSScriptRoot\PowerShell\AESpassword.txt" 
$KeyFile = "$PSScriptRoot\PowerShell\AES.key" 
$key = Get-Content $KeyFile
$MyPassword = ConvertTo-SecureString (Get-Content $PasswordFile | ConvertTo-SecureString -Key $key) -AsPlainText -Force
$Marshal = [System.Runtime.InteropServices.Marshal]
$BSTR = $Marshal::SecureStringToBSTR($MyPassword)
$password = $Marshal::PtrToStringAuto($BSTR)

1 个答案:

答案 0 :(得分:1)

根据@MathiasRJessen建议使用$ MyPassword = Get-Content $ PasswordFile | ConvertTo-SecureString -Key $ key就像一个魅力!非常感谢!请务必将该评论作为答案发布,以便我为您提供信誉。 :)