Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. import string
  2. import re
  3.  
  4. letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  5. message = "Its not a story the Jedi would tell"
  6. key = 'LFWOAYUISVKMNXPBDCRJTQEGHZ'
  7.  
  8. def encrypt(key, message):
  9.     # this function removes the whitespaces from the message
  10.     def remove(string):
  11.         pattern = re.compile(r'\s+')
  12.         return re.sub(pattern, '', string)
  13.  
  14.     # it is importany to remember the string operations are case sensitive
  15.     # hence the lower function
  16.  
  17.     keyList = list(key)
  18.     messageLength = len(remove(message.lower()))
  19.     messageList = list(remove(message.lower()))
  20.     messageArray = []
  21.     messageNum = []
  22.     encrypted_mess = " "
  23.     d = 0
  24.     x = 0
  25.  
  26.     for d in range(messageLength):
  27.         # push the letters into an array
  28.         messageArray.append(messageList[d])
  29.         # push their numerical value  into another array
  30.         messageNum.append(string.ascii_lowercase.index(messageArray[d]))
  31.    
  32.     for x in range(messageLength):
  33.         #with each iteration in the for loop
  34.         # the corresponding letter to the number
  35.         # is added to the encrypted message
  36.         encrypted_mess += keyList[messageNum[x]]
  37.    
  38.     # print(encrypted_mess)
  39.     return encrypted_mess
  40.  
  41. print("The message: ", message)
  42. print("The encrypted cipher", encrypt(key, message))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement