Often I have requirements to change a password somewhere using a Powershell script and am required to enter it at run time. At this point, one has an option: include the password in a log file or not. I figured it would be good to have it in the log file (the script is run on a trusted box), but plain text isn’t best. How to do this?
That’s when I thought of base64.
Here’s Powershell code to perform these actions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | Param ( [string] $Password, [switch] $Encode, [switch] $DeCode ) IF ($Encode) { ## Display & Log Base64 Encoded password $Encoded = [System.Text.Encoding]::UTF8.GetBytes($Password) $EncodedPassword = [System.Convert]::ToBase64String($Encoded) } IF ($DeCode) { ## Display & Log Base64 Decoded password $Decoded = [System.Convert]::FromBase64String($Password); $DecodedPassword = [System.Text.Encoding]::UTF8.GetString($Decoded ); Write-Output "Password ($Password) Base64 decoded is $DecodedPassword " } |