Obfuscation using Caesar cipher and ROT13 in Python

Partha Pratim Nayak
4 min readJan 11, 2022

--

Photo by Nemanja Peric on Unsplash

A Caesar cipher is an ancient trick where you just move every letter forward three characters in the alphabet. Here is an example:

  • Plaintext: ABCDEFGHIJKLMNOPQRSTUVWXYZ
  • Ciphertext: DEFGHIJKLMNOPQRSTUVWXYZABC

So, HELLO becomes KHOOR.

To implement it in Python, we’re going to use the string.find() method. The interactive mode of Python is good for testing new methods, hence it’s easy to create a string. You can make a very simple script to implement the Caesar cipher with a string namedalphafor alphabet. You can then take input from the user, which is the plaintext method, then set a value,n, which equals the length of the string, and the string out is equal to an empty string. We then have a loop that goes through ’n’ repetitions, finding the character from string in and then finding the location of that character in the alpha string. It then prints out those three values so that we can make sure that the script is working correctly, then it adds3to loc(location) and puts the corresponding character in string out, and again prints out partial values so that we can see that the script is working correctly. In the end, we print our final output. Adding extra print statements is a very good way to begin your programming because you can detect mistakes.

Implementing the Caesar cipher in Python

Let’s look at the following script that implements Caesar cipher in Python. We will use Python in interactive mode first and then make a string that just has some letters in order to test this method:

Caesar Cipher (with static shift value)
Output of Caesar Cipher

When we run this script, it takes the input of HELLO and it breaks it up character by character so that it processes each character on a separate line. H is found to be the 7th character, so adding 3 gives us10, which results in K (from the alpha character). It shows us character by character how it works. So, the first version of the script is a success.

The shift variable of 3 is currently hardcoded in the above script. We need to be careful with that value. We need to make sure that the shift variable does not lead us to get the string index out of range error. Instead of providing a static shift value and solving the problem in perpetuity is use % 26, which will calculate the number modulus26, which will prevent it from ever leaving the range of 0 through 25. It will divide it by 26 and keep only the remainder, as expected in this case.

Caesar Cipher (with user entered shift value)

Deciphering a Caesar Cipher

We can decipher by just trying all the shift values from 0 to 25, and one of them will just be readable. This is a simple brute-force attack. If we put it in KHOOR, it’ll shift it by a variety of values, and we can see the one that’s readable at 23, which is HELLO.

ROT13

ROT13 is nothing more than a Caesar cipher with a shift equal to 13 characters. In the script that follows, we will hardcode the shift to be 13. If we run one cycle of ROT 13, it changes HELLO to URYYB, and if we encrypt it again with the same process, putting in that URYYB, it’ll turn back into HELLO, because the first shift is just by 13 characters and shifting by another 13 characters takes the total shift to 26, which wraps right around, and that is what makes this one useful and important.

--

--