Skip to content

Instantly share code, notes, and snippets.

@gabemarshall
Last active November 1, 2023 11:17
Show Gist options
  • Save gabemarshall/f25afd533b341e1b21bc39f8e26946b7 to your computer and use it in GitHub Desktop.
Save gabemarshall/f25afd533b341e1b21bc39f8e26946b7 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
@sctfic
Copy link

sctfic commented Jul 3, 2018

Function ConvertTo-XorString {
    <#
        .SYNOPSIS
            Chiffrage reverssible avec une PassPhrase
        .DESCRIPTION
            Chiffre ou deChiffre une chaine d'entree
        .PARAMETER DataString
            Chaine a convertir
        .PARAMETER PassPhrase
            Clef de chiffrement
        .PARAMETER fromBase64
            Si la chaine d'entree et au format Base64
        .PARAMETER toBase64
            Si la chaine de sortie doit etre au format Base64
        .EXAMPLE
            ConvertTo-XorString
        .NOTES
            Alban LOPEZ 2018
            [email protected]
            http://git/PowerTech/
        #>


    [alias("ConvertTo-Crypt","ConvertFrom-Crypt","ConvertFrom-XorString")]
    param(
        [Parameter(ValueFromPipeline=$true)]
            [string[]]$DataString,
        $PassPhrase = $false,
        [switch]$fromBase64,
        [switch]$toBase64
    )
    begin {
        $xorkey = "$("$PassPhrase" | Get-Hash -Algorithm SHA512)".ToCharArray()
        $Encoding = [System.Text.Encoding]::UTF8
    }
    process {
        foreach ($Data in $DataString){
            $out = @()
            try{
                if($fromBase64){
                    $Data = [System.Convert]::FromBase64String($Data)
                } else {
                    $Data = $Encoding.GetBytes($Data) # .ToCharArray()
                }

                for ($i = 0; $i -lt $Data.count; $i++) {
                    $out += [char]([Byte]$Data[$i] -bxor [Byte]$xorkey[$i%$xorkey.count])
                    #Write-color " [$i]:", $Data[$i], ' -bxor ', "[$($i%$xorkey.count)]:", $xorkey[$i%$xorkey.count], ' = ',$out[-1] -ForeGroundColor Cyan,Yellow,White,Green,Magenta,red
                }

                if($toBase64) {
                    [System.Convert]::ToBase64String($out)
                } else {
                    $Encoding.GetString($out)
                }
            } catch {
                Write-LogStep '',$_ Error
                # throw $_
            }
        }
    }
    end {
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment