Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

I'm new to python3, I'm getting the following error when trying to print the first_block value below.

 File "cbcmodechal10.py", line 18, in main

    first_block = iv ^ plaintext[0]

TypeError: unsupported operand type(s) for ^: 'bytes' and 'bytes'

Can please someone also explain why this happens, although both the variables are in bytes, why this operation is not successful?

I know that there is some problem with utf-8 encoding but couldn't figure out what?

#!/bin/bash/python3

import urllib.request

import base64

from Crypto.Cipher import AES

def main():

    file = urllib.request.urlopen('https://cryptopals.com/static/challenge-data/10.txt')

    ciphertext = file.read().decode('utf-8')

    cipher = base64.b64decode(ciphertext)

    key = bytes('YELLOW SUBMARINE', 'utf-8')

    iv  = bytes('\x00' * 16, 'utf-8')

    blocksize = 16

    chunks = [cipher[i:i+blocksize] for i in range(0, len(cipher), blocksize)]

    #print(chunks[0])

    cipher1 = AES.new(key, AES.MODE_ECB)

    blocks = int(len(cipher) / blocksize)

    plaintext = [cipher1.decrypt(chunks[j]) for j in range(0, blocks)]

    first_block = iv ^ plaintext[0]

    print(first_block)

if __name__ == '__main__':

    main()

expected output: "I'm back and I'm"

1 Answer

0 votes
by (25.1k points)

On the last 5th line, instead of doing:

first_block = iv ^ plaintext[0]

Do it like this:

first_block = bytes(map(operator.xor, iv, plaintext[0]))

Browse Categories

...