Skip to content

Instantly share code, notes, and snippets.

@binarytrails
Forked from gabemarshall/xor.ps1
Created October 29, 2021 19:52
Show Gist options
  • Save binarytrails/0643f86816e352637d3e8a3523133dd5 to your computer and use it in GitHub Desktop.
Save binarytrails/0643f86816e352637d3e8a3523133dd5 to your computer and use it in GitHub Desktop.
Simple Encrypt and Decrypt with Powershell
# Not secure by any means, just a PoC for XOR'ing data using powershell
# Credit to http://stackoverflow.com/questions/3478954/code-golf-xor-encryption
$enc = [System.Text.Encoding]::UTF8
function xor {
param($string, $method)
$xorkey = $enc.GetBytes("secretkey")
if ($method -eq "decrypt"){
$string = $enc.GetString([System.Convert]::FromBase64String($string))
}
$byteString = $enc.GetBytes($string)
$xordData = $(for ($i = 0; $i -lt $byteString.length; ) {
for ($j = 0; $j -lt $xorkey.length; $j++) {
$byteString[$i] -bxor $xorkey[$j]
$i++
if ($i -ge $byteString.Length) {
$j = $xorkey.length
}
}
})
if ($method -eq "encrypt") {
$xordData = [System.Convert]::ToBase64String($xordData)
} else {
$xordData = $enc.GetString($xordData)
}
return $xordData
}
$output = xor "text to encrypt" "encrypt"
# $output = xor "ciphertext" "decrypt"
Write-Host $output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment