The early days of encryption using Python — Caesar’s Cipher (Part 1)
By: Akhmad Sofwan
Tags: Caesars Cipher, Encryption, Decryption

Photo by Markus Spiske on Unsplash

Encryption is a popular term and used widely in Information Technology. Encryption is a method of protecting data from people you don’t want to access it [1]. The protected data can be chat, e-mail, file and database. In chat, a Chat Messenger have to apply encryption for protecting chat from read by people who don’t have right to read the chat by intercepting chat in the middle of chat tunnel communication system.It also to protect user privacy and security in chatting to make the user comfortable to use the chat messenger. An application which use password and save the password in a table of database is encouraged to encrypt the password before save it in a table, so even an Application Administrator or a Database Administrator who can access the database doesn’t know what the password is although he can change it because he has full access in the database.

If you want to encrypt data or text, you change your text by using a key which is a method or an algorithm, so the text becomes into something can’t be understood. The people you sent the data must have a certain key to decrypt or translate it for understanding the text.

Today encryption algorithm is more complex and difficult to understand than early days encryption. There are some modern algorithms, such as TripleDES, Two fish Encryption and Blowfish encryption which are some of the strongest encryption algorithms [2].

In this serial articles, I would like to explain an encryption algorithms in early days. First is Caesar’s cipher who used by Julius Caesar [2], a Roman general and politician who assassinated by political rivals in 44 BC [3] and the second is Polybius square which is still in draft as the second article. Both of them I implemented with Python.

Caesar’s cipher is an encryption technique by shifting each letter of the alphabet to the right or left by a number of positions . For example by using the cipher below, if you write BLACK, it becomes EOAFN. [2]

Plain : ABCDEFGHIJKLMNOPQRSTUVWXYZ

Cipher : DEFGHIJKLMNOPQRSTUVWXYZ

It is simple to translate from plain to encrypted text by using such Cipher and we can do it manually especially when the text is short but if the text is long it needs a big effort to convert it manually. To make our task easier, I created a Python script so we can encrypt a long words much easier than manually.I also add Python script to decrypt the encryption text.

The algorithm to write Caesar’s Cipher as follows:

1. Create list of plain Alphabet (named plainnya), begin from A until Z.

2. Create list of cipher (named ciphernya) by shifting left for 3 alphabets,

so it begins from D until C.

3. Initiate list variable (named bil_huruf) to store index of input text

4. Input text

5. Convert the input text becomes upper text

6. Find index number of Input text based on plain index.

7. Print out alphabet of ciphernya based on input text index

And the code as follows :

plainnya=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","A","B","C"," "] ciphernya=["D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S",

"T","U","V","W","X","Y","Z"," "]



On the code above, we create plainnya list and ciphernya list. Here, we shift 3 alphabets to left and you can modify it how many alphabets you want to shift to right or left.

bil_huruf=[]

sente=input("Sentence :")

sentence_up=sente.upper()

We create list named bil_huruf, creating an input text and convert it Uppercase to every alphabetical we type to make it similar with plainnya list and ciphernya list content

for i in range(len(sentence_up)):

for j in range(len(plainnya)):

if (sentence_up[i]==" "):

bil_huruf.append(26)

break



if plainnya[j]==sentence_up[i]:

bil_huruf.append(j)

Then we compare between every member of plainnya list and sentence_up list. If the alphabet from those list is same, we save the number of index to bil_huruf list by using append.If there is “space” we append it on index 26 which it is space in ciphernya list. For example if we input RED FISH, so the index in bil_huruf contains 17,4,3,26,5,8,18,7.

R=17, E=4,D=3,space=26,F=5,I=8,S=18,H=7.

print("Result :")

for k in range(len(bil_huruf)):

print(ciphernya[bil_huruf[k]],end=" ")

After that, we print every index we already have in bil_huruf list with ciphernya list, so from text : RED FISH will become : UHG ILVK

Image 1 : Result of Encryption

For the decryption code, it reverse, make the encryption result as Input and decrypt with plain. The result for text : UHG ILVK as follows :

Image 2 : Result of Decryption

And the code for decryption as follows :

# Decryption 1 - Caesar's Cipher plainnya=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q",

"R","S","T","U","V","W","X","Y","Z"," "]

ciphernya=["D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S",

"T","U","V","W","X","Y","Z","A","B","C"," "] bil_huruf=[]

sente=input("Sentence :")

sentence_up=sente.upper() for i in range(len(sentence_up)):

for j in range(len(ciphernya)):

if (sentence_up[i]==" "):

bil_huruf.append(26)

break



if ciphernya[j]==sentence_up[i]:

bil_huruf.append(j) print("Hasil :")

for k in range(len(bil_huruf)):

print(plainnya[bil_huruf[k]],end=" ")

The code, I also save on my github.

https://github.com/sofwanbl/encrypt-decrypt-1

Conclusion

Caesar’s cipher is an early days of encryption and easy to understand and not implemented on modern encryption because there are any other modern encryptions more secure, but by understanding the early days encryption we understand how encryption works.Caesar’s cipher can be implemented using a programming language and this article implemented it by Python.

Reference

[1] Whitson Gordon, A beginner’s guide to Encryption: What it is and how to set it up, Jan 27 2014. Accessed Apr 17 2021 [Online]. Available : https://lifehacker.com/a-beginners-guide-to-encryption-what-it-is-and-how-to-1508196946.

[2] Rithesh Raghavan, What is data encryption ? which all are the top encryption algorithms ?, Jan 17 2020. Accessed Apr 17 2021 [Online]. Available : https://acodez.in/data-encryption-algorithms/

[3] National Geographic Society, Julius Caesar, July 6 2018. Accessed Apr 17 2021 [Online].

Available : https://www.nationalgeographic.org/encyclopedia/julius-caesar/