Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Text Encryption
- # Using external files to encrypt and decrypt messages
- # Procedures and functions
- # ===========================================
- def displayMenu():
- # procedure to display the text encryption menu
- print(
- """
- Text Encryption
- 0 - Exit the program
- 1 - Encrypt message
- 2 - Decrypt message
- """)
- def encryption():
- # Procedure that contains the Encryption code
- print("You have chosen encryption.")
- time.sleep(1)
- readfile()
- key()
- time.sleep(2)
- offset()
- time.sleep(2)
- convert()
- time.sleep(3)
- print("==================================")
- def decryption():
- # Procedure that contains the Decryption code
- print("You have chosen decryption.")
- time.sleep(2)
- print("Decrypt")
- def readfile():
- # Function that contains the code to read in a file.
- print("Make sure the file is in the same folder as the program.")
- # Ask the user to enter a filename
- fname = input("Enter the file name: ")
- # Check if the filename entered has .txt at the end. If not, add it on.
- if ".txt" not in fname:
- fname += ".txt"
- # Check if the file exists(returns a boolean)
- validfile = os.path.isfile(fname)
- # While loop that loops every time the user enters an invalid filename.
- while validfile == False:
- print("The file was not found. Please try again.")
- print("Make sure the text file is in the same folder as the program.")
- # Asks the user to enter a filename
- fname = input("Enter the file name: ")
- # Checks if the filename entered has a .txt at the end. If not, add it on.
- if ".txt" not in fname:
- fname += ".txt"
- # Check if the file exists(return a boolean)
- validfile = os.path.isfile(fname)
- # Open the file
- read_file = open(fname, "r")
- # Store the text in the file
- message = read_file.read()
- # Close the file
- read_file.close()
- # Inform the user that the text has been successfully saved.
- print("Text file has been saved.")
- print("\nThis is the message that is going to be encrypted:")
- time.sleep(2)
- print(message)
- time.sleep(1)
- global message
- def key():
- # Function that generates the 8 character ASCII key
- # Creates a list to store the key in a list
- keys = []
- # Alert the user that the program is generating their key.
- print("\nGenerating numbers..." * 3)
- time.sleep(2)
- # For loop that loops 8 times to produce a different ASCII code every time.
- for i in range(8):
- # Generate a random number between 33 and 127(including 33, excluding 127)
- key = random.randrange(33, 127)
- # Turn the integer into ASCII form.
- key = chr(key)
- # Append the ASCII to the list created earlier
- keys.append(key)
- # Alert the user that this is important and they need to store it.
- print("\nALERT! This is the key to decrypt your message for later. Please save it somewhere. (CASE SENSITIVE)")
- time.sleep(2)
- # Print the 8 letter key to the user.
- print("".join(keys))
- input("Press enter once you have written it down: ")
- global keys
- def offset():
- # Function that generates the offset factor
- time.sleep(1)
- print("\nGenerating offset factor...")
- # Create a variable that will be used later.
- number = 0
- # Create a for loop that takes out each character, turns it into an ASCII code, and adds it to the variable.
- for character in keys:
- ascii = ord(character)
- number = number + ascii
- # Divide the total number by 8 to find the average
- number = number / 8
- # Round the total number down to a whole number
- number = int(number)
- # Minus 32 from the total number
- number = number - 32
- global number
- print("This is the offset factor:", number)
- def convert():
- # Function that generates the offset factor
- # Create a variable that will be used later.
- finalmessage = ""
- # Use a for loop to take out each character from message
- for character in message:
- # Use an if statement to check if the character is a space.
- if character == " ":
- # If the character is a space, add it to the variable created before.
- finalmessage = finalmessage + character
- # Use else if the character is not a space.
- else:
- # Create a new variable with the ASCII code of each character.
- ASCIIcode = ord(character)
- # Add the offset factor to the ASCII code.
- ASCIIcode = ASCIIcode + number
- # Check if the total number is over 126.
- if ASCIIcode > 126:
- # If the total number is over 126, then minus 94 from it.
- ASCIIcode = ASCIIcode - 94
- # Turn the ASCII code into an actual character.
- ASCIIcharacter = chr(ASCIIcode)
- # Add the character to the final encrypted message.
- finalmessage = finalmessage + ASCIIcharacter
- time.sleep(2)
- print("This is your encrypted message:")
- time.sleep(1)
- # Print the final encrypted message
- print(finalmessage)
- # Data structures and variables
- # ===================================================
- choice = None
- import os.path
- import random
- import time
- # Main program and menu
- # =======================================
- # While statement that runs most of the code in the program.
- while choice != "0":
- displayMenu()
- choice = input("Choice: ")
- print()
- # This if statement allows the user to quit if they enter 0.
- if choice == "0":
- print("Goodbye.")
- # This elif statement calls the encryption procedure
- elif choice == "1":
- encryption()
- # This elif statement calls the decryption procedure.
- elif choice == "2":
- decryption()
- # Error catching else statement
- else:
- print("\nSorry, but", choice, "isn't a valid choice.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement