Skip to content

Instantly share code, notes, and snippets.

@alpenmilch411
Created September 14, 2015 12:21
Show Gist options
  • Save alpenmilch411/d2e65f77ce19b97a32af to your computer and use it in GitHub Desktop.
Save alpenmilch411/d2e65f77ce19b97a32af to your computer and use it in GitHub Desktop.
ROTX encode/decoder
#ROTX-Encoder/Decoder
# Functions:
def encode(string, integer):
rotated = []
for x in string:
rotated.append(ord(x) + integer)
translated = ''
for x in rotated:
translated += chr(x)
return translated
def back_to_menu():
import time
print('\nBack to menu.')
for x in range(3):
print('.')
time.sleep(0.5)
def decode(string, integer):
rotated = []
for x in string:
rotated.append(ord(x) - integer)
translated = ''
for x in rotated:
translated += chr(x)
return translated
def write_to_file(txt):
option = input('Do you want to save to a text file? y/n\n')
if option.lower() == 'y':
name = input('Enter file name: \n')
file = open(name, 'w')
file.write(txt)
file.close()
back_to_menu()
elif option.lower() == 'n':
back_to_menu()
return
print('\n\nWelcome to the ROTX-encoder\n')
while True:
# Options in menu
option = input('Do you want to encode, decode or exit the program.\n')
#Encoding - From file or manual input?
if option.lower() == 'encode':
input_type = input('''Options:
a) Read from file
b) Enter text manually \n''')
#Encoding text from data
if input_type == 'a)' or input_type == 'a' or input_type.lower() == 'read from file':
file = input('Enter name of text file.\n')
string = open(file, 'r')
integer = int(input('Enter key. \n'))
print(encode(str(string), integer))
string.close()
write_to_file(encode(str(string), integer))
#Encoding manually entered text
elif input_type == 'b)' or input_type == 'b' or input_type.lower() == 'Enter text manually \n':
string = input('Enter string \n')
integer = int(input('Enter key. \n'))
print(encode(string, integer))
write_to_file(encode(string, integer))
#Decode - From file or manual input?
elif option.lower() == 'decode':
input_type = input('''Options:
a) Read from file
b) Enter text manually \n''')
#Decoding text from file
if input_type == 'a)' or input_type == 'a' or input_type.lower() == 'read from file':
file = input('Enter name of text file.\n')
string = open(file, 'r')
integer = int(input('Enter key. \n'))
print(decode(str(string), integer))
string.close()
write_to_file(decode(str(string), integer))
#Decoding manually entered text
elif input_type == 'b)' or input_type == 'b' or input_type.lower() == 'Enter text manually \n':
string = input('Enter string \n')
integer = int(input('Enter key. \n'))
print(decode(string, integer))
write_to_file(decode(string, integer))
#Exit program
elif option.lower() == 'exit':
break
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment