簡體   English   中英

Python UnicodeDecodeError: 'utf-8' 編解碼器無法解碼字節

[英]Python UnicodeDecodeError: 'utf-8' codec can't decode byte

首先,很抱歉,關於這個問題有很多問題,我淹沒在這么多答案中,但我還是不明白,因為我的代碼只要寫入文件(?)

那么,為什么這有效:

# 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)

Output:

b'\xa0U\xee\xda\xa8R'

b'\xa0U\xee\xda\xa8R'

我的文本

但只是通過評論寫入文件(文件仍然存在並具有相同的信息):

    #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'

Tkinter 回調 Traceback 中的異常(最近調用最后一次):文件“C:\Users......\Python37-32\lib\tkinter__init__.py”,行1705,在調用中返回 self.func(*args) 文件“C:\Users\Fabio\source................”,第 141 行,在 AbrirConfig 中 decrypted_data = deciphered_bytes.decode('utf -8') UnicodeDecodeError: 'utf-8' codec can't decode byte 0xdd in position 1: invalid continuation byte

讀取的代碼使用與注釋部分相同的文件保存它!

我嘗試保存並讀取“latin-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()

伙計們,我做到了,感謝@Hoenie insight,每次加密。 我存儲 iv 值並在需要時使用它來解密,如果碰巧需要再次加密。 它保存另一個 iv 值等等......謝謝謝謝!

好的,這似乎與編碼/解碼無關。 您需要創建一個新的密碼 object,因為它只能使用一次。

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

密碼 object 是有狀態的:一旦你解密了一條消息,你就不能解密(或加密)另一條具有相同 object 的消息。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM