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

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

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

问题

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

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

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

  1. def repeating_key_xor(message_bytes, key):
  2. output_bytes = b''
  3. for i, byte in enumerate(message_bytes):
  4. output_bytes += bytes([key[i % len(key)] ^ byte])
  5. return output_bytes
  6. def main():
  7. key = b"ICE"
  8. message = b"Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal"
  9. ciphertext = repeating_key_xor(message, key)
  10. print(ciphertext)
  11. if __name__ == '__main__':
  12. 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.

  1. def repeating_key_xor(message_bytes, key):
  2. output_bytes = b''
  3. for i, bytes in enumerate(message_bytes):
  4. output_bytes += bytes([key[i % len(key)] ^ bytes])
  5. return output_bytes
  6. def main():
  7. key = b"ICE"
  8. message = b"Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal"
  9. ciphertext = repeating_key_xor(message, key)
  10. print(ciphertext)
  11. if __name__ == '__main__':
  12. main()

答案1

得分: 1

bytes 是一个内置类型,你正在进行覆盖操作。

你可能想要的是:

  1. def repeating_key_xor(message_bytes, key):
  2. output_bytes = b''
  3. for i, byte in enumerate(message_bytes):
  4. output_bytes += bytes([key[i % len(key)] ^ byte])
  5. return output_bytes
英文:

bytes is a built-in which you are overriding.

You probably meant:

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

答案2

得分: 0

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

我建议利用Python的优雅特性和库,避免索引并优化代码。

  1. import itertools as it
  2. def repeating_key_xor(message_bytes, key):
  3. for byte, k in zip(message_bytes, it.cycle(key)):
  4. yield k ^ byte
  5. def main():
  6. key = b"ICE"
  7. message = b"Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal"
  8. ciphertext = bytes(repeating_key_xor(message, key))
  9. print(ciphertext)
  10. if __name__ == '__main__':
  11. main()

这会产生以下结果:

  1. 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

  1. import itertools as it
  2. def repeating_key_xor(message_bytes, key):
  3. for byte, k in zip(message_bytes, it.cycle(key)):
  4. yield k ^ byte
  5. def main():
  6. key = b"ICE"
  7. message = b"Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal"
  8. ciphertext = bytes(repeating_key_xor(message, key))
  9. print(ciphertext)
  10. if __name__ == '__main__':
  11. main()

Which produces

  1. 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:

确定