ROT13 - Econowmics
Foto von Rachel Claire von Pexels

[Python] Rot-13 Encryption Method


If he had anything confidential to say, he wrote it in cipher, that is, by so changing the order of the letters of the alphabet, that not a word could be made out. If anyone wishes to decipher these, and get at their meaning, he must substitute the fourth letter of the alphabet, namely D, for A, and so with the others.

Suetonius, Life of Julius Caesar


ROT13 - Econowmics
Foto von Rachel Claire von Pexels


The ROT-13 (i.e. rotate by 13 places) method is a very simple encryption method, in which one replaces each letter of the alphabet with the 13th letter after it. So, ‘A’ becomes ‘N’, ‘B’ becomes ‘M’ and so on. This method is a special case of the Caesar Cipher, with which Caesar encoded his letters in the ancient Rome.

Let’s write some simple code that can encode and decode given text files based on the ROT-13 method in Python!

Here is our approach for encoding a given string:

  1. First, all the letters of the given text are transformed to lower case letters.
  2. Then we loop through all characters, and if the character is a letter, we replace it with the 13th letter after it. If it is not a letter (for instance ‘?’), we just add it to the final solution.

Bear in mind that, after ‘z’ we start counting from ‘a’ again: for instance, ‘n’ is the 14th letter of the alphabet, so if we use ROT-13 on it, we arrive at 27, which is one after ‘z’, or ‘a’, and to do so in our code, we always consider the remainder of any number divided by 26. In our example the remainder of dividing 27 by 26 is 1, which is ‘a’.

We can write a function to decode the given string, but wait! There are 26 (2 * 13) letters in the alphabet, so ROT-13 is basically it’s own inverse. In other words: ROT13(ROT13(x)) = x. 


Let’s jump to the code:

[pastacode lang=”python” manual=”’abcdefghijklmnopqrstuvwxyz’%0A%0A%23%20Function%20to%20encrypt%20a%20text%20based%20on%20the%20Rot-13%20method%0Adef%20encrypt(text)%3A%0A%20%20%20%20%22%22%22The%20function%20receives%20a%20string%20as%20input%2C%20and%20encrypts%20it%20using%20the%20Rot-13%20method%20%22%22%22%0A%20%20%20%20%0A%20%20%20%20%23%20Transform%20the%20string%20to%20lower-case%20chars%0A%20%20%20%20text%20%3D%20text.lower()%0A%20%20%20%20%0A%20%20%20%20%23%20the%20encrypted%20message%0A%20%20%20%20result%20%3D%20”%0A%20%20%20%20%0A%20%20%20%20%23%20Replace%20each%20letter%20in%20the%20string%20with%20a%20letter%20which%20is%2013%20positions%20further%0A%20%20%20%20for%20char%20in%20text%3A%0A%20%20%20%20%20%20%20%20if%20char.isalpha()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20result%20%2B%3D%20alphabet%5B(alphabet.index(char)%20%2B%2013)%20%25%2026%5D%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20result%20%2B%3D%20char%0A%20%20%20%20return%20result%0A%20%20%20%20%20%20%20%20%0A%0A%0A%20%20%20%20%0A%20%20%20%20″ message=”” highlight=”” provider=”manual”/]


So, let’s try encoding a simple ‘Hello’:

ROT13 - Encryption Test -

And let’s also decode an encrypted message, by calling the very same function on it:

ROT13 - Test Decryption -