Advertisement
functionython

file encryption

Feb 16th, 2017
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.83 KB | None | 0 0
  1. # File encryption
  2. # Use dictionary to assign codes to each letter of the alphabet
  3.  
  4. def main():
  5.     codes = {'A':'!', 'a':'0', 'B':'@', 'b':'9', 'C':'#',
  6.              'c':'8', 'D':'$', 'd':'7', 'E':'%', 'e':'6',
  7.              'F':'^', 'f':'5', 'G':'&', 'g':'4', 'H':'*',
  8.              'h':'3', 'I':';;', 'i':'2', 'J':'~', 'j':'1',
  9.              'K':'x', 'k':'?', 'L':'b', 'l':'Y', 'M':'e',
  10.              'm':'q', 'N':'a', 'n':'c', 'O':'f', 'o':'D',
  11.              'P':'j', 'p':'G', 'Q':'B', 'q':'J', 'R':'K',
  12.              'r':'E', 'S':'A', 's':'d', 'T':'I', 't':'L',
  13.              'U':'C', 'u':'>', 'V':'<', 'v':'/', 'W':'F',
  14.              'w':'k', 'X':'r', 'x':'R', 'Y':'t', 'y':'o',
  15.              'Z':'n', 'z':'s', ' ': ' '}
  16.  
  17.     # Create an empty string to stored the encryption.
  18.     encrypted_words = ''
  19.  
  20.     # Open file for reading
  21.     text_file = 'text.txt'
  22.     with open(text_file) as file_object:
  23.         encrypted = file_object.read()
  24.        
  25.         # Perform the encryption
  26.         i = 0
  27.         while encrypted !='' and i < len(encrypted):
  28.             if encrypted[i] in codes:
  29.                 encrypted_words += codes[encrypted[i]]
  30.             elif encrypted[i] == ' ':
  31.                 encrypted_words += ' '
  32.             i += 1
  33.         print(encrypted)
  34.         print()
  35.         print(encrypted_words)
  36.  
  37.     # Write encrypted_words to a text file.
  38.     encrypted_file = 'encryption_words.txt'
  39.     with open(encrypted_file, 'w') as encrypted_object:
  40.         for i in range(len(encrypted_words)):
  41.             encrypted_object.write('{:50}\n'.format(encrypted_words[i]))
  42.  
  43.     print()
  44.     print('Encrypted words save to encryption_words.txt')
  45.  
  46.     # Read the encrypted text
  47.     encrypted_text = 'encryption_words.txt'
  48.     with open(encrypted_text) as text:
  49.         print(text.read())
  50.        
  51.    
  52. # Call the main function
  53. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement