我正在尝试读取一个字符串加密它保存到一个文本文件然后读取文本文件读取加密的字符串在 python 中解密
I am trying to read a string encrypt it save it to a text file then read the text file read the encrypted string decrypt it in python
我正在尝试读取一个字符串加密它保存到一个文本文件然后读取文本文件读取加密的字符串在 python 中解密它,我正在使用加密库,我认为错误是因为python 列表在开头和结尾添加 ["string"],我也尝试将读取列表转换为二进制,但它把它当作 b["b'key'"],任何想法
def write_key(): #creating a key
key = Fernet.generate_key()
with open("key.key", "wb") as key_file:
key_file.write(key)
def load_key(): #reading the generated key
return open("key.key", "rb").read()
data = input()
data = bytes(data,'utf-8') #you cannot encrypt str do converting it to bytes
write_key() #creating the key
key = load_key() #loading the key
f = Fernet(key)
encrypted = f.encrypt(data) #encrypting the data
print(encrypted)
file = open("encrypted.txt", "w") #writing the encrypted the data
file.write("%s\n" %(encrypted))
file.close()
with open("encrypted.txt", "r") as f: #reading the encrypted the data
rdata = [line.rstrip('\n') for line in f] #removing \n as it is added while saving the txt file
print(rdata)
key = load_key()
f = Fernet(key)
decrypted_encrypted = f.decrypt(rdata)
print(decrypted_encrypted)
我得到的输出:
Enter the secret message: YOUR MESSAGE
Printing the encrypted message after encryption b'gAAAAABfWdNG64dvT-tpQA1EA-zYC8lsC4hL9EoZ0e008BMIWikfafT_FOmLyjWJh2dinGG8oi6VI16XCpwB1H4AZE2sk-ZgJQ=='
Printing the encrypted message after reading it from the txt file ["b'gAAAAABfWdNG64dvT-tpQA1EA-zYC8lsC4hL9EoZ0e008BMIWikfafT_FOmLyjWJh2dinGG8oi6VI16XCpwB1H4AZE2sk-ZgJQ=='"]
Traceback (most recent call last):
File "C:\Users\Documents\Projects\temp.py", line 31, in <module>
decrypted_encrypted = f.decrypt(rdata)
File "C:\Users\anaconda3\lib\site-packages\cryptography\fernet.py", line 74, in decrypt
timestamp, data = Fernet._get_unverified_token_data(token)
File "C:\Users\anaconda3\lib\site-packages\cryptography\fernet.py", line 85, in _get_unverified_token_data
utils._check_bytes("token", token)
File "C:\Users\anaconda3\lib\site-packages\cryptography\utils.py", line 31, in _check_bytes
raise TypeError("{} must be bytes".format(name))
TypeError: token must be bytes
传递给 decrypt
的数据应该是字节而不是字符串。因此这个问题。
从 encrypted.txt
读取和写入时使用如下相应的二进制模式。
def write_key(): #creating a key
key = Fernet.generate_key()
with open("key.key", "wb") as key_file:
key_file.write(key)
def load_key(): #reading the generated key
return open("key.key", "rb").read()
data = input()
data = bytes(data,'utf-8') #you cannot encrypt str do converting it to bytes
write_key() #creating the key
key = load_key() #loading the key
f = Fernet(key)
encrypted = f.encrypt(data) #encrypting the data
file = open("encrypted.txt", "wb") #writing the encrypted the data
file.write(encrypted)
file.close()
with open("encrypted.txt", "rb") as f: #reading the encrypted the data
rdata =f.read()
key = load_key()
f = Fernet(key)
decrypted_encrypted = f.decrypt(rdata)
print(decrypted_encrypted)
我正在尝试读取一个字符串加密它保存到一个文本文件然后读取文本文件读取加密的字符串在 python 中解密它,我正在使用加密库,我认为错误是因为python 列表在开头和结尾添加 ["string"],我也尝试将读取列表转换为二进制,但它把它当作 b["b'key'"],任何想法
def write_key(): #creating a key
key = Fernet.generate_key()
with open("key.key", "wb") as key_file:
key_file.write(key)
def load_key(): #reading the generated key
return open("key.key", "rb").read()
data = input()
data = bytes(data,'utf-8') #you cannot encrypt str do converting it to bytes
write_key() #creating the key
key = load_key() #loading the key
f = Fernet(key)
encrypted = f.encrypt(data) #encrypting the data
print(encrypted)
file = open("encrypted.txt", "w") #writing the encrypted the data
file.write("%s\n" %(encrypted))
file.close()
with open("encrypted.txt", "r") as f: #reading the encrypted the data
rdata = [line.rstrip('\n') for line in f] #removing \n as it is added while saving the txt file
print(rdata)
key = load_key()
f = Fernet(key)
decrypted_encrypted = f.decrypt(rdata)
print(decrypted_encrypted)
我得到的输出:
Enter the secret message: YOUR MESSAGE
Printing the encrypted message after encryption b'gAAAAABfWdNG64dvT-tpQA1EA-zYC8lsC4hL9EoZ0e008BMIWikfafT_FOmLyjWJh2dinGG8oi6VI16XCpwB1H4AZE2sk-ZgJQ=='
Printing the encrypted message after reading it from the txt file ["b'gAAAAABfWdNG64dvT-tpQA1EA-zYC8lsC4hL9EoZ0e008BMIWikfafT_FOmLyjWJh2dinGG8oi6VI16XCpwB1H4AZE2sk-ZgJQ=='"]
Traceback (most recent call last):
File "C:\Users\Documents\Projects\temp.py", line 31, in <module>
decrypted_encrypted = f.decrypt(rdata)
File "C:\Users\anaconda3\lib\site-packages\cryptography\fernet.py", line 74, in decrypt
timestamp, data = Fernet._get_unverified_token_data(token)
File "C:\Users\anaconda3\lib\site-packages\cryptography\fernet.py", line 85, in _get_unverified_token_data
utils._check_bytes("token", token)
File "C:\Users\anaconda3\lib\site-packages\cryptography\utils.py", line 31, in _check_bytes
raise TypeError("{} must be bytes".format(name))
TypeError: token must be bytes
传递给 decrypt
的数据应该是字节而不是字符串。因此这个问题。
从 encrypted.txt
读取和写入时使用如下相应的二进制模式。
def write_key(): #creating a key
key = Fernet.generate_key()
with open("key.key", "wb") as key_file:
key_file.write(key)
def load_key(): #reading the generated key
return open("key.key", "rb").read()
data = input()
data = bytes(data,'utf-8') #you cannot encrypt str do converting it to bytes
write_key() #creating the key
key = load_key() #loading the key
f = Fernet(key)
encrypted = f.encrypt(data) #encrypting the data
file = open("encrypted.txt", "wb") #writing the encrypted the data
file.write(encrypted)
file.close()
with open("encrypted.txt", "rb") as f: #reading the encrypted the data
rdata =f.read()
key = load_key()
f = Fernet(key)
decrypted_encrypted = f.decrypt(rdata)
print(decrypted_encrypted)