Skip to content

Instantly share code, notes, and snippets.

@Arnaud-Chevalier
Last active January 13, 2021 12:23
Show Gist options
  • Save Arnaud-Chevalier/7d5d4880b75fdc19d549c0d072870b38 to your computer and use it in GitHub Desktop.
Save Arnaud-Chevalier/7d5d4880b75fdc19d549c0d072870b38 to your computer and use it in GitHub Desktop.
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$Form = New-Object System.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(600,270)
$Form.Text = "Caesar Cipher Encryption"
$Form.ShowIcon = $False
###Function used to encrypt the text
function Encrypt {
#Populate $i with the selected shift to apply
$i = $DropDownBox.SelectedItem.ToString() #populate the var with the value you selected
#Populate $text with the text input
$text = $InputBox.Text
#Transform the text to an array of ascii
$asciis = [int[]][char[]]"$text"
#Add the shift specified to every ascii character
$encrypt = foreach ($ascii in $asciis) {
$ascii + $i
}
#Transform back the ascii to text
$asciitotext = [char[]]$encrypt
#Transform the array to a string to read it easily
$arraytostring = -join $asciitotext
$OutputBox.text = $arraytostring
}
#Label1 to show explanation
$label1 = New-Object System.Windows.Forms.Label
$label1.Location = New-Object System.Drawing.Point(10,10)
$label1.Size = New-Object System.Drawing.Size(280,20)
$label1.Text = 'Enter the text to encrypt : '
$form.Controls.Add($label1)
###Text to encrypt
$InputBox = New-Object System.Windows.Forms.TextBox
$InputBox.Location = New-Object System.Drawing.Size(10,35)
$InputBox.Size = New-Object System.Drawing.Size(565,40)
$InputBox.MultiLine = $True
$InputBox.ScrollBars = "Vertical"
$Form.Controls.Add($InputBox)
###Combox box to select the shift to apply to the text
$DropDownBox = New-Object System.Windows.Forms.ComboBox
$DropDownBox.Location = New-Object System.Drawing.Size(10,80)
$DropDownBox.Size = New-Object System.Drawing.Size(180,20)
$DropDownBox.text = "Select the Shift to apply"
$DropDownBox.DropDownHeight = 200
$Form.Controls.Add($DropDownBox)
$Numbers=@(1,2,3,4,5,6,7,8,9)
foreach ($Number in $Numbers) {
$DropDownBox.Items.Add($Number)
}
###Button to launch the encryption
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(400,80)
$Button.Size = New-Object System.Drawing.Size(110,80)
$Button.Text = "Encrypt"
$Button.Add_Click({Encrypt})
$Form.Controls.Add($Button)
#Label2 to show explanation
$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(10,150)
$label2.Size = New-Object System.Drawing.Size(280,20)
$label2.Text = 'Your text encrypted : '
$form.Controls.Add($label2)
###Show the result of the encryption
$OutputBox = New-Object System.Windows.Forms.TextBox
$OutputBox.Location = New-Object System.Drawing.Size(10,170)
$OutputBox.Size = New-Object System.Drawing.Size(565,40)
$OutputBox.MultiLine = $True
$OutputBox.ScrollBars = "Vertical"
$Form.Controls.Add($OutputBox)
###Show the Gui Box
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment