Skip to content
🎉 Welcome! Enjoy your reading, and I hope you will learn something new.

Ceasar Cipher

The Caesar cipher is one of the simplest, oldest, and most widely known encryption techniques used in cryptography. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions along the alphabet.

Note

Note that this cipher is a special case of Affine Cipher


Here is a simple Python implementation.

ceasar-cipher.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import string

CHARS = string.ascii_letters


def get_letter_position(letter):
    """Convert letter to numeric position (A=0, B=1, etc.)"""
    return ord(letter.upper()) - ord("A")


def position_to_letter(position):
    """Convert numeric position back to letter"""
    return chr(position + ord("A"))


def encrypt(text, shift):
    """Encrypt given text by a fixed shift offset"""
    encrypted = ""
    for letter in text:
        if letter in CHARS:
            lpos = get_letter_position(letter)
            encrypted += position_to_letter((lpos + shift) % 26)
        else:
            encrypted += letter

    return encrypted


def decrypt(text, shift):
    """Decrypt given text by a fixed shift offset"""
    return encrypt(text, -shift)


################################################
# TEST
################################################

ciphertext = encrypt("The flag is COUCOU", 3)
print(ciphertext)
print(decrypt(ciphertext, 3))
print(encrypt(ciphertext, -3))

Observation

Notice that, because the Caesar cipher is said to be symmetric, the decrypt function is exactly the same as the encrypt one; it just takes the inverse of the key parameter.

Attacks

Brute-force

This cipher is very easy to crack because of its nature. As there are only \(26\) possible substitutions, you can try all of them until you find the correct plaintext.

This online tool does just that.

Or, you can code it by yourself in Python.

ceasar-cipher-cracker.py
1
2
for shift in range(26):
    print(decrypt(ciphertext, shift))

Resources

Last updated on