Caesar Shift

Caesar Shift Cipher Using PowerShell

I am reading “The Code Book” by Simon Singh, and was inspired to create my version of the Caesar Shift Cipher in PowerShell just to see if I could do it. The main concept of the Caesar Shift Cipher is that it shifts the plaintext letters N spaces to create a new encrypted letter, for example “Hello” is shifted (Caesar Cipher Shift Key) 2 places to the right to create the encrypted text “Jgnnq”.

In my Caesar Shift Cipher program, I am using the ASCII value of each letter plus the Caesar Cipher Shift Key value to create a new letter, then I concatenate the new letters back together again to create the encrypted message. I have added a number of Write-Host commands to show you the encryption\decryption process. Enjoy having fun encrypting your own messages or changing the program, let me know how it goes.

Lets create an encrypted message, you can Copy & Paste the full code below into your favorite PowerShell Editor or open Windows PowerShell ISE.

Create Encrypted Message

  1. Enter “e” to create the encrypted message
  2. Enter “2” for the Caesar Cipher Shift key, this will shift the letter two places to the right. Note you need to remember this value to decrypt the message
  3. Enter message “Hello World”
  4. The Encrypted text will be “Jgnnq Yqtnf”

caesar1

To Decrypt the message

  1. Enter “d” to decrypt the message
  2. Enter “2” for the Caesar Cipher Shift key
  3. Enter the encrypted text “Jgnnq Yqtnf”
  4. The decrypted text is now back to the original message “Hello World”

caesar2

 

I will highlight main sections of the program to show you how the encryption\decryption works

1. This section of code displays the PlainText (unencrypted) letter ASCII value.

Write-Host "PlainText Letter ASCII Values" -foregroundcolor "green" #Hide


foreach ($letter in $String) #Hide (the foreeach cmd)
{
    $nbr = [int[]][char]$letter
    Write-Host $nbr -foregroundcolor "green"
}

 

2. This Section of code I am using the letter ASCII value to determine if the letter is upper\lower case or if it is a symbol. If the letter is a non alphabetic character I am passing it directly to the concatenation process.

Write-Host "Encrypted Text Letter ASCII Values"  -foregroundcolor "red" #Hide

$String=[char[]]$msg
foreach ($letter in $String)
{
    $nbr = [int[]][char]$letter
     
    If ($nbr -ge 0 -and $nbr -le 64) #ASCII Codes
       {
       [string]$Snbr = $nbr
       [int]$Nnbr = $Snbr  
       [int]$Enbr = $Nnbr 
       Write-Host $Enbr -foregroundcolor "red" #Hide
       [string]$ELetter = [char]$Enbr
       }
   
    If ($nbr -ge 65 -and $nbr -le 90) #Alphabet UpperCase
       {
       [string]$Snbr = $nbr
       [int]$Nnbr = $Snbr 
       [int]$nkey = $key
       [int]$Enbr = $Nnbr + $nkey 
       If ($Enbr -gt 90) {$Enbr = $Enbr - 26}
       If ($Enbr -lt 65) {$Enbr = $Enbr + 26}
       Write-Host $Enbr -foregroundcolor "red" #Hide
       [string]$ELetter = [char]$Enbr
       #Write-Host "Lowercase " + $ELetter        
       } 
   
    If ($nbr -ge 91 -and $nbr -le 96) #ASCII Codes
       {
       [string]$Snbr = $nbr
       [int]$Nnbr = $Snbr  
       [int]$Enbr = $Nnbr 
       Write-Host $Enbr -foregroundcolor "red"
       [string]$ELetter = [char]$Enbr
       }
   
    If ($nbr -ge 97 -and $nbr -le 122) #Alphabet Lowercase
       {
       [string]$Snbr = $nbr
       [int]$Nnbr = $Snbr 
       [int]$nkey = $key
       [int]$Enbr = $Nnbr + $nkey 
       If($Enbr -gt 122) {$Enbr = $Enbr - 26}
       If  ($Enbr -lt 97) {$Enbr = $Enbr + 26}
       Write-Host $Enbr -foregroundcolor "red" #Hide
       [string]$ELetter = [char]$Enbr
       #Write-Host "Lowercase " + $ELetter        
       }
        
     If ($nbr -ge 123 -and $nbr -le 127) #ASCII Codes
        {
        [string]$Snbr = $nbr
        [int]$Nnbr = $Snbr  
        [int]$Enbr = $Nnbr 
        Write-Host $Enbr -foregroundcolor "red" #Hide
        [string]$ELetter = [char]$Enbr
        }

 

3. Let’s take a closer look at the encryption process. In this case, I am using the letter ASCII value to determine that the letter is an upper case letter. Then I need to add the Caesar Cipher shift key value to the letter ASCII value to create the new encrypted letter ([int]$Enbr = $Nnbr + $nkey). One of the challenges that I had is to keep the letter an upper case after it was encrypted, this is being accomplished by the two inner If statements ( If ($Enbr -gt 90) {$Enbr = $Enbr – 26}, If ($Enbr -lt 65) {$Enbr = $Enbr + 26}), I needed to keep the letter looping through the Upper case letters. Then the new ASCII value is being convert back to a letter character and saved to the $ELetter parameter for the concatenation process.

    If ($nbr -ge 65 -and $nbr -le 90) #Alphabet UpperCase
       {
       [string]$Snbr = $nbr
       [int]$Nnbr = $Snbr 
       [int]$nkey = $key
       [int]$Enbr = $Nnbr + $nkey 
       If ($Enbr -gt 90) {$Enbr = $Enbr - 26}
       If ($Enbr -lt 65) {$Enbr = $Enbr + 26}
       Write-Host $Enbr -foregroundcolor "red" #Hide
       [string]$ELetter = [char]$Enbr
       #Write-Host "Lowercase " + $ELetter        
       }

 

4. For every letter that is ran through the foreach commend it is concatenated back into the encrypted message and then displayed, this is accomplished by $EMsg = $EMsg + $ELetter .

$EMsg = $EMsg + $ELetter 
    
} 
Write-Host "Encrypted Text: " $EMsg -foregroundcolor "red"
Clear-variable EMsg
Write-Host "-------------------------------------------------" 
}

 

5. The Decrypt process is the same as the encryption process except for the Caesar Cipher Shift key is minus from the letter ASCII value ([int]$Enbr = $Nnbr – $nkey). I have the encryption process looping to the right and the decryption process looping through to the left.

    If ($nbr -ge 65 -and $nbr -le 90) #Alphabet UpperCase
       {
       [string]$Snbr = $nbr
       [int]$Nnbr = $Snbr 
       [int]$nkey = $key
       [int]$Enbr = $Nnbr - $nkey 
       If ($Enbr -gt 90) {$Enbr = $Enbr - 26}
       If ($Enbr -lt 65) {$Enbr = $Enbr + 26}
       Write-Host $Enbr -foregroundcolor "green" #Hide
       [string]$ELetter = [char]$Enbr
       #Write-Host "Lowercase " + $ELetter        
       }

 

Full Code

Write-Host "Caesar Shift Cipher"

$EType = read-host "Enter 'e' for Encryption or Enter 'd' from Decryption"
[int]$key = read-host "Create Caesar Cipher Shift Key, enter in a number from 1 to 26"
$msg = read-host "Enter Message"
Write-Host "-------------------------------------------------" 

#Encryption
if ($EType -eq "e")
{

Write-Host "PlainText Message: " $msg -foregroundcolor "green" #Hide

$String=[char[]]$msg


Write-Host "PlainText Letter ASCII Values" -foregroundcolor "green" #Hide


foreach ($letter in $String) #Hide (the foreeach cmd)
{
    $nbr = [int[]][char]$letter
    Write-Host $nbr -foregroundcolor "green"
}


Write-Host "Encrypted Text Letter ASCII Values"  -foregroundcolor "red" #Hide

$String=[char[]]$msg
foreach ($letter in $String)
{
    $nbr = [int[]][char]$letter
     
    If ($nbr -ge 0 -and $nbr -le 64) #ASCII Codes
       {
       [string]$Snbr = $nbr
       [int]$Nnbr = $Snbr  
       [int]$Enbr = $Nnbr 
       Write-Host $Enbr -foregroundcolor "red" #Hide
       [string]$ELetter = [char]$Enbr
       }
   
    If ($nbr -ge 65 -and $nbr -le 90) #Alphabet UpperCase
       {
       [string]$Snbr = $nbr
       [int]$Nnbr = $Snbr 
       [int]$nkey = $key
       [int]$Enbr = $Nnbr + $nkey 
       If ($Enbr -gt 90) {$Enbr = $Enbr - 26}
       If ($Enbr -lt 65) {$Enbr = $Enbr + 26}
       Write-Host $Enbr -foregroundcolor "red" #Hide
       [string]$ELetter = [char]$Enbr
       #Write-Host "Lowercase " + $ELetter        
       } 
   
    If ($nbr -ge 91 -and $nbr -le 96) #ASCII Codes
       {
       [string]$Snbr = $nbr
       [int]$Nnbr = $Snbr  
       [int]$Enbr = $Nnbr 
       Write-Host $Enbr -foregroundcolor "red"
       [string]$ELetter = [char]$Enbr
       }
   
    If ($nbr -ge 97 -and $nbr -le 122) #Alphabet Lowercase
       {
       [string]$Snbr = $nbr
       [int]$Nnbr = $Snbr 
       [int]$nkey = $key
       [int]$Enbr = $Nnbr + $nkey 
       If($Enbr -gt 122) {$Enbr = $Enbr - 26}
       If  ($Enbr -lt 97) {$Enbr = $Enbr + 26}
       Write-Host $Enbr -foregroundcolor "red" #Hide
       [string]$ELetter = [char]$Enbr
       #Write-Host "Lowercase " + $ELetter        
       }
        
     If ($nbr -ge 123 -and $nbr -le 127) #ASCII Codes
        {
        [string]$Snbr = $nbr
        [int]$Nnbr = $Snbr  
        [int]$Enbr = $Nnbr 
        Write-Host $Enbr -foregroundcolor "red" #Hide
        [string]$ELetter = [char]$Enbr
        }    

$EMsg = $EMsg + $ELetter 
    
} 
Write-Host "Encrypted Text: " $EMsg -foregroundcolor "red"
Clear-variable EMsg
Write-Host "-------------------------------------------------" 
}
  
#Decrypt
if ($EType -eq "d")
{

Write-Host "Encrypted Message: " $msg -foregroundcolor "red"

$String=[char[]]$msg

Write-Host "Encrypted Text Letter ASCII Values" -foregroundcolor "red" #Hide

foreach ($letter in $String) #Hide (the foreeach cmd)
{
    $nbr = [int[]][char]$letter
    Write-Host $nbr -foregroundcolor "red"
}

Write-Host "PlainText Letter ASCII Values"  -foregroundcolor "green" #Hide

$String=[char[]]$msg
foreach ($letter in $String)
{
    $nbr = [int[]][char]$letter
   
    If ($nbr -ge 0 -and $nbr -le 64) #ASCII Codes
       {
       [string]$Snbr = $nbr
       [int]$Nnbr = $Snbr  
       [int]$Enbr = $Nnbr 
       Write-Host $Enbr -foregroundcolor "green"#Hide
       [string]$ELetter = [char]$Enbr
       }
  
    If ($nbr -ge 65 -and $nbr -le 90) #Alphabet UpperCase
       {
       [string]$Snbr = $nbr
       [int]$Nnbr = $Snbr 
       [int]$nkey = $key
       [int]$Enbr = $Nnbr - $nkey 
       If ($Enbr -gt 90) {$Enbr = $Enbr - 26}
       If ($Enbr -lt 65) {$Enbr = $Enbr + 26}
       Write-Host $Enbr -foregroundcolor "green" #Hide
       [string]$ELetter = [char]$Enbr
       #Write-Host "Lowercase " + $ELetter        
       }  
   
    If ($nbr -ge 91 -and $nbr -le 96) #ASCII Codes
       {
       [string]$Snbr = $nbr
       [int]$Nnbr = $Snbr  
       [int]$Enbr = $Nnbr 
       Write-Host $Enbr -foregroundcolor "green"
       [string]$ELetter = [char]$Enbr
       }
          
    If ($nbr -ge 97 -and $nbr -le 122) #Alphabet LowerCase
       {
       [string]$Snbr = $nbr
       [int]$Nnbr = $Snbr 
       [int]$nkey = $key
       [int]$Enbr = $Nnbr - $nkey 
       If($Enbr -gt 122) {$Enbr = $Enbr - 26}
       If  ($Enbr -lt 97) {$Enbr = $Enbr + 26}
       Write-Host $Enbr -foregroundcolor "green" #Hide
       [string]$ELetter = [char]$Enbr       
       }    
     
    If ($nbr -ge 123 -and $nbr -le 127) #ASCII Codes
       {
       [string]$Snbr = $nbr
       [int]$Nnbr = $Snbr  
       [int]$Enbr = $Nnbr 
       Write-Host $Enbr -foregroundcolor "green" #Hide
       [string]$ELetter = [char]$Enbr
       }    

$EMsg = $EMsg + $ELetter    
} 
Write-Host "PlainText: " $EMsg -foregroundcolor "green"
Clear-variable EMsg
Write-Host "-------------------------------------------------"
}
Advertisements