# XOR

The XOR (Exclusive OR) operator is a logical operation that takes two binary inputs and returns a single binary output.

In cryptography, XOR is often used as a simple method for combining two values into a single value or for separating a single value into two parts.

## How it works ?

XOR is a bitwise operator which returns **`0`** if the bits are the same, and **`1`** otherwise. In textbooks the XOR operator is denoted by **`⊕`**, but in most programming languages you will see the char`^` used instead.

| A | B | Result |
| - | - | ------ |
| 0 | 0 | 0      |
| 0 | 1 | 1      |
| 1 | 1 | 0      |
| 1 | 0 | 1      |

For longer binary inputs, the XOR operation is done bit by bit : `1010 ^ 0111 = 1101` . If the two operand haven't the same size, then the shorter padded with non-significant `0` :

```
1010 ^ 01 = 1010 ^ 0001 = 1011
```

## Properties

Like other standard mathematical objects, xor has some specific properties :

```
Commutative  : A ⊕ B = B ⊕ A
Associative  : A ⊕ B ⊕ C = A ⊕ (B ⊕  C) = (A ⊕ B) ⊕ C
Identity     : A ⊕ 0 = A
Self-Inverse : A ⊕ A = 0
```

These properties are very interesting cause it permit the following :

$$
D = A \oplus B \ A \oplus D = A \oplus ( A \oplus B) = A \oplus A \oplus B = (A \oplus A) \oplus B = 0 \oplus B = B
$$

## Python

```python
from pwn import *

A = "someData"
B = "OtherData"

xored = xor(A,B)
```

## Resources

{% embed url="<https://gchq.github.io/CyberChef/#recipe=XOR(%7B>" %}


---

# Agent Instructions: 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:

```
GET https://www.ctfrecipes.com/cryptography/misc/xor.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
