-1

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?

Here is the syntax error I'm getting:

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

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

Here is the full script I'm running:

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)

Any and all help is appreciated. Thanks!

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?

Community
  • 1
  • 1
  • Line 5 is an empty line. `defencodeRepeatingKeyXor` is missing. Also see [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – jww Dec 08 '19 at 09:20
  • Thanks for the link! Also, what do you mean by line 5 is an empty line? Because it's not from what I can tell; line 5 is where the def function is? – pythonhelp99 Dec 08 '19 at 16:41
  • @jww see above edit please – pythonhelp99 Dec 08 '19 at 17:11
  • The error message and the code you show don't seem to quite match up. Based on the error message you're reporting you're missing a space between `def` and `encodeRepeatingKeyXor` (or possibly you've got a unicode character that kind of looks like a space but isn't). On that basis I'm voting to close as a typo – DavidW Dec 08 '19 at 18:38

1 Answers1

0

bytes() and b'...' doesn't work like you expect them to do. It's because they're immutable. If you refactor your code with bytearrays it works:

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)

Take a look at this question for further reading..

can
  • 444
  • 6
  • 14
  • I tried what you suggested and I'm still getting the same syntax error. – pythonhelp99 Dec 08 '19 at 16:38
  • I've run the script you gave and it gave a completely different error and i fixed that. Error and the script you gave doesn't match. Could you run the script in linux and copy-paste the error here? And how do you run the script? If it is from the command line try this command to see if you really use python 3.x or 2.x: python --version – can Dec 09 '19 at 06:27