gpt4 book ai didi

python-3.x - 我正在进行 cryptopals 挑战 5。尝试在 kali linux 中运行此脚本时,我在第 5 行遇到语法错误

转载 作者:太空宇宙 更新时间:2023-11-04 05:07:38 25 4
gpt4 key购买 nike

EDIT: I ran the script in IDLE on OSX and it ran without a hitch but when I try to run it in gedit on kali linux I still get the syntax errors. Any guesses?

这是我收到的语法错误:

./challenge5.py: line5: syntax error near unexpected token (

./challenge5.py: defencodeRepeatingKeyXor(s, key):

这是我正在运行的完整脚本:

import binascii

def encodeRepeatingKeyXor(s, key):
return bytes([s[i] ^ key[i % len(key)] for i in range(len(s))])

x = b'''Burning 'em, if you ain't quick and nimble
I go crazy when I hear a cymbal'''
key = b'ICE'
encodedExpectedY = '0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f'
expectedY = binascii.unhexlify(encodedExpectedY)

if __name__ == '__main__':
y = encodeRepeatingKeyXor(x, key)
encodedY = binascii.hexlify(y).decode('ascii')
print(encodedY)
print(encodedExpectedY)
if y != expectedY:
raise Exception(encodedY + ' != ' + encodedExpectedY)

感谢任何和所有帮助。谢谢!

EDIT: I ran the script in IDLE on OSX and it ran without a hitch but when I try to run it in gedit on kali linux I still get the syntax errors. Any guesses?

最佳答案

bytes() 和 b'...' 并不像您期望的那样工作。这是因为它们是不可变的。如果您使用字节数组重构代码,它会起作用:

import binascii


def encodeRepeatingKeyXor(s, key):
return bytearray([s[i] ^ key[i % len(key)] for i in range(len(s))])



x = b'''Burning 'em, if you ain't quick and nimble
I go crazy when I hear a cymbal'''
key = b'ICE'
encodedExpectedY = '0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f'
expectedY = binascii.unhexlify(encodedExpectedY)

if __name__ == '__main__':
y = encodeRepeatingKeyXor(bytearray(x), bytearray(key))
encodedY = binascii.hexlify(y).decode('ascii')
print(encodedY)
print(encodedExpectedY)
if y != expectedY:
raise Exception(encodedY + ' != ' + encodedExpectedY)

看看this进一步阅读的问题..

关于python-3.x - 我正在进行 cryptopals 挑战 5。尝试在 kali linux 中运行此脚本时,我在第 5 行遇到语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59233267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com