CAESAR CIPHER

Aman Gondaliya
4 min readMay 20, 2020

--

Introduction :-

This cipher was named after Julius Caesar. According to Suetonius, a roman historian, Julias Caesar is the first person who used this cipher to protect messages related to the military. There are more complicated ciphers used by julius and his family but Caesar cipher is very famous and you will often find it in hacking competitions.

It is a substitution cipher in which we will rotate characters of our plaintext up or down (in alphabets A-Z). The number of rotations will be our key. For ex, we can rotate character ‘A’ 3 times which will map to ‘D’. We can rotate the character ‘F’ 7 times which will map to ‘M’.

So according to key value, we can form an encryption table. There are two ways we can talk about Caesar cipher.

  1. Caesar Wheel :

First we will set the outer will with the inner wheel such that the alphabets of both wheels coincide with each other. After that We can rotate the outer wheel and change the position of it according to the key. The inner wheel will not rotate.

Using this wheel, we can generate our ciphertext from the plaintext.Currently in the given picture, ‘a’ of the inner wheel is coinciding with ‘H’. So in this case our rotation or key will be 7.

Also, we can move the outer wheel clockwise or anticlockwise. Julius’s nephew, Augustus used a clockwise direction in case of encryption while Caesar originally used anticlockwise direction for encryption. So we will consider the anticlockwise direction here for encryption. Decryption is just a reverse process of encryption so we will rotate the outer wheel in reverse direction, i.e. in clockwise direction.

2. Encryption Table:

Encryption Table

In the above table, We rotated alphabets according to key. This is an encryption table and we did a left shift here. You can imagine this table from a Caesar wheel by connecting the ends of a string. The first column of the table will act as an inner wheel while the third column of the table will act as an outer wheel . For the decryption process we can just do reverse, i.e. right shift.

Understanding the table is easy, for key = 1, ‘A’ is replaced with ‘B’, ‘B’ is replaced with ‘C’ and so on.. in order to generate ciphertext. For key=2 ‘A’ is replaced with ‘C’, ‘B’ is replaced with ‘D’ and so on.. in order to generate ciphertext. Same can be understood for other keys.

Encryption :-

Steps -

  1. Select the key.
  2. Write down table for a particular key or keep the table given above.
  3. Observe and replace characters using the encryption table.

Example -

Let’s encrypt “HELLO” using a Caesar cipher with key=3.

1 & 2. Table for key=3.

3.

Decryption :-

Steps -

  1. Select the key.
  2. Write down table for a particular key or keep the table given above.
  3. We just have to do the reverse process of encryption. Use the third column table as cipher text and replace those alphabets with the corresponding first column of the table in order to get the plaintext.

Example -

Let’s decrypt “KHOOL” using a Caesar cipher.

1 & 2. Table for key=3.

3.

Security :-

This cipher is not secure even if we will keep our key secret. Because keys can take values only between 1 and 26. And at key=26 our wheel or shift will be back to original place and we won’t get any difference in plaintext or ciphertext. If we go beyond 26, our key will wrap around and we will get results for key = 27 to 53 same as for the key = 1 to 26 accordingly. Why?

Let’s look at the mathematical aspects of what’s going on here. Suppose we denote ‘A’ as 0, ‘B’ as 1, ‘C’ as 2 up-to ‘Y’ as 24 and ‘Z’ as 25. We can say we are denoting using indexes.

Now, we can compute encryption and decryption using formula.

E(x) = (x + n) mod26

D(x) = (x — n) mod26

Where x is our corresponding digit (index) of character of plaintext or ciphertext and n is a value of key. You can verify the above formulas.

We did mod26 for both encryption and decryption so beyond that, our key will wrap around and start with 0 again.

So It is quite easy to break a Caesar cipher whether we have a key or not. There are only 26 possible values of key. And hence there are only 26 possibilities of ciphertext for any plaintext encrypted using this cipher. Brute Forcing it is a piece of cake.

Scripts:

1. Caesar Encryption and Decryption plus brute forcing.

Link : https://github.com/Amangondaliya555/Crypto/blob/master/PyCaesar/PyCeaser.py

--

--