Caesar Cipher Made Simple: A Step-by-Step Guide

Caesar cipher is the most  known cipher in cryptography. It goes with other names like (Shift cipher, Caesar’s code, Caesar’s shift and Caesar’s cipher).

Caesar cipher is an examples of monoalphabetic substitution cipher. It is also a symmetric cipher as the same key is used for both encryption and decryption process.

In this guide I will be discussing:

Where this caesar cipher originates from, its uses, resources for further learning, online tools for encoding and decoding caesar cipher and implementation of this encryption algorithm in various programming languages.

A brief history of caesar cipher:

It was invented by Julius Caesar with the aim of sending messages to his troops. He used a shift of 3 in the letters of the alphabet to encrypt messages.

Augustus also used caesar cipher with a slight variation:

He shifted every letter with one space to the right to scramble the message.

1) How Caesar cipher works

Basically this is a substitution cipher where each letter of the plaintext is replaced with a letter that corresponds to a given letter that has been either shifted up or down the alphabet.

What you come up after the encryption has been done is called ciphertext. This text is completely scrambled and unreadable.

 

For example if I decide to use a shift of 3 to encode my message:

“A” will become “D”, “B” will become “E”, “C” will become “F” and so one down the alphabet.

To encode a simple message is equally easy as all you have to do is match each letter of the plaintext to the corresponding shifted letter to come up with the ciphertext.

Here is an example of Caesar cipher:

Plaintext: Learning cryptography is fun.

Ciphertext: Ohduqlqj fubswrjudskb lv ixq

To decrypt a message encrypted with caesar cipher requires you to have the key used. The easy way to go about is generate a table using the key that was used to encrypt the message.

The benefit of this step is to enable you to quickly decode the message. After you have the table decryption process is as easy as match every letter of the ciphertext to the right letter on the table to get the original message.

Here are is an example (use shift 3) for you to decrypt:

Ciphertext: Vkliw flskhu lv dzhvrph

Plaintext: ?

2) Caesar cipher mathematics

Consider the following: a=0, b=1…..and z=25. Each letter of the alphabet is assigned a number.

Caesar cipher encryption formula is given by:

E(x) = ( x + p) mod 26

The “P” represents the shift applied to each letter. You can think of it as the key too. The “x” is the letter you want to encrypt or decrypt.

Caesar cipher decryption formula is given by:

E(x) = ( x – p) mod 26

Note that the result of each process is undergoes modulo division. This is to ensure that if a letter is shifted past the end of the alphabet you are using, it will be wrapped at the beginning.

3) Caesar variations

ROT13 cipher is an example of caesar cipher with a shift of 13 as the key. It is commonly used to hide offensive words in newspapers and forums.

Also a more complex cipher like vigenere uses some principles of caesar’s code for the encryption process.

4) How to break (crack) Caesar cipher

It is very easy to break this cipher considering that there are only 25 possible keys.

It should not be used today as it’s a weak (insecure) cipher and offers no security at all.

Caesar cipher is very vulnerable to attacks considering the power of modern computers that can evaluate a lot of combinations in just seconds.

Here are ways to crack this cipher:

  • Brute force attack: If you don’t have the key you easily decrypt a message by trying all the 25 possible combinations.
  • Frequency analysis: since the structure of the message doesn’t change that much, you can easily recognize patterns in the letters and with enough time you will be able to decode the message without even requiring a key.

For example if the language used is English, you can start by replacing the most frequent letter in the message with letter “e”.

5) Caesar cipher tools and resources

If you want to learn and read caesar cipher easily there are several tools that will help you do just that. Use these tools to write simple caesar cipher message and learn how it works.

You can encode and decode to text and English or the Language of your choice.

Tools:

6) Implementation of Caesar cipher in various programming languages

The following are the source code that implements this cipher. Use the code to learn more about how Caesar’s code works.

Caesar cipher Java program

public class CaesarCipher
{

        public static void main(String[] args) 
        {
                //gets a string to encrypt
                String str = (JOptionPane.showInputDialog("Input Data to encypt:"));
                
                //gets a key 
                String key = (JOptionPane.showInputDialog("Input the key:"));
                int keyLength=str.length(key);
                
        
                
                //prints encryption
                String encrypted = encrypt(str, keyLength);
                System.out.println("Encrypted:" + encrypted);

                //prints decryption
                String decrypted = decrypt(encrypted, keyLength);
                System.out.println("Decrypted:" + decrypted);
        }

        public static String encrypt(String str, int keyLength) 
        {
                String encrypted = "";
                for(int i = 0; i < str.length(); i++) 
                {
                        int c = str.charAt(i);
                        
                        if (Character.isUpperCase(c)) 
                        {
                                //26 letters of the alphabet so mod by 26
                                c = c + (key % 26);
                                if (c > 'Z')
                                c = c - 26;
                        } 
                        else if (Character.isLowerCase(c)) 
                        {
                                c = c + (key % 26);
                                if (c > 'z')
                                c = c - 26;
                        }
                        encrypted += (char) c;
                }
        return encrypted;
        }

        public static String decrypt(String str, int key) 
        {
                String decrypted = "";
                for(int i = 0; i < str.length(); i++) 
                {
                        int c = str.charAt(i);
                        if (Character.isUpperCase(c)) 
                        {
                                c = c - (key % 26);
                                if (c < 'A')
                                c = c + 26;
                `       } 
                        else if (Character.isLowerCase(c)) 
                        {
                                c = c - (key % 26);
                                if (c < 'a')
                                c = c + 26;
                        }
                        decrypted += (char) c;
                }
                return decrypted;
        }
}

Caesar’s code in Python

# we need 2 helper mappings, from letters to ints and the inverse
L2I = dict(zip("ABCDEFGHIJKLMNOPQRSTUVWXYZ",range(26)))
I2L = dict(zip(range(26),"ABCDEFGHIJKLMNOPQRSTUVWXYZ"))

key = 3
plaintext = "DEFEND THE EAST WALL OF THE CASTLE"

# encipher
ciphertext = ""
for c in plaintext.upper():
    if c.isalpha(): ciphertext += I2L[ (L2I[c] + key)%26 ]
    else: ciphertext += c

# decipher
plaintext2 = ""
for c in ciphertext.upper():
    if c.isalpha(): plaintext2 += I2L[ (L2I[c] - key)%26 ]
    else: plaintext2 += c

print plaintext
print ciphertext
print plaintext2

Caesar cipher program in c++

/*****Please include following header files*****/ // string // ctype.h /***********************************************/ /*****Please use following namespaces*****/ // std /*****************************************/ static char Cipher(char ch, int key) {         if (!isalpha(ch))                 return ch;         char offset = isupper(ch) ? 'A' : 'a';         return (char)((((ch + key) - offset) % 26) + offset); } static string Encipher(string input, int key) {         string output = "";         for each (char ch in input)                 output += Cipher(ch, key);         return output; } static string Decipher(string input, int key) {         return Encipher(input, 26 - key); }

Caesar’s shift program in PHP

function Cipher($ch, $key) {         if (!ctype_alpha($ch))                 return $ch;         $offset = ord(ctype_upper($ch) ? 'A' : 'a');         return chr(fmod(((ord($ch) + $key) - $offset), 26) + $offset); } function Encipher($input, $key) {         $output = "";         $inputArr = str_split($input);         foreach ($inputArr as $ch)                 $output .= Cipher($ch, $key);         return $output; } function Decipher($input, $key) {         return Encipher($input, 26 - $key); }

Caesar cipher program in VB.Net

Private Shared Function Cipher(ch As Char, key As Integer) As Char         If Not Char.IsLetter(ch) Then                 Return ch         End If         Dim offset As Integer = Convert.ToInt32(If(Char.IsUpper(ch), "A"c, "a"c))         Return ChrW((((Convert.ToInt32(ch) + key) - offset) Mod 26) + offset) End Function Public Shared Function Encipher(input As String, key As Integer) As String         Dim output As String = String.Empty         For Each ch As Char In input                 output += Cipher(ch, key)         Next         Return output End Function Public Shared Function Decipher(input As String, key As Integer) As String         Return Encipher(input, 26 - key) End Function

 

Add Comment