在 Python 中搜索文本的 base64 解码二进制数据

searching base64 decoded binary data for text in Python

我在数据库中有一些数据是 base64 编码的

我用

很容易解码它
b64 = base64.b64decode(row[2])

但是我现在需要搜索各种文本字符串的解码数据并获取所述字符串的偏移量。解码后的数据是二进制的,即它包含很多不可打印的、NULL 等

我试过了

ofs = b64.find('content')

但是报错TypeError: Type str doesn't support the buffer API

这表明我显然找错了树

我是 Python 的新手,但出于各种原因想尝试使用它来获得解决方案 - 请问有什么想法吗?

搜索 bytes object instead of a str

ofs = b64.find(b'content')

或少一些硬编码方式

search = 'content'
ofs = b64.find(bytes(search))

由于ofs被编码为bytes,你应该在里面寻找对应的对象;)。



来自文档,bytes 当用作函数时 returns 一个新的 bytes 对象:

bytes([source[, encoding[, errors]]])

Return a new “bytes” object, which is an immutable sequence of integers in the range 0 <= x < 256. bytes is an immutable version of bytearray – it has the same non-mutating methods and the same indexing and slicing behavior.