bitwise-xor
python 3中是否有一个可以字节的内置函数?例如,如果我有 2 个字节数组:
one = oE1ltQSsoEqRC4j1EMz1ORU1dyucIcI4WstKz-uhuKA=
two = Rffs1PW5zA1h5RFVh5MkLw5R7a2QVHY7cwnjuSPktwc=
one XOR two = 5bqJYfEVbEfw7pmgl1_RFhtkmoYMdbQDKcKpdshFD6c=
bitwise-xor
python 3中是否有一个可以字节的内置函数?例如,如果我有 2 个字节数组:
one = oE1ltQSsoEqRC4j1EMz1ORU1dyucIcI4WstKz-uhuKA=
two = Rffs1PW5zA1h5RFVh5MkLw5R7a2QVHY7cwnjuSPktwc=
one XOR two = 5bqJYfEVbEfw7pmgl1_RFhtkmoYMdbQDKcKpdshFD6c=
一般来说,如果你有两个bytes
es,你可以这样做
one_xor_two = bytes(a ^ b for (a, b) in zip(one, two))
对它们进行逐元素异或。
在您的情况下,您首先要对字符串进行 base64 解码,然后是 XOR,然后是 base64 编码......但是,由于 Python 不喜欢其中的错误填充,示例字符串将不起作用。
import base64
one = base64.b64decode('oE1ltQSsoEqRC4j1EMz1ORU1dyucIcI4WstKz-uhuKA=')
two = base64.b64decode('Rffs1PW5zA1h5RFVh5MkLw5R7a2QVHY7cwnjuSPktwc=')
one_xor_two = bytes(a ^ b for (a, b) in zip(one, two))
print(base64.b64encode(one_xor_two))