Cryptopals challenge 7: Implement AES in ECB mode with Python

This is my write up of the seventh Cryptopals challenge, using Python3 as my language of choice. The challenge:

AES in ECB mode

The Base64-encoded content in this file has been encrypted via AES-128 in ECB mode under the key

“YELLOW SUBMARINE”.

(case-sensitive, without the quotes; exactly 16 characters; I like “YELLOW SUBMARINE” because it’s exactly 16 bytes long, and now you do too).

Decrypt it. You know the key, after all.

Easiest way: use OpenSSL::Cipher and give it AES-128-ECB as the cipher.

Do this with code.

You can obviously decrypt this using the OpenSSL command-line tool, but we’re having you get ECB working in code for a reason. You’ll need it a lot later on, and not just for attacking ECB.

This challenge has two steps: 1) Decode the Base64 encoded content; 2) Decrypt the decoded AES-128 ECB encrypted content.

Background on AES ECB

From Wikipedia: The simplest of the encryption modes is the Electronic Codebook (ECB) mode (named after conventional physical codebooks). The message is divided into blocks, and each block is encrypted separately.

 

Step one: Decode the Base64 encoded content from the file


>>> import base64
>>> with open('7.txt') as fh:
...         ciphertext = base64.b64decode(fh.read())

Step 2: Decrypt the decoded AES-128 ECB encrypted content

When I did a quick Internet search I found that Python3 doesn’t have many cryptographic modules in the standard lib with the exception of modules for hashing and for number generation (https://docs.python.org/3/library/crypto.html). I ended up using the 3rd party, PyCryptodome to handle the AES decryption. I followed the documentation for encryption and decryption with AES and was able to solve the challenge.


>>> import base64
>>> with open('7.txt') as fh:
...         ciphertext = base64.b64decode(fh.read())
...
>>> key = b'YELLOW SUBMARINE'
>>> from Crypto.Cipher import AES
>>> key = b'YELLOW SUBMARINE'
>>> cipher = AES.new(key, AES.MODE_ECB)
>>> plaintext = cipher.decrypt(ciphertext)
>>> plaintext
b"I'm back and I'm ringin' the bell \nA rockin' on the mike while the fly girls yell \nIn ecstasy in the back of me \nWell that's my DJ Deshay cuttin' all them Z's \n…

Here is the completed script:

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *