python xor 只呈现零?

python xor renders only zero?

我正在尝试实现重复键 xor encryption scheme in python

我有这个实现:

from binascii import hexlify


def repeat_to_length(string_to_expand, length):
    return (string_to_expand * (int(length/len(string_to_expand))+1))[:length]

def Enc(plaintext,k):
    ciphertext = b''
    s1 = str.encode(plaintext)
    key =  str.encode(repeat_to_length(k, len(s1)))

    i = 0

    for s11,k1 in zip(s1,key):
        tmp =  bytes(s11^k1)
        ciphertext += tmp 

    return ciphertext

key = "ICE"

m1 = "Burning 'em, if you ain't quick and nimble"
m2 = "I go crazy when I hear a cymbal"

c = Enc(m1,key)

print(str(hexlify(c), "utf-8"))

我使用函数repeat_to_length将密钥长度扩展到与明文相同的长度,只是为了方便使用。然后我将这两个字符串转换为字节对象,这样我就可以对它们进行异或运算。

奇怪的是,当我在这里 运行 我的代码时,这是我得到的输出:

b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

这是怎么回事?为什么 xor 只渲染为零?

正如我在评论中提到的,bytes(n) 不会将 n 转换为字节,它会创建一个包含 n 个零的字节对象。我确定 xor 工作正常。而不是:

tmp =  bytes(s11^k1)

使用:

tmp = (s11^k1).to_bytes(1, 'big')

如评论中所述,bytes(int) 创建一个零填充字节串 int 元素长。 尝试更改此行

tmp = bytes(s11^k1)

对此:

tmp = bytes([s11^k1])

bytes 的文档描述了它根据传递的参数的不同工作方式:

>>> help(bytes)
Help on class bytes in module builtins:

class bytes(object)
 |  bytes(iterable_of_ints) -> bytes
 |  bytes(string, encoding[, errors]) -> bytes
 |  bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer
 |  bytes(int) -> bytes object of size given by the parameter initialized with null bytes
 |  bytes() -> empty bytes object
 |  
 |  Construct an immutable array of bytes from:
 |    - an iterable yielding integers in range(256)
 |    - a text string encoded using the specified encoding
 |    - any object implementing the buffer API.
 |    - an integer
...