encryption - How to open a key file for encrypting and decrypting in python -


so im trying write program encrypts word document , decrypts in another. im able program work if put key program want have read key key.txt. keep getting error (attributeerror: 'str' object has no attribute 'items') when put key in program. appreciated. thanks

this key file contains {'a':'6', 'a':'~', 'b':'66', 'b':';', 'c':'<', 'c':'@', 'd':'%$', 'd':'#', \ 'e':'5', 'e':'$', 'f':'3', 'f':'%', 'g':'71', 'g':'^', 'h':'72', 'h':'&', 'i':'4', 'i':'*', \ 'j':'74', 'j':'(', 'k':'75', 'k':')', 'l':'1', 'l':'_', 'm':'77', 'm':'`', 'n':'/:', \ 'n':'-', 'o':'79', 'o':'+', 'p':'2', 'p':'=', 'q':'99', 'q':'9', 'r':'82', 'r':'>', 's':'83', \ 's':'[','t':'', 't':']', 'u': ';', 'u':'{', 'v':'86', 'v':'}', 'w':'7', 'w':'/', \ 'x':'/+', 'x':'8', 'y':'%(', 'y':'0', 'z':'90', 'z':'$122'}

heres encryption

def main():  codes = open('key.txt', 'r') code = codes.read()  inputfile = open('text.txt', 'r') paragraph = inputfile.read() inputfile.close()  encrypt = open('encrypted_file.txt', 'w') ch in paragraph:     if ch in code:         encrypt.write(code[ch])     else:         encrypt.write(ch) encrypt.close()  main() 

heres decryption

def main(): codes = open('key.txt', 'r') code = codes.read()  inputfile = open('encrypted_file.txt', 'r') encrypt = inputfile.read() inputfile.close()  code_items = code.items()   ch in encrypt:     if not ch in code.values():         print(ch, end='')     else:         k,v in code_items:             if ch == v:                 print(k, end='') main() 

code = codes.read() 

at point code string, case when file read. python not automatically figure out convert to, since file can contain literally anything. convert dictionary:

from ast import literal_eval  code = literal_eval(codes.read()) 

Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -