ROT13 is a simple encryption method that shifts the string x (clear text) by 13 positions in the alphabet.
This simple Python one-liner does ROT13 encryption for you:
abc = "abcdefghijklmnopqrstuvwxyz" "".join([abc[(abc.find(c)+13)%26] for c in x]).
This Python library call does ROT13 encryption in Python:
import codecs codecs.encode(phrase, 'rot_13').
What is ROT13?
The ROT13 algorithm is a simple encryption algorithm. It is used in many forums (for example Reddit) to prevent spoilers – or to hide the semantics of a communication from newbies. (Please find a separate ROT13 application section below.)
ROT13 is so simple that it practically provides no security. But it helps you to finally understand these insider conversations on Reddit.
The algorithm can be explained in one sentence. ROT13 = Rotate the string to be encrypted by 13 positions (modulo 26) in the alphabet of 26 characters.
In other words, you want to encrypt a string. You shift each character by 13 positions in the alphabet. When shooting over the last character “z”, you start over at the first position in the alphabet “a”.
What are ROT13 implementations in Python?
I will first give you a simple Python implementation of the ROT13 algorithm that is easily understandable. Then, I provide you a short and concise Python one-liner. Finally, I give you the library call for ROT13 encryption. Take the version you prefer.
So check out this ROT13 algorithm in very simple Python code without using libraries. Read the code carefully, because I will ask you a question about the code in a moment.
def rot13(phrase): abc = "abcdefghijklmnopqrstuvwxyz" out_phrase = "" for char in phrase: out_phrase += abc[(abc.find(char)+13)%26] return out_phrase phrase = "xthexrussiansxarexcoming" print(rot13(phrase)) # kgurkehffvnafknerkpbzvat print(rot13(rot13(phrase))) # What's the output?
The last print statement showcases a nice property of the algorithm. Which one?
The solution to the puzzle is the output “thexrussiansxarexcoming” because the function rot13 is its own inverse function (shifting by 13+13 positions brings you back to the original output).
An advanced coder will always prefer the shortest and cleanest way of writing Pythonic code. So, let’s rewrite the ROT13 algorithm as a Python one-liner.
abc = "abcdefghijklmnopqrstuvwxyz" def rt13(x): return "".join([abc[(abc.find(c)+13)%26] for c in x]) print(rt13(rt13(phrase)))
We create a list of encrypted characters and join this list to the final result. The code uses list comprehension to create the list. It encrypts each character from the original string x separately. How? Simply by replacing it with the character 13 positions on the right in the alphabet.
Note that it is crucial to prevent overshooting for all characters in the alphabet with index>13. To do this, we use the modulo operator to ensure that we restart on the left with index 0.
Alternative solution: one of my “Coffee Break Python” email subscribers, Thomas, came up with an alternative solution that is fast and easy to read, too.
def rot13(phrase): key = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" val = "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM" transform = dict(zip(key, val)) return ''.join(transform.get(char, char) for char in phrase)
The idea is to “hardcode” the mapping between keys and values which is a bit more “tedious” programming work but is still a perfectly valid solution to the ROT13 algorithm (and it works for Uppercase, too).
Is there a library for ROT13 in Python?
Yes! The library is called ‘codecs’. Using the ROT13 algorithm with the library is as simple as importing the library via ‘import codecs’ and executing the ‘encode(…)’ function.
Here is an example:
import codecs phrase = "xthexrussiansxarexcoming" print(codecs.encode(codecs.encode(phrase, 'rot_13'), 'rot_13')) # xthexrussiansxarexcoming print(codecs.encode('hello', 'rot_13')) # uryyb
The encode function of the codecs library takes up to three parameters. The first parameter is the string object to encode. The second parameter is the encoding scheme (default: ‘utf-8’). The third parameter allows you to customize the error handling. In most cases, you can skip the last parameter and use the default error handling.
What are the applications of the ROT13 algorithm?
The ROT13 algorithm is easy to decrypt. An attacker can easily crack your code by running a probabilistic analysis on the distribution of the letters in your encrypted text. You should never rely on this algorithm to encrypt your messages!
Then, what are applications of the ROT13 algorithm?
- Obscure potentially offensive jokes in online forums.
- Obscure the result of puzzles in online forums.
- Obscure possible spoilers for movies or books.
- Make fun of existing (weak) encryption algorithms: “56-bit DES is stronger than ROT13”
- Obscure emails addresses on websites against not very sophisticated email bots (the 99%).
- Use it as a game to find phrases that make sense in both forms, encrypted or decrypted. Examples: (png, cat), (be, or).
- ROT13 is a special case of the popular Caesar cipher. ROT13 serves as an educational tool to explain it. (https://en.wikipedia.org/wiki/Caesar_cipher
In summary, ROT13 is more a fun encryption method that has been a popular running gag in the internet culture (in the good old days)…
As a user on StackExchange describes it:
“So a lot of things that did mild obfuscation used ROT13, because it was commonly available, and so it’s been backported into a number of more modern languages. It’s just a weird quirk.”
How is CAPITALIZATION handled (uppercase and lowercase)?
The encode function of the codecs library handles capitalization for you. If you apply ROT13 to an uppercase letter, it will remain uppercase after the encoding. If you apply ROT13 to a lowercase letter, it will remain lowercase.
Here is an example:
import codecs print(codecs.encode('Hello', 'rot_13')) # Uryyb
How is ROT13 related to the Caesar cipher?
The Caesar cipher is the generalization of the ROT13 algorithm. So what’s the Caesar cipher?
“It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet.”Wikipedia
As you can see, ROT13 does nothing but fixing the “number of positions down the alphabet” to +13.
Why shifting the clear text (also called ‘plaintext’) by exactly 13 positions? This ensures that applying the encryption two times leads to the original clear text. Hence, you do not have to define two separate methods for encryption and decryption – one method to rule them all.
Obviously, this is not the case, if you use – for example – ROT5 where you shift the clear text only by 5 positions. If you apply ROT5 twice to the same clear text, you get ROT10 encryption (5+5=10).
Online Tool for ROT13 encryption and decryption
To encrypt your own clear text, simply replace the string value of variable ‘clear_text’ with your personal string.
What are alternatives of ROT13?
Most alternatives are stronger than ROT13. Here are a few of them:
- Triple DES
If you want to dive deeper into these alternatives, check out this article that briefly describes their ideas. https://blog.storagecraft.com/5-common-encryption-algorithms/
What are examples of ROT13?
Here are examples from various sources in the web. I tried to focus on examples where the ROT13 encryption produces some kind of english word as well.
- aha ↔ nun
- ant ↔ nag
- balk ↔ onyx
- bar ↔ one
- barf ↔ ones
- be ↔ or
- bin ↔ ova
- ebbs ↔ roof
- envy ↔ rail
- er ↔ re
- errs ↔ reef
- flap ↔ sync
- fur ↔ she
- gel ↔ try
- gnat ↔ tang
- irk ↔ vex
- clerk ↔ pyrex
- purely ↔ cheryl
- PNG ↔ cat
- SHA ↔ fun
- furby ↔ sheol
- terra ↔ green
- what ↔ Jung
- URL ↔ hey
- purpura ↔ Chechen
- shone ↔ FUBAR
- Ares ↔ Nerf
- abjurer ↔ nowher