Python Base64 解密错误:UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9c in position 0: invalid start byte
Python Base64 解密错误:UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9c in position 0: invalid start byte
在使用 Python 进行 Base64 解密时,可能会遇到 UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9c in position 0: invalid start byte 错误。这个错误通常是因为解密后的数据中包含了无法使用 UTF-8 编码解码的字节。
以下是一段示例代码,展示了如何解决该问题:
import base64
data = [0x7C, 0x7B, 0x77, 0x21, 0x99, 0x44, 0x73, 0x42, 0x79, 0x4C, 0x75, 0x42, 0xA0, 0x5C, 0x7F, 0x42, 0x93, 0x76, 0x22, 0x03, 0x79, 0x56, 0x91, 0x4C, 0x9F, 0x03, 0x9D, 0x49, 0x7C, 0x44, 0x87, 0x41]
flag = ""
for i in range(len(data)):
if (i%2 == 0)|(data[i] == 33):
data[i] = data[i] - 33
else:
data[i] = data[i] + 0x03
flag += chr(data[i]^0x35)
# #base64解密
flag = base64.b64decode(flag)
# 使用 'latin-1' 编码解码
flag = flag.decode('latin-1')
print(flag)
这段代码中,我们将 flag.decode('utf-8') 修改为 flag.decode('latin-1'),这样就可以成功解码并打印出 flag 了。
总结:
当遇到 UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9c in position 0: invalid start byte 错误时,可以尝试使用其他编码方式来解码,例如 'latin-1' 编码。
原文地址: https://www.cveoy.top/t/topic/o7eY 著作权归作者所有。请勿转载和采集!