Search This Blog

Sunday, November 26, 2023

Cryptography: Enciphering with PowerShell

The idea came to me after having followed some ARG's (alternate reality games), where people solve puzzles, often decoding some enciphered texts using more or lesser known cipher techniques, such as the classic Caesar Cipher, where you shift a letter in the alphabet a number of steps.

I wanted to see if I could create a script in PowerShell that would perform this action, after prompting ChatGPT a bit I managed to get the main logic behind the shifting.

This was put into an encoding function and a decoding function. Later on after the script matured, I decided to create a basic GUI for it, based off of the simple password generator I had created earlier.

This version offers you case cleaing, optional clipboard copy and decoding/encoding. In the future there might be some error handling put into the code, such as handling empty text fields.




This is what the code looks like:

# Caesar cipher /w GUI #


[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")


$Main = New-Object System.Windows.Forms.Form

$Main.text = "Caesar Cipher 1.1"

$Main.size = New-Object system.drawing.size(600,200)

$Main.BackColor = "DarkGray"

$Main.FormBorderStyle = "FixedDialog"


$PlainText = New-Object System.Windows.Forms.TextBox

$PlainText.Size = New-Object System.Drawing.Size(400,100)

$PlainText.Location = New-Object System.Drawing.Size(50,30)

$PlainText.BackColor = "Black"

$PlainText.ForeColor = "LightGreen"

$Main.Controls.Add($PlainText)


$CipherText = New-Object System.Windows.Forms.TextBox

$CipherText.Size = New-Object System.Drawing.Size(400,100)

$CipherText.Location = New-Object System.Drawing.Size(50,50)

$CipherText.BackColor = "Black"

$CipherText.ForeColor = "LightGreen"

$Main.Controls.Add($CipherText)


$Clipboard = New-Object System.Windows.Forms.CheckBox

$Clipboard.Size = New-Object System.Drawing.Size(80,40)

$Clipboard.Location = New-Object System.Drawing.Size(50,75)

$Clipboard.Checked = $False

$Clipboard.Text = "Clipboard"

$Main.Controls.Add($Clipboard)


$CipherKey = New-Object System.Windows.Forms.TextBox

$CipherKey.Size = New-Object System.Drawing.Size(30,50)

$CipherKey.Location = New-Object System.Drawing.Size(140,87)

$CipherKey.Text = "1"

$Main.Controls.Add($CipherKey)


function Encode-CaesarCipher {

    param ($pt, $ck)

    $pt = $PlainText.Text;

    $ck = $CipherKey.Text;

    $e = ""

    foreach ($c in $pt.ToUpper().ToCharArray()) {

        $x = [int][char]$c

        if ($x -ge 65 -and $x -le 90) { $e += [char](65 + (($x - 65 + $ck) % 26)) }

        elseif ($x -ge 97 -and $x -le 122) { $e += [char](97 + (($x - 97 + $ck) % 26)) }

        else { $e += $c }

    }

    $CipherText.text = $e

    if ($Clipboard.checked) {$e | Set-Clipboard}

}


function Decode-CaesarCipher {

    param ($ct, $ck)

    $ct = $CipherText.Text;

    $ck = $CipherKey.Text;

    $m = ""

    foreach ($c in $ct.ToUpper().ToCharArray()) {

        $x = [int][char]$c

        if ($x -ge 65 -and $x -le 90) { $m += [char](65 + (($x - 65 - $ck + 26) % 26)) }

        elseif ($x -ge 97 -and $x -le 122) { $m += [char](97 + (($x - 97 - $ck + 26) % 26)) }

        else { $m += $c }

    }

    $PlainText.text = $m

    if ($Clipboard.checked) {$m | Set-Clipboard}

}


$EncodeButton = New-Object System.Windows.Forms.Button

$EncodeButton.Size = New-Object System.Drawing.Size(60,40)

$EncodeButton.Location = New-Object System.Drawing.Size(180,87)

$EncodeButton.Text = "Encode"

$EncodeButton.BackColor = "Green"

$EncodeButton.ForeColor = "Yellow"

$EncodeButton.Add_Click({Encode-CaesarCipher;})

$Main.Controls.Add($EncodeButton)


$DecodeButton = New-Object System.Windows.Forms.Button

$DecodeButton.Size = New-Object System.Drawing.Size(60,40)

$DecodeButton.Location = New-Object System.Drawing.Size(250,87)

$DecodeButton.Text = "Decode"

$DecodeButton.BackColor = "Green"

$DecodeButton.ForeColor = "Yellow"

$DecodeButton.Add_Click({Decode-CaesarCipher;})

$Main.Controls.Add($DecodeButton)


[void] $Main.ShowDialog()

No comments:

Post a Comment