functions/security/ConvertFrom-EncryptedString.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
function ConvertFrom-EncryptedString { param( [Parameter(Mandatory = $true, ValueFromPipeline = $true)] [string]$EncryptedString, [Parameter(Mandatory = $false)] [switch]$AsPlainText, [Parameter(Mandatory = $false)] [ValidateSet('CurrentUser', 'LocalMachine')] [System.Security.Cryptography.DataProtectionScope]$Scope = [System.Security.Cryptography.DataProtectionScope]::CurrentUser ) begin { $entropy = Get-Entropy } process { $decryptedData = [System.Security.Cryptography.ProtectedData]::Unprotect( [System.Convert]::FromBase64String($EncryptedString), [System.Text.Encoding]::Unicode.GetBytes($entropy), $Scope) $decrtypted = [System.Text.Encoding]::Unicode.GetString($decryptedData) if ($AsPlainText) { $decrtypted } else { ConvertTo-SecureString -String $decrtypted -AsPlainText } } } |