# Level 7: AES in ECB mode

## <mark style="color:purple;">Task</mark>

The Base64-encoded content [in this file](https://cryptopals.com/static/challenge-data/7.txt) 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.

{% hint style="warning" %}

#### 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.
{% endhint %}

***

### <mark style="color:purple;">Explanation</mark>

**What is AES?**

AES (Advanced Encryption Standard) is a symmetric cipher algorithm that operates on 16-byte blocks. It is widely used for secure data encryption. AES supports several operation modes, including ECB, CBC, and GCM, among others.

**What is ECB?**

ECB (Electronic Codebook) is one of the operation modes of AES-128. It determines how AES processes multiple data blocks. In ECB mode, the message is split into 16-byte blocks, and each block is encrypted or decrypted independently using the same key.

However, ECB does not use an Initialization Vector (IV), a random value that provides additional security. As a result, ECB is vulnerable to certain attacks, especially when encrypting repetitive or structured data.

In this exercise, we need to use **AES-128-ECB** because the cipher key is already provided.

***

### <mark style="color:purple;">Resolution</mark>

First, we are going to create a file named `task7.py`

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfX1Diel5TLChAxSHdGWSa8Dbyds7H9_yflt4WC5_Dsr6NodBuQRUWEUzV90hrNz8guiGXOAQr12l9fVuc_Tz52cQiO9w0J9iHp-iZ-WfNG-W4fBU0JITwf_bRkvQ12P3E6fUvy?key=0z75chVsoEW_cfTQtgh3riLM" alt=""><figcaption></figcaption></figure>

Understanding the code

* <mark style="color:blue;">AES</mark> and <mark style="color:blue;">base64</mark> libraries are imported to handle AES decryption and Base64 decoding.
* The script opens the **7.txt** file with <mark style="color:blue;">`open()`</mark> in read mode, assigning its content to the `encrypted_base64` variable.
  * You can download the file using a terminal command:
    * <https://cryptopals.com/static/challenge-data/7.txt> > 7.txt
* `encrypted_base64` is then Base64 decoded and stored in the `encrypted_bytes` variable.
* The `key`  variable is initialized with the string <mark style="color:yellow;">YELLOW SUBMARINE</mark>, which is the decryption key provided in the challenge.
* `cipher` is created as a new AES object, where the <mark style="color:orange;">`key`</mark> and <mark style="color:orange;">`AES.MODE_ECB`</mark> are passed as parameters.
* `decrypted_bytes` is initialized by decrypting `encrypted_bytes` using the <mark style="color:blue;">`cipher.decrypt()`</mark> method.
* `decrypted_text` is initialized with `decrypted_bytes` decoded to "<mark style="color:green;">UTF-8</mark>", converting the bytes to a readable string.
* Finally, `decrypted_text` is printed, revealing the plaintext message.

***

### <mark style="color:purple;">Result</mark>

The result is the same that task 6.

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXfLlD2UqggKDYQ_jnn95AIL9eW0GfFanYzScn7fQ2CM-JVc0UVXt0BLCPXfbQOLnb_ygYW-cvoOHpfRNT29k-mqFO0NF0fyd0K50TXQfzhRx_VR6buCxXEJ7lP8PI2NbAU_2KJ4mQ?key=0z75chVsoEW_cfTQtgh3riLM)
