function Encrypt-Password {
param (
[String] $Password,
[String] $Purpose = "default"
)
if ($PSVersionTable.PsVersion.Major -gt 2) {
[Reflection.Assembly]::LoadWithPartialName("System.Web")
$enc = [system.Text.Encoding]::UTF8
$data1 = $enc.GetBytes($Password)
$enc = [System.Convert]::ToBase64String([System.Web.Security.MachineKey]::Protect($data1, $Purpose))
Write-Host $enc
} else {
Write-Warning "Powershell Version 2 installed. Needs Powershell Version 3 or higher."
Write-Warning "Unable to replace password."
}
}
function Decrpt-Password {
param (
[String] $Password,
[String] $Purpose = "default"
)
if ($PSVersionTable.PsVersion.Major -gt 2) {
[Reflection.Assembly]::LoadWithPartialName("System.Web")
$data1 = [System.Convert]::FromBase64String($Password)
$data1 = [System.Web.Security.MachineKey]::Unprotect($data1, $Purpose)
$enc = [System.Text.Encoding]::UTF8
$data1 = $enc.GetString($data1)
Write-Host $data1
} else {
Write-Warning "Powershell Version 2 installed. Needs Powershell Version 3 or higher."
Write-Warning "Unable to replace password."
}
}
Encrypt-Password -Password "mB23339h0392DFiBomfyXOAr" -Purpose "SecureProtect"
Decrpt-Password -Password "nUuImlpb2gC/BOTtdEJk2NHc7n19TbuZo3238p0SkIdCmYO8y5D2FNqCnnmJKqSbUDvftfoig+lXHJ1r63WSK2FYLqGlumtDYjjXCWn5sGkYROp1GA=" -Purpose "SecureProtect"shell