# Level 5: Implement repeating-key XOR

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

Here is the opening stanza of an important work of the English language:

```
Burning 'em, if you ain't quick and nimble
I go crazy when I hear a cymbal
```

Encrypt it, under the key "ICE", using repeating-key XOR.

In repeating-key XOR, you'll sequentially apply each byte of the key; the first byte of plaintext will be XOR'd against I, the next C, the next E, then I again for the 4th byte, and so on.

It should come out to:

```
0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272
a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f
```

Encrypt a bunch of stuff using your repeating-key XOR function. Encrypt your mail. Encrypt your password file. Your .sig file. Get a feel for it. I promise, we aren't wasting your time with this.

***

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

How repeating-key works?

Repeating-key XOR is a method where the key is shorter than the string to encrypt. Once the key's characters are exhausted, it cycles back to the beginning and repeats until the entire string is processed.

Example:

String: <mark style="color:blue;">B u r n i n g</mark>

Key:      <mark style="color:orange;">I C E</mark>

XOR:   <mark style="color:blue;">B</mark> ⊕ <mark style="color:orange;">I</mark>, <mark style="color:blue;">u</mark> ⊕ <mark style="color:orange;">C</mark>, <mark style="color:blue;">r</mark> ⊕ <mark style="color:orange;">E</mark>, <mark style="color:blue;">n</mark> ⊕ <mark style="color:orange;">I</mark>, <mark style="color:blue;">i</mark> ⊕ <mark style="color:orange;">C</mark>, <mark style="color:blue;">n</mark> ⊕ <mark style="color:orange;">E</mark>, <mark style="color:blue;">g</mark> ⊕ <mark style="color:orange;">I</mark>

* <mark style="color:blue;">**B**</mark>**&#x20;⊕&#x20;**<mark style="color:orange;">**I**</mark> means that the first character of the string is XORed with the first character of the key.
* Once the last character of the key is reached, it loops back to the first character.
* This cycle continues until all characters in the string have been XORed.

***

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

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

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXdJrd97blXCm7UEhSHIFf-2voYIRBA4WuqQF4SVTNl05eA3ioJw-OAzFrAGXCX6ATrbLofHeKBBx98eg7Yv5YU453OsCKrD-lp-yBcbdVAOOUIOFIQnpXyVvOYEfn0PeAoJpsH_?key=0z75chVsoEW_cfTQtgh3riLM" alt=""><figcaption></figcaption></figure>

#### Understanding the code

* <mark style="color:blue;">`repeating_key_xor(plaintext: str, key:str) -> str`</mark> is a function that takes two string parameters and returns a string.
  * Three variables are created
    * `plaintext_bytes` stores the plaintext encoded to bytes.
    * `key_bytes` stores the key encoded to bytes.
    * `key_length` stores the length of `key_bytes`.
  * `ciphertext_bytes` is created as a result of the following expression:
    * <mark style="color:blue;">`bytes(plaintext_bytes[i] ^ key_bytes[i % key_lenght] for i in range(len(plaintext_bytes))`</mark>
      * <mark style="color:orange;">plaintext\_bytes\[i]</mark> represents the byte at the _i_-th position of the plaintext.
      * <mark style="color:orange;">key\_bytes\[i % key\_lenght]</mark> is the key byte used for XOR.
        * <mark style="color:orange;">i % key\_lenght</mark> is the trick that allows the key to repeat when it reaches the end.
      * <mark style="color:orange;">^</mark> is the XOR operator, which performs the XOR operation between the plaintext byte and the key byte.
      * <mark style="color:orange;">range(len(plaintext\_bytes))</mark> iterates over all the plaintext bytes.

> Following the previous example, it works as shown:

<table data-header-hidden><thead><tr><th width="40"></th><th width="98"></th><th width="164"></th><th width="98"></th><th width="267"></th><th width="68"></th></tr></thead><tbody><tr><td>i</td><td>plaintext character</td><td>byte (plaintext_bytes[i])</td><td>key character</td><td>Byte (key_bytes[i % key_lenght])</td><td>XOR result</td></tr><tr><td>0</td><td>B</td><td>66</td><td>I</td><td>73</td><td>11</td></tr><tr><td>1</td><td>u</td><td>117</td><td>C</td><td>67</td><td>54</td></tr><tr><td>2</td><td>r</td><td>114</td><td>E</td><td>69</td><td>55</td></tr><tr><td>3</td><td>n</td><td>110</td><td>I</td><td>73</td><td>39</td></tr><tr><td>4</td><td>i</td><td>105</td><td>C</td><td>67</td><td>46</td></tr><tr><td>5</td><td>n</td><td>110</td><td>E</td><td>69</td><td>43</td></tr><tr><td>6</td><td>g</td><td>103</td><td>I</td><td>73</td><td>46</td></tr></tbody></table>

* Finally, `plaintext` is initialized with the string provided by Cryptopals, and `key` is initialized with the key provided by Cryptopals. The result of the <mark style="color:blue;">`repeating_key_xor`</mark> function is printed.

***

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

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXdzfP74WR2YcBNr5LGKyD2OExzfcXY-usRGlAONoLGUwQwnbf5QxiAXe0twxpFlGMNbnStVP1VJ03Ya6d4ZGhH7by4DtX7VK15sga8unIMbgHCk4Vnm-bX4jwS_6wWbRwt8yU96zw?key=0z75chVsoEW_cfTQtgh3riLM" alt=""><figcaption></figcaption></figure>
