我正在执行cryptopals挑战5。尝试在kali linux中运行此脚本时,第5行出现语法错误

时间:2019-12-08 07:15:43

标签: python-3.x linux syntax cryptography

  

编辑:我在OSX上的IDLE中运行了脚本,并且运行顺利,但是当我尝试在kali linux上的gedit中运行它时,仍然出现语法错误。有任何猜测吗?

这是我遇到的语法错误:

  

./ challenge5.py:第5行:语法错误,出现在意外令牌附近(

     

./ 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)

任何帮助都将受到赞赏。谢谢!

  

编辑:我在OSX上的IDLE中运行了脚本,并且运行顺利,但是当我尝试在kali linux上的gedit中运行它时,仍然出现语法错误。有任何猜测吗?

1 个答案:

答案 0 :(得分:0)

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问题以供进一步阅读。