> For the complete documentation index, see [llms.txt](https://soyabeanboi.gitbook.io/bag-of-flags/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://soyabeanboi.gitbook.io/bag-of-flags/practice/cryptohack/general/xor-properties.md).

# XOR Properties

> In the last challenge, you saw how XOR worked at the level of bits. In this one, we're going to cover the properties of the XOR operation and then use them to undo a chain of operations that have encrypted a flag. Gaining an intuition for how this works will help greatly when you come to attacking real cryptosystems later, especially in the block ciphers category.\
> \
> There are four main properties we should consider when we solve challenges using the XOR operator\
> \
> `Commutative: A ⊕ B = B ⊕ A` \
> `Associative: A ⊕ (B ⊕ C) = (A ⊕ B) ⊕ C`\
> `Identity: A ⊕ 0 = A`\
> `Self-Inverse: A ⊕ A = 0`\
> \
> Let's break this down. Commutative means that the order of the XOR operations is not important. Associative means that a chain of operations can be carried out without order (we do not need to worry about brackets). The identity is 0, so XOR with 0 "does nothing", and lastly something XOR'd with itself returns zero.\
> \
> Let's put this into practice! Below is a series of outputs where three random keys have been XOR'd together and with the flag. Use the above properties to undo the encryption in the final line to obtain the flag.\
> \
> `KEY1 = a6c8b6733c9b22de7bc0253266a3867df55acde8635e19c73313`\
> `KEY2 ^ KEY1 = 37dcb292030faa90d07eec17e3b1c6d8daf94c35d4c9191a5e1e`\
> `KEY2 ^ KEY3 = c1545756687e7573db23aa1c3452a098b71a7fbf0fddddde5fc1`\
> `FLAG ^ KEY1 ^ KEY3 ^ KEY2 = 04ee9855208a2cd59091d04767ae47963170d1660df7f56f5faf`\
> \
> \&#xNAN;*Before you XOR these objects, be sure to decode from hex to bytes.*

## Goodbye XOR

The most important property is the *self-inverse* property since it gives us an easy way to undo the keys

The critical thing to note is `(A ⊕ B) ⊕ A = B` so we can remove the effect of `A` XOR on `B`

What we want is `FLAG`, so we need to remove `KEY1`, `KEY2`, and `KEY3` by XORing the respective values

* We have `FLAG ^ KEY1 ^ KEY3 ^ KEY2`
* We are also given `KEY1` and `KEY2 ^ KEY3` and these 2 strings give us one instance of each key
* Thus if we XOR all of these strings together, we can isolate the `FLAG`

-> Due to associativity, the order in which things are XORed does not matter

```python
from pwn import xor

KEY1 = "a6c8b6733c9b22de7bc0253266a3867df55acde8635e19c73313"
KEY2_KEY1 = "37dcb292030faa90d07eec17e3b1c6d8daf94c35d4c9191a5e1e"
KEY2_KEY3 = "c1545756687e7573db23aa1c3452a098b71a7fbf0fddddde5fc1"
FLAG_KEY1_KEY3_KEY2 = "04ee9855208a2cd59091d04767ae47963170d1660df7f56f5faf"

# Convert to bytes
k1 = bytes.fromhex(KEY1)
k2k3 = bytes.fromhex(KEY2_KEY3)
fk1k3k2 = bytes.fromhex(FLAG_KEY1_KEY3_KEY2)

# XOR everything
flag = xor(k1, xor(k2k3, fk1k3k2))

print(flag)
```

## Flag

`crypto{x0r_i5_ass0c1at1v3}`


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://soyabeanboi.gitbook.io/bag-of-flags/practice/cryptohack/general/xor-properties.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
