【问题标题】:I'm doing the cryptopals challenge 5. I'm getting a syntax error in line 5 when trying to run this script in kali linux我正在做密码挑战 5。尝试在 kali linux 中运行此脚本时,我在第 5 行遇到语法错误
【发布时间】:2020-04-01 15:14:30
【问题描述】:

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

这是我得到的语法错误:

./challenge5.py: line5: 意外标记附近的语法错误(

./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 中运行它时,我仍然遇到语法错误。有什么猜测吗?

【问题讨论】:

  • 第 5 行是空行。 defencodeRepeatingKeyXor 不见了。另请参阅How to create a Minimal, Complete, and Verifiable example
  • 感谢您的链接!另外,第5行是空行是什么意思?因为这不是我能说的;第 5 行是 def 函数在哪里?
  • @jww 请参阅上面的编辑
  • 错误消息和您显示的代码似乎不太匹配。根据您报告的错误消息,您在 defencodeRepeatingKeyXor 之间缺少一个空格(或者您可能有一个看起来像空格但不是的 unicode 字符)。在此基础上,我投票关闭作为一个错字

标签: python-3.x linux syntax cryptography


【解决方案1】:

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

【讨论】:

  • 我尝试了您的建议,但仍然遇到相同的语法错误。
  • 我已经运行了你给的脚本,它给出了一个完全不同的错误,我修复了它。错误与您提供的脚本不匹配。您可以在 linux 中运行脚本并在此处复制粘贴错误吗?以及如何运行脚本?如果是从命令行尝试这个命令,看看你是否真的使用 python 3.x 或 2.x:python --version
猜你喜欢
  • 2022-11-11
  • 2020-07-06
  • 2022-06-30
  • 2023-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多