Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import time
- import os
- import sys
- invalid_input = True
- filename = None
- key = None
- def menu():
- print("Welcome to the Encryption / Decryption program!\n-------------------------------------------")
- print("Please select a number (1-3) corresponding to the instruction you would like")
- menuInput = input("[1] Encrypt a message\n[2] Decrypt a message\n[3] Exit the program\n-------------------------------------------\n")
- if menuInput == '1':
- encrypt()
- elif menuInput == '2':
- decrypt()
- elif menuInput == '3':
- end = input("Are you sure you want to exit the program?\n[1] Yes\n[2] No\n")
- if end == '1':
- exit()
- elif end == '2':
- menu()
- else:
- print("Invalid selection. Returning to main menu.")
- time.sleep(2)
- menu()
- else:
- print("Invalid Selection")
- menu()
- def encrypt():
- global filename
- global key
- print("You have chosen to Encrypt a message!")
- invalid_input = False
- encrypt = input("Please type in the name of the file containing the non-encrypted message\n")
- encrypt = encrypt.upper()
- message = open(encrypt+".txt","r")
- message = message.read()
- if encrypt == "SAMPLE":
- key = ''
- for i in range(0,8):
- random_number = (random.randint(33,126))
- key+=str(chr(random_number))
- print(key)
- print("You will need this key to decrypt your file!")
- time.sleep(1.5)
- print("Encrypting your file now...")
- time.sleep(2.5)
- calculate = []
- for x in key:
- x = ord(x)
- calculate.append(x)
- numbers = sum(calculate)
- divided = numbers / 8
- rounded = round(divided-0.5)
- finalNumber = rounded - 32
- time.sleep(1)
- encrypted = ''
- spaces = ''
- for letter in message:
- valLetter = ord(letter)
- if valLetter == 32:
- encrypted += chr(valLetter)
- else:
- added = finalNumber + valLetter
- if added > 126:
- subtracted = added - 94
- encrypted += chr(subtracted)
- else:
- encrypted += chr(valLetter)
- print("Here is your encrypted message")
- time.sleep(1)
- print(encrypted)
- filename = input("Please enter the name you wish the file to be called: ")
- cipher_filename = filename + ".txt"
- f = open(cipher_filename,"w")
- f.write(encrypted)
- print("Thank you, you will be redirected to the main menu shortly where you can decrypt your message!")
- time.sleep(1)
- else:
- print("Invalid Filename!")
- def decrypt():
- print("You have chosen to decrypt a message!")
- invalid_input = False
- newFile = input("What is the name of your file you stored your encrypted message in?\n")
- decrypt = open(newFile+".txt","r")
- msg = ''
- for words in decrypt:
- msg += str(words)
- if newFile == filename:
- print("Correct!")
- keyAuth = input("Please enter the key we provided you with earlier!")
- if keyAuth == key:
- print("Success! Decrypting your message now.")
- calculate = []
- for x in key:
- x = ord(x)
- calculate.append(x)
- numbers = sum(calculate)
- divided = numbers / 8
- rounded = round(divided-0.5)
- offset = rounded - 32
- time.sleep(1)
- decrypted = ''
- spaces = ''
- for letter in msg:
- valLetter = ord(letter)
- if valLetter == 32:
- decrypted += chr(valLetter)
- else:
- subtracted = valLetter - offset
- if subtracted < 33:
- newVal = subtracted + 94
- decrypted += chr(newVal)
- else:
- decrypted += chr(subtracted)
- print("Here is your decrypted message")
- print(decrypted)
- else:
- print("Invalid Key.")
- else:
- print("Invalid Input, try again!")
- while invalid_input:
- menu()
Advertisement
Add Comment
Please, Sign In to add comment