带有解密的Python密码学错误

时间:2018-06-19 16:57:34

标签: python encryption cryptography

我正在处理一段代码,该代码接受输入,将其写入文本文件,然后读取文本文件,对其解密并打印输出。除了我的代码的解密部分,一切都正常。我上面有两条注释。他们还产生了错误,使我相信这是我的解密问题。

import cryptography
from cryptography.fernet import Fernet
import os
import os.path

doc = open("cryptest.txt", "a+")
query = input('Type the text to be encrypted:').encode('utf-8')
print(query + ' is the query'.encode('utf-8'))

key = Fernet.generate_key()
f = Fernet(key)
cipher_text = f.encrypt(query)
#plain_text = f.decrypt(query)
#plain_text = f.decrypt(cipher_text)


doc.write("\n" + str(cipher_text))
doc.close() #closes file
open1 = open("cryptest.txt", "r") #opens file to read it
#open2 = open1.encode('utf-8')
print(open1.read() + 'is the contents of the text document')
#print (f.decrypt(open2.read())) #prints whatever is in the text file 
plaint = open1.read().encode('utf-8')
print(plaint +'is the encoded contents of the file'.encode('utf-8'))
decrypto = f.decrypt(plaint)
print(str(decrypto) + 'is the decrypted information'.encode('utf-8'))

doc.close()

我得到的错误如下

Traceback (most recent call last):
File ".\CryptoTut.py", line 25, in <module>
decrypto = f.decrypt(plaint)
File "C:\Program Files (x86)\Python36-32\lib\site- 
packages\cryptography\fernet.py", line 74, in decrypt
timestamp, data = Fernet._get_unverified_token_data(token)
File "C:\Program Files (x86)\Python36-32\lib\site- 
packages\cryptography\fernet.py", line 88, in _get_unverified_token_data
raise InvalidToken
cryptography.fernet.InvalidToken

0 个答案:

没有答案