1

I have been following along on numerous how-to articles that explain how to decrypt an AES encrypted password. No matter how I try, the password ends up with $password displaying as System.Security.SecureString. I need the password to echo in plain text because I am calling a command line utility that does not use windows permissions (ADSI/LDAP). Here's my script:

$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)
5
  • 3
    Why two calls to ConvertTo-SecureString? $MyPassword = Get-Content $PasswordFile | ConvertTo-SecureString -Key $key should suffice Commented Feb 24, 2017 at 14:54
  • Yeah, you first call ConvertTo-SecureString on the decrypted password and then complain that it is a secure string?!? Commented Feb 24, 2017 at 15:21
  • Your suggestion to use $MyPassword = Get-Content $PasswordFile | ConvertTo-SecureString -Key $key worked like a charm! Thank you so much! Make sure to post that comment as the answer so I can give you credit for it. :) Commented Feb 24, 2017 at 15:53
  • bbcompent1 - Tip: use @username to ensure @MathiasRJessen is notified of your comment asking him to promote the comment to an answer :-) Commented Feb 27, 2017 at 18:01
  • @MathiasRJessen Your suggestion to use $MyPassword = Get-Content $PasswordFile | ConvertTo-SecureString -Key $key worked like a charm! Thank you so much! Make sure to post that comment as the answer so I can give you credit for it. :) Commented Mar 2, 2017 at 18:17

1 Answer 1

1

According to @MathiasRJessen the suggestion to use $MyPassword = Get-Content $PasswordFile | ConvertTo-SecureString -Key $key worked like a charm! Thank you so much! Make sure to post that comment as the answer so I can give you credit for it. :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.