Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # File encryption
- # Use dictionary to assign codes to each letter of the alphabet
- def main():
- codes = {'A':'!', 'a':'0', 'B':'@', 'b':'9', 'C':'#',
- 'c':'8', 'D':'$', 'd':'7', 'E':'%', 'e':'6',
- 'F':'^', 'f':'5', 'G':'&', 'g':'4', 'H':'*',
- 'h':'3', 'I':';;', 'i':'2', 'J':'~', 'j':'1',
- 'K':'x', 'k':'?', 'L':'b', 'l':'Y', 'M':'e',
- 'm':'q', 'N':'a', 'n':'c', 'O':'f', 'o':'D',
- 'P':'j', 'p':'G', 'Q':'B', 'q':'J', 'R':'K',
- 'r':'E', 'S':'A', 's':'d', 'T':'I', 't':'L',
- 'U':'C', 'u':'>', 'V':'<', 'v':'/', 'W':'F',
- 'w':'k', 'X':'r', 'x':'R', 'Y':'t', 'y':'o',
- 'Z':'n', 'z':'s', ' ': ' '}
- # Create an empty string to stored the encryption.
- encrypted_words = ''
- # Open file for reading
- text_file = 'text.txt'
- with open(text_file) as file_object:
- encrypted = file_object.read()
- # Perform the encryption
- i = 0
- while encrypted !='' and i < len(encrypted):
- if encrypted[i] in codes:
- encrypted_words += codes[encrypted[i]]
- elif encrypted[i] == ' ':
- encrypted_words += ' '
- i += 1
- print(encrypted)
- print()
- print(encrypted_words)
- # Write encrypted_words to a text file.
- encrypted_file = 'encryption_words.txt'
- with open(encrypted_file, 'w') as encrypted_object:
- for i in range(len(encrypted_words)):
- encrypted_object.write('{:50}\n'.format(encrypted_words[i]))
- print()
- print('Encrypted words save to encryption_words.txt')
- # Read the encrypted text
- encrypted_text = 'encryption_words.txt'
- with open(encrypted_text) as text:
- print(text.read())
- # Call the main function
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement