2017-02-24 462 views
1

我一直在关注如何解密AES加密密码的许多指导性文章。不管我如何尝试,密码都以$ password的形式显示为System.Security.SecureString。我需要密码才能以纯文本形式回显,因为我正在调用不使用Windows权限的命令行实用程序(ADSI/LDAP)。这里是我的脚本:在PowerShell脚本中将AES加密密码解密为纯文本

$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) 
+3

为什么两次调用'ConvertTo-SecureString'? '$ MyPassword = Get-Content $ PasswordFile |的ConvertTo-SecureString的-Key $ key'应该足够 –

+0

呀,你第一个电话'的ConvertTo-SecureString'解密的密码,然后抱怨说,它是一个安全的字符串?!? –

+0

您的建议使用$ MyPassword = Get-Content $ PasswordFile | ConvertTo-SecureString -Key $ key像一个魅力一样工作!非常感谢!请务必张贴评论作为答案,所以我可以给你信贷。 :) – bbcompent1

回答

0

根据@MathiasRJessen建议使用$ MyPassword输入=获取内容$ PASSWORDFILE | ConvertTo-SecureString -Key $ key像一个魅力一样工作!非常感谢!请务必张贴评论作为答案,所以我可以给你信贷。 :)