使用密钥对消息进行异或操作:TypeError:’int’对象不可调用

huangapple go评论76阅读模式
英文:

XOR a message with a key: TypeError: 'int' object is not callable

问题

尝试逐字节使用给定的密钥 b"ICE" 对消息进行异或操作。为了实现这个目标,我必须枚举密钥索引和消息,同时确保使用密钥的模运算来循环遍历它。

但是,我遇到了 TypeError: 'int' object is not callable 的问题。

我不确定如何修复这个问题。非常感谢任何建议。以下是所有的代码。

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


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()

注意:我已经将代码中的错误进行了修复,将变量名 "bytes" 改为 "byte" 并将双引号替换为单引号。

英文:

Trying to XOR a message byte by byte with a given key b"ICE". To do this I have to enumerate through the key indices and message whilst making sure to use the modulo of the key to loop through it.

However, I'm getting the TypeError: 'int' object is not callable.

I'm not sure how to fix this. Any advice is much appreciated. All code is below.

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()

答案1

得分: 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
英文:

bytes is a built-in which you are overriding.

You probably meant:

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

答案2

得分: 0

如前一答案所述,您正在覆盖bytes并创建了一个同名变量。

我建议利用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.\'(/''
英文:

As said in the previous answer, you are overriding bytes creating a variable with the same name.

I would suggest to exploit python's elegant features and libraries, avoid indexing and optimise the code

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()

Which produces

b'\x0b67\'*+.cb,.ii*#i:*<c$ -b=c4<*&"c$\'\'e\'*(+/ C\ne.,e*1$3:e>+ \'c\x0ci+ (1e(c&0.\'(/'

huangapple
  • 本文由 发表于 2020年1月6日 02:29:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/59602966.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定