Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #=====================================#
- #==============MODULES================#
- #=====================================#
- #=====================================#
- #==============VARIABLES==============#
- #=====================================#
- alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- alpha_size = len(alpha)
- menus = ["(1) Encrypt", "(2) Decrypt", "(3) Exit "]
- max_set = ord("Z")
- min_set = ord("A")
- #=====================================#
- #==============FUNCTIONS==============#
- #=====================================#
- def menu(parameter):
- print " +===========+"
- print " | MENU |"
- print " |-----------|"
- for word in parameter:
- print " |%s|" % word
- print " |-----------|"
- #define encryption/decryption algorithm
- def algo(data, key):
- output = ""
- for char in data: #iterates through each character in string
- if char.isalpha(): #if character is True....
- nums = ord(char) #convert character to ascii number
- nums = (nums + key) #shift ascii number with key
- if nums > max_set: #if new ascii character higher than ascii "Z"
- nums = (nums - alpha_size) #subtract 26 to bring character in Modulo 26
- elif nums < min_set: #if new ascii character lower than ascii "A"
- nums = (nums + alpha_size) #add 26 to bring the character in Modulo 26
- output = (output + chr(nums)) #append blank string with converted ascii to new character
- else:
- output = (output + char) #append string with special characters and numbers b/c of established modulo
- print output
- #=====================================#
- #===========MAIN PROGRAM==============#
- #=====================================#
- def main():
- menu(menus)
- choice = input("Please make a selection(1-3): ")
- #Encryption
- if choice == 1:
- key = input("Please enter a key(0-25): ")
- data = raw_input("Please enter a message to encrypt: ").upper()
- print "The encrypted message is: "
- algo(data, key)
- #Decryption
- elif choice == 2:
- key = input("Please enter a key(0-25): ")
- key = (key * -1)
- data = raw_input("Please enter a message to decrypt: ").upper()
- print "The decrypted message is: ")
- algo(data, key)
- #Exit
- elif choice == 3:
- print "Exiting Program. Goodbye"
- return
- #Fault
- else:
- print "Invalid Selection"
- return main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement