Python Unicode解码器错误:“utf8”编解码器无法解码字节

2024-07-05 09:25:29 发布

您现在位置:Python中文网/ 问答频道 /正文

首先,我很抱歉,关于这个问题有这么多问题,我被这么多答案淹没了,但我仍然不明白,因为我的代码只在写入文件(?)

那么,为什么会这样:

# encrypt and write to file    
    data_to_encrypt = "mytext" 
    data = data_to_encrypt.encode('utf-8')
    ciphered_bytes = cipher_encrypt.encrypt(data)
    ciphered_data = ciphered_bytes
    print(ciphered_data)

    with open(fileConfigBIN, "wb") as binary_file:
        binary_file.write(ciphered_data)

# read the file and decrypt
    with open(fileConfigBIN, "rb") as binary_file:
        data1 = binary_file.read()
        print(data1)
        deciphered_bytes = cipher_decrypt.decrypt(data1)
        decrypted_data = deciphered_bytes.decode('utf-8')
        print(decrypted_data)

输出:

b'\xa0U\xee\xda\xa8R'

b'\xa0U\xee\xda\xa8R'

mytext

但只需对写入文件进行注释(该文件仍然包含相同的信息):

    #data_to_encrypt = "mytext" 
    #data = data_to_encrypt.encode('utf-8')
    #ciphered_bytes = cipher_encrypt.encrypt(data)
    #ciphered_data = ciphered_bytes
    #print(ciphered_data)

    #with open(fileConfigBIN, "wb") as binary_file:
    #    binary_file.write(ciphered_data)

    with open(fileConfigBIN, "rb") as binary_file:
        data1 = binary_file.read()
        print(data1)
        deciphered_bytes = cipher_decrypt.decrypt(data1)
        decrypted_data = deciphered_bytes.decode('utf-8')
        print(decrypted_data)

我明白了:

b'\xa0U\xee\xda\xa8R'

Exception in Tkinter callback Traceback (most recent call last): File "C:\Users...................\Python37-32\lib\tkinter__init__.py", line 1705, in call return self.func(*args) File "C:\Users\Fabio\source.............", line 141, in AbrirConfig decrypted_data = deciphered_bytes.decode('utf-8') UnicodeDecodeError: 'utf-8' codec can't decode byte 0xdd in position 1: invalid continuation byte

读取的代码使用与注释部分相同的文件保存它

我试着用“拉丁语-1”、“ISO-8859-1”和其他语言保存和阅读。。它不提供错误,只返回奇怪的字符

使用Python 3.7

谢谢,这是事先准备好的

编辑:

完整工作最小代码:

from Crypto.Cipher import AES

fileConfigBIN = "data.dat"

key = b'\x16\x18\xed\x1c^\xaaGN\rl\xc0]\xf0t=\xd0\xdc]t\xaf\xb2\x12,\xe6\xfc\xd6\x11-\x10\xb4\xb1\x0b'
cipher_encrypt = AES.new(key, AES.MODE_CFB)
iv = cipher_encrypt.iv
cipher_decrypt = AES.new(key, AES.MODE_CFB, iv=iv)

def Encrypt():
    data_to_encrypt = "MyTextToEncrypt" 
    data = data_to_encrypt.encode('utf-8')
    ciphered_bytes = cipher_encrypt.encrypt(data)
    ciphered_data = ciphered_bytes

    with open(fileConfigBIN, "wb") as binary_file:
        binary_file.write(ciphered_data)
        print("CRYPTED DATA SAVED TO FILE: ", str(ciphered_data))

def Decrypt():
    with open(fileConfigBIN, "rb") as binary_file:
        data1 = binary_file.read()
        print("DATA READ FROM FILE: ", data1)
        deciphered_bytes = cipher_decrypt.decrypt(data1)
        decrypted_data = deciphered_bytes.decode('utf-8')
        print("DECRYPTED DATA: ", decrypted_data)

Encrypt() #comment this function to show the problem
print('---------------------')
Decrypt()

I did it guys! Thanks to @Hoenie insight, for every encryption, I store the iv value and use it to decrypt when needed. If happens to need to encrypt again, it save another iv value and so on... Thanks thanks!


Tags: todatabytesutffileencryptcipherprint
1条回答
网友
1楼 · 发布于 2024-07-05 09:25:29

好的,看来这与编码/解码无关。 您需要创建一个新的密码对象,因为它只能使用一次

def Decrypt():
    cipher_decrypt = AES.new(key, AES.MODE_CFB, iv=iv)   # <  add this
    with open(fileConfigBIN, "rb") as binary_file:
        data1 = binary_file.read()
        print("DATA READ FROM FILE: ", data1)
        deciphered_bytes = cipher_decrypt.decrypt(data1)
        decrypted_data = deciphered_bytes.decode('utf-8')
        print("DECRYPTED DATA: ", decrypted_data)

在源代码中:GitHub

A cipher object is stateful: once you have decrypted a message you cannot decrypt (or encrypt) another message with the same object.

感谢:https://stackoverflow.com/a/54082879/7547749

相关问题 更多 >