Created
July 11, 2017 11:58
-
-
Save atao/934fc9ea083fa2007c8f07f88d2bdfdb to your computer and use it in GitHub Desktop.
Functions to crypt and decrypt string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://community.idera.com/powershell/powertips/b/tips/posts/encrypting-text-information-using-passphrase | |
function encrypt{ | |
param( | |
[string]$pass, | |
[string]$text | |
) | |
$key = [Byte[]]($pass.PadRight(24).Substring(0,24).ToCharArray()) | |
# $secure = $text | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString -Key $key | |
$secure = $text | | |
ConvertTo-SecureString -AsPlainText -Force | | |
ConvertFrom-SecureString -Key $key | |
return $secure | |
} | |
encrypt -pass "pass" -text "blablaa" | |
function decrypt{ | |
param( | |
[string]$pass | |
[string]$secure | |
) | |
$key = [Byte[]]($pass.PadRight(24).Substring(0,24).ToCharArray()) | |
try | |
{ | |
$textSecureString = $secure | | |
ConvertTo-SecureString -Key $key -ErrorAction Stop | |
$cred = New-Object -TypeName System.Management.Automation.PSCredential('dummy', $textSecureString) | |
$text = $cred.GetNetworkCredential().Password | |
} | |
catch | |
{ | |
$text = '(wrong key)' | |
} | |
return $text | |
} | |
decrypt -pass "pass" -secure "76492d1116743f0423413b16050a5345MgB8AGgAZwBaAE0ATwBNAE4AVwB6AFMAZQAvADAATAAyAFMAWQByAHkANwBWAHcAPQA9AHwAYgAxAGYAYwA3ADIAMAAxADEAZQBmAGQANAA0AGEAOAA3AGQAZAA4ADYAOAAxAGMAZQA5ADYAZgAxAGMAMgA1AA==" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment