We all know that Julius Caesar is a Roman dictator , who is also notable for his initial cryptography studies.The one thing all of us are unaware is hundreds of trees were cut down in 50 BC to provide Cipher wheels to all the Roman generals.A Cipher wheel is a data encrypting device that use Caesar cipher algorithm which gave the base idea for all the modern encryption technologies.

### Little past

The Roman ruler Julius Caesar (100 B.C. – 44 B.C.) used a very simple cipher for secret communication. He substituted each letter of the alphabet with a letter three positions further along. Later, any cipher that used this “displacement” concept for the creation of a cipher alphabet, was referred to as a Caesar cipher. Of all the substitution type ciphers, this Caesar cipher is the simplest to solve, since there are only 25 possible combinations.

### What is a Cipher wheel ?

A cipher wheel is an encrypting device that consists of two concentric circles inner circle and outer circle.The inner circle is fixed and outer circle is rotated randomly,so that it stops at some point.Then ‘A’ of outer circle is tallied with the position of ‘A’ of inner circle.That position is considered as key and the mapping of all the positions of outer and inner circles is used as encrypting logic.

here key = 3 ,since ‘A’ of outer circle is on ‘D’ of inner circle

### Why Julius Caesar wonder if he is alive ?

If encrypting message is small, then it can be encrypted using a Cipher disk by hand.But if message consists of thousands of lines, then computing power can only make it as easy as a ‘Home Alone’ task.Unfortunately Caesar doesn’t have a computer and a python interpreter in it to do that.If he is alive,he might have been wondered how simple it is to implement any mathematical algorithm in python.We here now building a Cipher wheel in python, a minimal encryption program for communicating secrets.

### Ready,set go. build it

```
#cipherwheel.py
import string
from random import randrange
#functions for encryption and decryption
def encrypt(m):
#define circular wheels
inner_wheel = [i for i in string.lowercase]
outer_wheel = inner_wheel
#caluclate random secret key
while True:
key = randrange(26)
if key!=0:
break
cipher_dict={}
#map the encryption logic
original_key =key
for i in range(26):
cipher_dict[outer_wheel[i]] = inner_wheel[key%26]
key = key+1
#getting encrypted message
print 'Encrypted with secret key ->> %d\n'%original_key
cipher = ''.join([cipher_dict[i] if i!=' ' else ' ' for i in m])
return cipher,original_key
def decrypt(cipher,key):
inner_wheel = [i for i in string.lowercase]
outer_wheel = inner_wheel
cipher_dict={}
for i in range(26):
cipher_dict[outer_wheel[i]] = inner_wheel[key%26]
key = key+1
#decryption logic
reverse_dict = dict(zip(cipher_dict.values() , cipher_dict.keys()))
#getting original message back
message = ''.join([reverse_dict[i] if i!=' ' else ' ' for i in cipher])
return message
#Using cipher wheel here
while True:
s = raw_input("Enter your secret message:")
encrypted = encrypt(s)
print 'encrypted message ->> %s\n'%(encrypted[0])
print 'decrypted message ->> %s\n'%decrypt(encrypted[0],encrypted[1])
```

This is the small basic encryption system that uses Caesar cipher as its algorithm.Let us do anatomy of program and try to understand how it was built.

### Anatomy of above Caesar wheel

First let us design encrypt function with cipher wheel.It is analogous to encrypt() function in our program caesarcipher.py

We need an inner wheel,an outer wheel initialized with 26 alphabets.So for that use string module variable string.lowercase that returns ‘abcd……xyz’.So we are splitting it to get list of alphabets.

```
import string
inner_wheel = [i for i in string.lowercase]
outer_wheel = inner_wheel
```

so now both outer and inner circles are initialized with list of alphabets.Now when outer circle is rotated it should stop at some random point which is key of the algorithm.

```
from random import randrange
#rotating outer circle i.e generating random key
while True:
key = randrange(26)
if key!=0:
break
```

Here program is rotating the outer circle and generating a random key which is used to encrypt message.While encrypting i lose key value,so we are making backup for it.

`original_key =key`

Now we need to create a mapping dictionary that maps the ‘a’ of outer circle to the respective alphabet of the inner circle at the position of key.For example if key=2,then ‘a’ of outer circle is mapped with ‘c’ of inner circle because c has the ‘2’ index in the list.This mapping procedure is done with the below code.

```
cipher_dict={}
for i in range(26):
cipher_dict[outer_wheel[i]] = inner_wheel[key%26]
key = key+1
```

By this a mapping dictionary according to a randomly generated key is formed.Now we need to use this dictionary to translate original message into secret message.

```
cipher = ''.join([cipher_dict[i] if i!=' ' else ' ' for i in m])
return cipher,original_key
```

cipher is the secret message created by the encryption mapping dictionary cipher_dict.Next we are sending both cipher,randomly generated key from encrypt() function

Decryption process is similar but we need to reverse map the dictionary in order to get original message.This intelligent one line tweak shows the expressive power of python.

```
#reverse map the dictionary
reverse_dict = dict(zip(cipher_dict.values() , cipher_dict.keys()))
#get original message from cipher
message = ''.join([reverse_dict[i] if i!=' ' else ' ' for i in cipher])
```

So the final output for the program look like this.