You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python base64混淆报错:expected str instance, bytes found

Base64混淆代码报错排查与修复

问题描述

我在个人项目中尝试对Base64字符串做混淆处理:每间隔指定长度的字符插入多个@符号,再通过Base64 UTF-8编码,重复该流程多次。作为Python新手,参考他人代码修改后适配需求,但不确定语法正确性。当前使用Python 3.10.5 64位版本,在VSCode运行代码时出现TypeError: sequence item 0: expected str instance, bytes found错误,相关代码及完整报错信息如下:

import base64
# Define 'encrypt' function to append @ signs. This could ostensibly be changed to any character that is not part of the base64 character set.
def encrypt(string, length):
    return '@@@@'.join(string[i:i+length] for i in range(0,len(string),length))

# Get user input
encString = input("Enter string to obfuscate:")
print("Enter Integer:")
loops = input()

# Set loops as an int as I was having problems with it being defined as a string, attempted to do the same for encString
x = int(loops)
encString = str(encString)

# Attempt to loop this for the specified number of times
while x != 0:
    x = x - 1
    encString = encrypt(encString,4)
    encString = base64.b64encode (bytes(encString, "utf-8"))

print(encString)

报错信息:

PS C:\Users\esmith> & C:/Users/esmith/AppData/Local/Programs/Python/Python310/python.exe "c:/Users/esmith/PATH/Documents/Learning Python/test.py"
Enter string to obfuscate:bnVtYmVyID0gMSArIDIgKiAzIC8gNApwcmludChudW1iZXIpCgpyZW1haW5kZXIgPSAxMSAlIDMKcHJpbnQocmVtYWluZGVyKQoKIyBUd28gKiogMgpjdWJlZCA9IDIgKiogMwpwcmludChzcXVhcmVkKQpwcmludChjdWJlZCkKCmxvdHNvZmhlbGxvcyA9ICJoZWxsbyAiICogMTAKcHJpbnQobG90c29maGVsbG9zKQoKCmV2ZW5fbnVtYmVycyA9IFsyLDQsNiw4XQpvZGRfbnVtYmVycyA9IFsxLDMsNSw3XQphbGxfbnVtYmVycyA9IG9kZF9udW1iZXJzICsgZXZlbl9udW1iZXJzCnByaW50KGFsbF9udW1iZXJzKQoKCnByaW50KFsxLDIsM10gKiAzKQ==
Enter Integer:
5
Traceback (most recent call last):
  File "c:\Users\esmith\PATH\Documents\Learning Python\test.py", line 15, in <module>
    encString = encrypt(encString,4)
  File "c:\Users\esmith\PATH\Documents\Learning Python\test.py", line 4, in encrypt
    return '@@@@'.join(string[i:i+length] for i in range(0,len(string),length))
TypeError: sequence item 0: expected str instance, bytes found

错误原因

核心问题是数据类型不匹配:

  • base64.b64encode()函数返回的是bytes类型对象,而非字符串
  • 第一次循环时,encString是字符串,encrypt()可以正常处理;但经过base64.b64encode()处理后,encString变成了bytes对象
  • 第二次循环调用encrypt()时,string[i:i+length]取到的是bytes片段,而'@@@@'.join()要求所有元素必须是字符串类型,因此触发类型错误

解决方法

每次调用base64.b64encode()后,必须将返回的bytes对象解码为UTF-8字符串,确保后续循环中encString始终保持字符串类型,避免类型冲突。

修正后的代码

import base64

def encrypt(string, length):
    return '@@@@'.join(string[i:i+length] for i in range(0, len(string), length))

encString = input("Enter string to obfuscate:")
print("Enter Integer:")
loops = input()

x = int(loops)
encString = str(encString)

while x != 0:
    x = x - 1
    encString = encrypt(encString, 4)
    # 关键修改:对b64encode的返回值进行utf-8解码,转为字符串
    encString = base64.b64encode(bytes(encString, "utf-8")).decode("utf-8")

print(encString)

验证说明

修正后,每次循环结束后encString都会保持字符串类型,encrypt()函数可正常处理,不会再触发类型错误,可输入测试值验证混淆流程是否符合预期。

内容的提问来源于stack exchange,提问作者Eben Smith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 11:54:14