Skip to content

Instantly share code, notes, and snippets.

@BlueNexus
Last active July 19, 2016 13:00
Show Gist options
  • Save BlueNexus/052712aea29135f72e4cfea21ab2bc2f to your computer and use it in GitHub Desktop.
Save BlueNexus/052712aea29135f72e4cfea21ab2bc2f to your computer and use it in GitHub Desktop.
44-line Python rot13 script
import string
charset = list(string.ascii_lowercase)
def rem_numbers(source):
new_string = []
to_remove = list(string.digits)
source = list(source.lower())
for char in source:
if char not in to_remove:
new_string.append(char)
return("".join(new_string))
def crypt(source, rot, param):
source_list = list(source)
result_list = []
for char in source_list:
try:
index = charset.index(char)
if param is True:
result_list.append(charset[(index + rot) % len(charset)])
else:
result_list.append(charset[(index - rot) % len(charset)])
except:
result_list.append(" ")
return("".join(result_list))
def main():
rot_by = int(input("Rotate by how many characters?:"))
while True:
rot_type = str(input("Encrypt or decrypt? (e/d):"))
rot_type = rot_type[0].lower()
if rot_type == "e" or rot_type == "d":
break
rot_string = rem_numbers(str(input("String to rot:")))
result = None
if rot_type == "e":
result = crypt(rot_string, rot_by, True)
else:
result = crypt(rot_string, rot_by, False)
print(result)
while True:
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment