Skip to content

Instantly share code, notes, and snippets.

@atao
Created July 11, 2017 11:58
Show Gist options
  • Save atao/934fc9ea083fa2007c8f07f88d2bdfdb to your computer and use it in GitHub Desktop.
Save atao/934fc9ea083fa2007c8f07f88d2bdfdb to your computer and use it in GitHub Desktop.
Functions to crypt and decrypt string
# 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