import random #This lets me call the random function which calls a random number which #you can specify to be an integer or float. import time #This lets me pause the code for certain periods of time so that it is easier #for the user to read. import os.path #This lets me check if a file exists, so that there is no error. def Save(): global encryptedfile global key #This calls the variables "encryptedfile" and "key" saving = input("Would you like to save the encrypted text in a file? (Y/N)\n") saving = saving.lower() #This sets the variable saving as a user input to check if they would like to save a file if saving == "y" or saving == "yes": #This checks if the response was y or Yes and if so, it saves the file. filename = input("What would you like to save the file as \n(Note, the file extention will be added):\n") #This asks the user what they would like to save the file as, #Then sets it as the file's name with .txt added on file_save = filename+".txt" print ("It is saved as '"+file_save+"' in the same folder as this program.") f = open(file_save, 'w') #This opens or creates the file and then joins the contents that is to be saved #As the variable Contents. contents = ''.join(encryptedfile) print (contents) f.write(contents) f.close() #This copies the variable "contents" into the file, which is then saved. key = "','".join(key) #This joins the key with a "','" between them, so that they can see each of the characters. print("Here is your key, the person using this to decrypt the message will need it.") print("So write it down") print("'"+key+"'\n") #This outputs the key, with the layout "'a','b','c','d','e','f','g','h'" main() #This runs the program "main()" elif saving == "no" or saving == "n": print ("Okay, here's the contents of the file") #This checks if the user said "no", or "n" and if they did #it outputs the contents and the key contents = ''.join(encryptedfile) print (contents) key = "','".join(key) print("\nHere is your key, the person using this to decrypt the message will need it.") print("So please write it down") print("'"+key+"'\n") time.sleep(0.5)#This pauses the code for half a second so it is easier for the user. main()#This runs the function "main" else: print("You have not entered a valid input, please try again") time.sleep(0.5) Save()#This runs the function "Save" def EncryptSpaces(): print("What is the name of the file that you would like to encrypt the spaces of?") filename = input("(Note the extention will be added '.txt')\n") #This asks the user to input a filename filename = (filename+".txt") #This sets the filename as a file if os.path.isfile(filename) == True: #This checks if there is a file with the filename contents = [] #If there is a file with the name specified, it opens the specified file file = open (filename,'r') for word in file:#This small function breaks down the file into words, for character in word:#then the words into characters then contents.append (character)#appends the characters into the list "contents" encryptedfile = [] spaces = 0 filesave = "" contents2 = [] for character in contents: if character != " ": #This removes all spaces in the file. contents2.append(character) for character in contents2: spaces = spaces +1 spaces = int(spaces) if spaces != 5: #This checks if the variable spaces is not equal to 5 encryptedfile.append(character) #if the variable spaces does not equal 5, it appends the character to #the variable "encryptedfile" and adds one to variable "spaces" elif spaces == 5: #this checks if the variable spaces is equal to 5 which, #is every 5 rounds, so there is 5 characters before each space. spaces=0 encryptedfile.append (character) encryptedfile.append (" ") #cut here filesave=''.join(encryptedfile) #This joins the list "encryptedfile" #into one variable. save = input("\nWould you like to save this file?Y/N\n") save = save.lower() #This sets the variable save as lowercase, so there are less #Parameters and it is more user friendly. if save == "y" or save == "yes": filename = input("What would you like the file to be called?") #If the user inputs "y", or "yes", it asks the user what they #would like their file to be called filename = filename+".txt" print ("It is saved as '"+filename+"' in the same folder as this program.") f = open(filename, 'w') f.write (filesave) f.close() #This opens the file, it writes the data into it, then closes the file. time.sleep(0.5) main() elif save == "n" or save == "no": print ("Here is your file that has been encrypted with spaces\n") #if the user inputted "no", or "n", it outputs the file contents print (filesave) print ("") time.sleep(0.5)#This also makes it easier for the user main() else: print("The file does not exist") #If the file does not exist, the program will run "encryptspaces()" time.sleep(0.5) EncryptSpaces() def Decrypt(): print("What is the file that you'd like to have decrypted?") file_to_decrypt = input("(Note the extention will be added '.txt'):\n") #This asks the user to input their desired name for the file. filename = (file_to_decrypt+".txt") key = [] #This checks if the file exists if os.path.isfile(filename) == True: contents = [] file = open (filename,'r') #This breaks the file down to characters. for word in file: for character in word: contents.append (character) print("Please enter the key for the file that you would like to be the decrypted?") print("Note, this is case sensitive") key = input("And if you make a mistake it will not display the right message:\n") #This asks the user for the key key2 = [] listofoffset = [] #This breaks the key into each of it's characters and #appends them to a key list. for character in key: key2.append (character) chara = "" for character in key2: chara = ord(character) listofoffset.append (chara) offset_val = 0 #This sets the key as their ascii values, then works out #the offset value. for x in list(listofoffset): offset_val = offset_val+x offset_val = offset_val/8 offset_val = int(offset_val) offset_val = offset_val - 32 decrypted_file=[] #Cut here char = "" for x in contents: char = ord(x) #This sets the characters as their ascii value if char == 32: char = chr(char) decrypted_file.append (char) #This checks if the character is a space #if it is, it puts it into the variable char. else: char = char-offset_val if char < 32: char = char+94 char = chr(char) decrypted_file.append (char) else: char = chr(char) decrypted_file.append (char) saving = input("Would you like to save the decrypted text in a file? (Y/N)") saving = saving.lower() if saving == "yes" or saving == "y": filename = input("What would you like to save the file as \n(Note, the file extention will be added):\n") file_save = filename+".txt" print ("It is saved as '"+file_save+"' in the same folder as this program.") f = open(file_save, 'w') contents = ''.join(decrypted_file) f.write(contents) f.close() time.sleep(2) main() elif saving == "no" or saving == "n": print ("Okay, here's the contents of the file\n") contents = ''.join(decrypted_file) time.sleep(1) print (contents) time.sleep(2) print ("") main() else: print("The file is not found. The decrypt function will be called again") time.sleep(1) Decrypt() def offset(): global contents global offset_val global key global encryptedfile encryptedfile = [] for c in list(contents): chara = ord(c) if chara == 32: char = chr(chara) encryptedfile.append (char) else: chara = int(chara) + int(offset_val) if chara > 126: chara = (chara - 94) char = chr(chara) encryptedfile.append (char) else: char = chr(chara) encryptedfile.append (char) Save() def Encrypt(): global contents global offset_val global key file_to_encrypt = input("What is the file that you'd like to have encrypted?\n(Note the extention will be added '.txt'):\n") filename = (file_to_encrypt+".txt") key = [] if os.path.isfile(filename) == True: contents = [] file = open (filename,'r') for word in file: for character in word: contents.append (character) listofoffset = [] for i in range (8): num = random.randint (33, 126) listofoffset.append (num) offset_val = 0 for x in list(listofoffset): offset_val = offset_val+x char = chr(x) key.append (char) offset_val = offset_val/8 offset_val = int(offset_val) offset_val = offset_val - 32 offset() else: print ("Oops, file is not found.") time.sleep (0.5) Encrypt() def Exitting(): print("Okay then, it's your loss...") time.sleep(0.5) exit()#This closes the window def main(): print("Welcome to the python encryption program.\nHere, you can encrypt, decrypt and encrypt spaces on your file.") select = input('If you would like to Encrypt a file, please enter "Encrypt"\nIf you would like to Decrypt an encrypted file, please enter "Decrypt"\nIf you would like to encrypt spaces on a file, please enter "Spaces"\nFinally, if you would like to close the app, please enter "Exit":\n') if select == "Encrypt" or select == "encrypt": Encrypt() elif select == "Decrypt" or select == "decrypt": Decrypt() elif select == "Spaces" or select == "spaces": EncryptSpaces() elif select == "Exit" or select == "exit": Exitting() else: print ("You have not entered a correct input, please enter one of the options specified") time.sleep(1) main() main()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question