Image showing sample run of ROT13 encoder decoder

ROT13 is a letter substitution cipher and a special case of Caesar Cipher where each character in the plain text is shifted exactly 13 places. If you are not aware of Caesar Cipher then look at Caesar Cipher. For example the cipher for SUN becomes FHA.

The cool thing about this technique is that if we do a ROT13 on the cipher text then we get back the plain text since each letter in the text is shifted by 13 places. For example when we do a ROT13 on FHA we get back SUN

 

A block representation of ROT13 encryption and decryption

A block representation of ROT13 encryption and decryption

 

ROT13 Encoder and Decoder

The encoder and Decoder for ROT13 is the same because there is no special logic during decoding since the shift for both encoding and decoding is the same. Below is the python code for the implementation of it. The code is pretty much the same as Caesar Cipher with the shift value set to 13 always.

 

alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

input_str = raw_input("Enter message that you would like to encrypt/decrypt using ROT13: ")
shift = 13
no_of_itr = len(input_str)
output_str = ""

for i in range(no_of_itr):
    current = input_str[i]
    location = alphabets.find(current)
    if location < 0:
        output_str += input_str[i]
    else:
        new_location = (location + shift)%26
        output_str += alphabets[new_location]

print "Here's the output: ", output_str

Here’s a sample run of ROT13

Image showing sample run of ROT13 encoder decoder

Image showing sample run of ROT13 encoder decoder

 

The entire source code for this post can be found at https://github.com/abhishuk85/cryptography-plays

Any questions, comments or feedback are most welcome.