Skip to content

Instantly share code, notes, and snippets.

@obikag
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save obikag/8fda145dc664c3956a35 to your computer and use it in GitHub Desktop.
Save obikag/8fda145dc664c3956a35 to your computer and use it in GitHub Desktop.
ROT13 substitution cipher implemented in Python. ROT13 method encrypts and decrypts a given string. Special characters and numbers are not encrypted with this cipher.
'''
Created on Feb 24, 2015
'''
import re
# Create a dictionary to store the cipher
cipher = {
'A':'N','B':'O','C':'P','D':'Q','E':'R','F':'S','G':'T','H':'U','I':'V','J':'W','K':'X','L':'Y','M':'Z',
'N':'A','O':'B','P':'C','Q':'D','R':'E','S':'F','T':'G','U':'H','V':'I','W':'J','X':'K','Y':'L','Z':'M',
'a':'n','b':'o','c':'p','d':'q','e':'r','f':'s','g':'t','h':'u','i':'v','j':'w','k':'x','l':'y','m':'z',
'n':'a','o':'b','p':'c','q':'d','r':'e','s':'f','t':'g','u':'h','v':'i','w':'j','x':'k','y':'l','z':'m'
}
#Method encrypts and decrypts using ROT13
def ROT13(ustr):
estr = ''
for c in ustr: #for each character in the string
if re.match('[a-zA-Z]',c):
estr = estr + cipher[c]
else:
estr = estr + c
return estr
#Test Section
print(ROT13('Hello World')) # Uryyb Jbeyq
print(ROT13('The quick brown fox jumps over the lazy moon')) # Gur dhvpx oebja sbk whzcf bire gur ynml zbba
print(ROT13('Are you hungry??')) # Ner lbh uhatel??
print(ROT13('Hey!!')) # Url!!
print(ROT13('test@test.com')) # grfg@grfg.pbz
print(ROT13('Qrpelcgrq')) # Decrypted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment