Created
September 13, 2012 20:49
-
-
Save astagi/3717539 to your computer and use it in GitHub Desktop.
Encode and decode letters and numbers using ROT13 and ROT5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# ROT: encode and decode letters and numbers using ROT13 and ROT5 | |
# Author: Andrea Stagi <stagi.andrea@gmail.com> | |
# License: c'mon default header, don't be silly :D | |
def rot_encode(clr_str): | |
rot_chars = [] | |
for ch in clr_str: | |
ch_dec = ord(ch) | |
if 65 <= ch_dec <= 90: | |
rot_chars.append(chr((ch_dec + 13) % 65 % 26 + 65)) | |
elif 97 <= ch_dec <= 122: | |
rot_chars.append(chr((ch_dec + 13) % 97 % 26 + 97)) | |
elif 48 <= ch_dec <= 57: | |
rot_chars.append(chr((ch_dec + 5) % 48 % 10 + 48)) | |
else: | |
rot_chars.append(ch) | |
return ''.join(rot_chars) | |
def rot_decode(enc_str): | |
rot_chars = [] | |
for ch in enc_str: | |
ch_dec = ord(ch) | |
if 65 <= ch_dec <= 90: | |
rot_chars.append(chr((ch_dec % 65 - 13) % 26 + 65)) | |
elif 97 <= ch_dec <= 122: | |
rot_chars.append(chr((ch_dec % 97 - 13) % 26 + 97)) | |
elif 48 <= ch_dec <= 57: | |
rot_chars.append(chr((ch_dec % 48 - 5) % 10 + 48)) | |
else: | |
rot_chars.append(ch) | |
return ''.join(rot_chars) | |
morning_str = "Good morning Mr. Andrea! How are you? Today is 28th of May!" | |
print "Initial string: '%s'" % morning_str | |
enc_str = rot_encode(morning_str) | |
print "Encoded string: '%s'" % enc_str | |
dec_str = rot_decode(enc_str) | |
print "Decoded string: '%s'" % dec_str |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment