2017-02-24 61 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像一個魅力一樣工作!非常感謝!請務必張貼評論作爲答案,所以我可以給你信貸。 :)