# Level 1: Convert hex to base64

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

Convert hex to base64

The string&#x20;

```
49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d
```

Should produce:

```
SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t
```

So go ahead and make that happen. You'll need to use this code for the rest of the exercises.

{% hint style="warning" %}

#### Cryptopals Rule

Always operate on raw bytes, never on encoded strings. Only use hex and base64 for pretty-printing.
{% endhint %}

***

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

When we work in cryptography, what really matters are the bytes, not their text representation.\
In cryptography, data is not text; it consists of sequences of bytes. Encodings like hex or base64 exist only to make data readable for humans, but they do not affect the actual content.

If we work with strings, we might encounter errors due to different encodings that alter the bytes, for example.

We need to think of data as bytes, not text.

***

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

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

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXf4cIRoVQ4LKci-7dBHeosi80XLXwe6GyVKxR5JSGvJmBPQIusp_Ppk_Jw39yOlVp101jPLLuu33at0_ZLxcoU5ooOA67ZwAIJtqB9E-xnew7HdGqaKLjoKYDB0ZrvjmgSQ1UIQGA?key=0z75chVsoEW_cfTQtgh3riLM" alt=""><figcaption></figcaption></figure>

#### Understanding the code

* Two libraries are imported
  * *<mark style="color:blue;">binascii</mark>* to manipulate hexadecimal data
  * *<mark style="color:blue;">base64</mark>* for base64 encoding and decoding functions
* After that, three variables are created
  * `hex_string` , which contains the hex string provided by Cryptopals
  * `bytes_data` , which converts the hex string to bytes using *<mark style="color:blue;">unhexlify</mark>*
  * `base64_data` that encodes bytes to base64
* Finally, `base64_data` is decoded and printed

***

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

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXcxl0GrxrVDBYauk4ZekoPX8r94ptLE-yvgYDu-edxY9k8rsiwGol2jNKkSTt82DT82Lr7pbD5GCNcA1DY4LEcRsV52EmR8GL7tuml0VVWQEDxo0zAxtFPfC24GCpwIawAD9t1-rQ?key=0z75chVsoEW_cfTQtgh3riLM" alt=""><figcaption></figcaption></figure>
