Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.61 KB | None | 0 0
  1. def main():
  2.     # Create the dictionary for the encryption codes.
  3.     codes = {'A':'!', 'a':'0', 'B':'@', 'b':'9', 'C':'#',
  4.              'c':'8', 'D':'$', 'd':'7', 'E':'%', 'e':'6',
  5.              'F':'^', 'f':'5', 'G':'&', 'g':'4', 'H':'*',
  6.              'h':'3', 'I':';', 'i':'2', 'J':'~', 'j':'1',
  7.              'K':'x', 'k':'?', 'L':'b', 'l':'Y', 'M':'e',
  8.              'm':'q', 'N':'a', 'n':'c', 'O':'f', 'o':'D',
  9.              'P':'j', 'p':'G', 'Q':'B', 'q':'J', 'R':'K',
  10.              'r':'E', 'S':'A', 's':'d', 'T':'I', 't':'L',
  11.              'U':'C', 'u':'>', 'V':'<', 'v':'/', 'W':'F',
  12.              'w':'k', 'X':'r', 'x':'R', 'Y':'t', 'y':'o',
  13.              'Z':'n', 'z':'s', ' ': ' '}
  14.  
  15.     # Get the encrypted word file.
  16.     encrypted_file = 'encrypted_words.txt'
  17.  
  18.     # Decrypt the file and print it out.
  19.     print('Below are the decrypted words:')
  20.     print()
  21.     print(decryption(codes, encrypted_file)
  22.  
  23.  
  24. # the decryption function accepts the codes and the encrypted
  25. # words, inverts the key-value pair in the codes and decrypts
  26. # the encrypted words.
  27. def decryption(codes, encrypted_file):
  28.     # Create an empty string to stored the decryption.
  29.     decrypted_words = ''
  30.    
  31.     # Create another dictionary with the key-value pair
  32.     # inverted.
  33.     inverted_codes = {val: key for key, val in codes.items()}
  34.  
  35.     # Open the encrypted file for reading.
  36.     with open(encrypted_file) as file_object:
  37.         decrypted = file_object.read()
  38.  
  39.         # Perform the decryption.
  40.         for char in decrypted:
  41.             if char in inverted_codes:
  42.                 decrypted_words += inverted_codes[char]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement