【问题标题】:Python UnicodeDecodeError: 'utf-8' codec can't decode bytePython UnicodeDecodeError:“utf-8”编解码器无法解码字节
【发布时间】:2020-02-14 01:01:50
【问题描述】:

首先,对不起,关于这个问题有很多问题,我淹没在很多答案中,但我仍然不明白,因为我的代码只要我写入文件就可以工作(?)

那么,为什么会这样:

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

我的文本

但只是通过注释写入文件(文件仍然存在相同的信息):

    #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 回调异常 回溯(最近一次通话最后): 调用中的文件“C:\Users.......\Python37-32\lib\tkinter__init__.py”,第 1705 行 返回 self.func(*args) AbrirConfig 中的文件“C:\Users\Fabio\source......”,第 141 行 decrypted_data = deciphered_bytes.decode('utf-8') UnicodeDecodeError:“utf-8”编解码器无法解码位置 1 的字节 0xdd:无效的继续字节

读取的代码使用的是和注释部分相同的文件保存!

我尝试保存并读取 '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 洞察力,对于每次加密,我都会存储 iv 值并在需要时使用它来解密。如果碰巧需要再次加密,它会保存另一个iv值等等......谢谢谢谢!

【问题讨论】:

  • 只有您知道cipher_decrypt 和cipher_encrypt 是什么,以及它们是如何初始化的。如果您没有显示完整的工作示例,那么我们将无法运行它并找出问题所在。我只能说解密失败并返回垃圾数据。
  • 我们需要一个minimal reproducible example。
  • 你试过编码cp1252吗?
  • cp1252 当我解密时返回像.. †“m]Û&{r¿ÉÞY%Œè 。我在这里错过了什么?

标签: python encryption utf-8 binaryfiles


【解决方案1】:

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

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

密码对象是有状态的:一旦你解密了一条消息 你不能用相同的方式解密(或加密)另一条消息 对象。

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

【讨论】:

  • 这有点工作!我将加密中生成的第一个 iv 值放在“iv=”中,现在我可以解密了。但是,我可以再次加密......谢谢老兄!这有帮助!
猜你喜欢
  • 2017-09-27
  • 2014-08-29
  • 2018-05-05
  • 1970-01-01
  • 2018-04-22
  • 1970-01-01
  • 2020-07-17
  • 2019-02-24
  • 2021-12-06
相关资源
最近更新 更多