XOR带有密钥的消息:TypeError:'int'对象不可调用

问题描述 投票:0回答:2

尝试用给定的密钥b“ ICE”逐字节对消息进行XOR。为此,我必须枚举键索引和消息,同时确保使用键的模来遍历它。

但是,我得到了TypeError:'int'对象不可调用。

我不确定如何解决此问题。任何建议深表感谢。所有代码都在下面。

def repeating_key_xor(message_bytes, key):
    output_bytes = b''
    for i, bytes in enumerate(message_bytes):
        output_bytes += bytes([key[i % len(key)] ^ bytes])
    return output_bytes


def main():
    key = b"ICE"
    message = b"Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal"
    ciphertext = repeating_key_xor(message, key)
    print(ciphertext)


if __name__ == '__main__':
    main()

python encryption xor
2个回答
1
投票

bytes是您要覆盖的内置对象。

您可能是说:

def repeating_key_xor(message_bytes, key):
    output_bytes = b''
    for i, byte in enumerate(message_bytes):
        output_bytes += bytes([key[i % len(key)] ^ byte])
    return output_bytes

0
投票

我建议利用python的优雅功能和库,避免建立索引并优化代码

import itertools as it

def repeating_key_xor(message_bytes, key):
    for byte, k in zip(message_bytes, it.cycle(key)):
        yield k ^ byte

def main():
    key = b"ICE"
    message = b"Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal"
    ciphertext = bytes(repeating_key_xor(message, key))
    print(ciphertext)

if __name__ == '__main__':
    main()

哪个生产

b'\x0b67\'*+.cb,.ii*#i:*<c$ -b=c4<*&"c$\'\'e\'*(+/ C\ne.,e*1$3:e>+ \'c\x0ci+ (1e(c&0.\'(/'
© www.soinside.com 2019 - 2024. All rights reserved.