Caesar Cipher Explained with Python Examples

In cryptography, the alphabets are the minimum unit that carries information. For example, in binary numbers, alphabets are 0 and 1. In the decimal number system, numbers from 0-9 are the alphabets and similarly, the English language has 26 alphabets from A-Z. Cryptography is used to encrypt or decrypt the message so that the message can be transferred securely through any communication channel. 

Caesar Cipher Introduction

Caesar cipher is an algorithm to encrypt/decrypt alphabets by substitution method. It belongs to the substitution cipher category. Hence, in order to generate ciphertext, this encryption/decryption algorithm replaces every alphabet of plaintext with another alphabet.

Caesar cipher technique defines the method of alphabet replacement to get ciphertext from a plain text. That means the Caesar cipher technique replaces every plain text alphabet with another alphabet by shifting it x times towards right (down the alphabets) to get ciphertext. In contrast, we replace every alphabet of ciphertext x timer towards the right to get the plaintext. Here, x is the key that defines the number of shifts.

Note: The name of this technique is given after his inventor Julius Caesar.

Encryption Example

For instance, if the key (x) is equal to 1, A will become B, B will become C, and so on. Similarly, If the key is 2, A will replace with C, B will replace with D, and so on. Therefore, the key (x) defines the number of shifts down the alphabets.

Caesar Cipher Example

For example, we have a plaintext “CS GEEKS HUB” and we want to encrypt this text using caesar cipher and the key is equal to 2.

Plaintext = CS GEEKS HUB
// that means shift every alphabet down to 2 positions
key = 2  
Ciphertext = EU IGGMU JWD 

In the above example with a key of 2, the Caesar cipher takes the alphabet C and maps it to alphabet E. Because with one shift, C becomes D and with second shift operation, D becomes E. Similarly, all other letters shifts to 2 position down to produce Ciphertext.

Decryption Example

The decryption process is the exact opposite of the encryption process. In contrast, decryption performs the left shift on alphabets by key ‘x’. In the above example with key =2, if we perform 2 positions left shift on each alphabet of ciphertext, we will get the plaintext.

Ciphertext = EU IGGMU JWD
//That means shift every alphabet up to 2 positions
key = 2 
Plaintext = CS GEEKS HUB

Cyclic Shift

Key is a main factor which determines the number of shifts up or down to alphabets. There are total of 26 alphabets in the English language. Therefore, the number of possible keys for caesar cipher is 26 or between 0-25.

To understand cycle shift, let’s take an example with key = 26. with this key the number of shifts is 26. That means A will map itself. This is because if we count 25 letters from the start from A, on the 25th count, we will get Z. Therefore, on the 26th count, we will get A again. Because in the English language, the total alphabets are 26. Therefore, each letter will map to itself with the key is equal to 26. This is also known as a cyclic shift.

As shown in this example, plaintext and ciphertext are the same. Because each letter replaces with itself due to the cyclic shift. That means having shift key zero or 26 is the same.

Plaintext = CS GEEKS HUB
key = 26
Ciphertext = CS GEEKS HUB

Caesar Cipher Python Code

def apply_rotation(c, factor):
"""Applies a shift of factor to the letter denoted by c"""
if c.isalpha():
lower = ord('A') if c.isupper() else ord('a')
c = chr(lower + ((ord(c) - lower + factor) % 26))
return c
def caesar_cipher(s, k):
"""Iterates through each letter and constructs the cipher text"""
new_message = ''
factor = k % 26
for c in s:
new_message += apply_rotation(c, factor)
return new_message

Leave a Comment