Let's Talk Encryption

Part 1 - Caesar Cipher

Posted on August 4, 2016 as Cryptography. 7 minutes read.

How the Ceasar cipher works?

The Caesar cipher, also called Caesar's code, shift cipher or Caesar shift, is one of the simplest and most known forms of encryption. It is a substitution cipher where each letter in the plaintext (original message) is replaced with a letter corresponding to a certain number of letters up or down in the alphabet creating a cipher text.

caesar-shift

Caesar cipher, due to the fact that the key is very small is vulnerable to the brute force attack, where attacker has to try only 25 combinations in order to decrypt the message. In addition, the original structure of the plaintext remains intact, so a person trying to decrypt the message can perform a frequency analysis - method to decrypt by analysing the frequency of each letter and comparing it with the frequency of each letter in certain alphabet. For example in English alphabet letter e is the most common one, and by comparing the most common letter in the cipher with letter e we can try to discover the shift that was used. Read more at:

Letter-Frequency

History behind Caesar Cipher

This cipher is named after Julius Caesar, who used it to protect his communication with generals during military campaigns. According to Suetonius, who wrote a book about life of the Roman emperor, Julius used the substituation cipher to a shift of three, meaning shifted each letter 3 places through the alphabet where D becomes A and so on.

If he had anything confidential to say, he wrote it in cipher, that is, by so changing the order of the letters of the alphabet, that not a word could be made out. If anyone wishes to decipher these, and get at their meaning, he must substitute the fourth letter of the alphabet, namely D, for A, and so with the others.

Suetonius, Life of Julius Caesar 56

Programming Caesar cipher tools

Great way to learn how this cipher works is to create your own tools to encrypt, decrypt or even brute force the message. You can do this in any programming language you know, but I'm gonna stick with Python as it's my favourite one.

Firsty let's create a function to encrypt the message with a certain shift.

#!/usr/bin/python

key = 'abcdefghijklmnopqrstuvwxyz'

def encrypt(plaintext, n):
    result = ''

    for letter in plaintext.lower():
        try:
            i = (key.index(letter) + n) % 26
            result += key[i]
        except ValueError:
            result += letter

    return result.lower()

Firstly we declare a variable key that is English aplhabet. Adding the algorith to check if the character exists in our alphabet will allow us to use punctuation, spaces without encrypting them.

Now let's add to this main function that will use function encrypt and will display the results.

def main(plaintext, n):
    encrypted = encrypt(plaintext, n)
    print('[*] Shift: %s' % n)
    print('[*] Plaintext: %s' % plaintext)
    print('[*] Ciphertext: %s' % encrypted)

And the last step: providing input for the message and the key.

if __name__ == '__main__':
    plaintext = input('Please enter your message: ')
    shift = int(input('Enter the shift: '))
    main(plaintext, shift)

Great! It works and the message is encrypted.

$ python3 caesar-encode.py
Please enter your message: No one will ever read this!
Enter the shift: 15
[*] Shift: 15
[*] Plaintext: No one will ever read this!
[*] Ciphertext: cd dct lxaa tktg gtps iwxh!

But what if we get the encrypted message from a friend, with a provided key?

Let's create a decrypt script!

#!/usr/bin/python

key = 'abcdefghijklmnopqrstuvwxyz'

def decrypt(n, ciphertext):
    result = ''

    for letter in ciphertext:
        try:
            i = (key.index(letter) - n) % 26
            result += key[i]
        except ValueError:
            result += letter

    return result

def main(encrypted, n):
    decrypted = decrypt(n, encrypted)
    print('[*] Shift: %s' % n)
    print('[*] Encrypted: %s' % encrypted)
    print('[*] Decrypted: %s' % decrypted)


if __name__ == '__main__':
    encrypted = input('Please enter your encrypted message: ')
    shift = int(input('Enter the shift: '))
    main(encrypted, shift)

As you can see it's mostly the same as the encrypt script. Let's test this!

$ python3 caesar-decode.py
Please enter your encrypted message: cd dct lxaa tktg gtps iwxh!
Enter the shift: 15
[*] Shift: 15
[*] Encrypted: cd dct lxaa tktg gtps iwxh!
[*] Decrypted: no one will ever read this!

Great! We decrypted the secret message! But what if we don't have a key but our curiosity won't let us leave this message unread. We can try to analyse frequency of letters but it may take some time. So why not to try and brute force it?

#!/usr/bin/python

import sys

ciphertext = input("[*] Enter your ciphertext: ").upper()
alphabet = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
plaintext = ""
shift = 1

while shift <= 26:
 for letter in ciphertext:
  if letter in alphabet:
   plaintext += alphabet[(alphabet.index(letter)+shift)%(len(alphabet))]
 print("Shift: " + str(shift))
 print("Ciphertext: " + ciphertext)
 print("Plaintext: " + plaintext)
 shift = shift + 1
 plaintext = ""

This script is not as advanced as the previous ones becouse as you'll see it 'eats' the spaces but it is capable of trying all combinations so let's give it a try.

$ python3 caesar-brute.py
[*] Enter your ciphertext: cd dct lxaa tktg gtps iwxh!
Shift: 1
Ciphertext: CD DCT LXAA TKTG GTPS IWXH!
Plaintext: DEEDUMYBBULUHHUQTJXYI
Shift: 2
Ciphertext: CD DCT LXAA TKTG GTPS IWXH!
Plaintext: EFFEVNZCCVMVIIVRUKYZJ
Shift: 3
Ciphertext: CD DCT LXAA TKTG GTPS IWXH!
Plaintext: FGGFWOADDWNWJJWSVLZAK
Shift: 4
Ciphertext: CD DCT LXAA TKTG GTPS IWXH!
Plaintext: GHHGXPBEEXOXKKXTWMABL
Shift: 5
Ciphertext: CD DCT LXAA TKTG GTPS IWXH!
Plaintext: HIIHYQCFFYPYLLYUXNBCM
Shift: 6
Ciphertext: CD DCT LXAA TKTG GTPS IWXH!
Plaintext: IJJIZRDGGZQZMMZVYOCDN
Shift: 7
Ciphertext: CD DCT LXAA TKTG GTPS IWXH!
Plaintext: JKKJASEHHARANNAWZPDEO
Shift: 8
Ciphertext: CD DCT LXAA TKTG GTPS IWXH!
Plaintext: KLLKBTFIIBSBOOBXAQEFP
Shift: 9
Ciphertext: CD DCT LXAA TKTG GTPS IWXH!
Plaintext: LMMLCUGJJCTCPPCYBRFGQ
Shift: 10
Ciphertext: CD DCT LXAA TKTG GTPS IWXH!
Plaintext: MNNMDVHKKDUDQQDZCSGHR
Shift: 11
Ciphertext: CD DCT LXAA TKTG GTPS IWXH!
Plaintext: NOONEWILLEVERREADTHIS

As always I hoped you enjoyed exploring this topic with me and it brough you some clarity about how the ciphers worked back then, how to write encoding, decoding and brute forcing scripts using shift cipher. In the next part of Let's Talk Encryption series we're going to explore more present ciphers.

UPDATE!

Check out my newest Caesar tool at Github!

Github

Keep learning and stay safe!

~ W3ndige