> For the complete documentation index, see [llms.txt](https://cajac.gitbook.io/ctf-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cajac.gitbook.io/ctf-notes/misc/image-processing/xor-images.md).

# XOR Images

## ImageMagick

We can use the `convert` tool from [ImageMagick](https://imagemagick.org/index.php)

```bash
convert image1.png image2.png -evaluate-sequence xor result.png
```

The XORed image will be saved as `result.png`.

## Paint.Net solution

We can use [Paint.NET](https://www.getpaint.net/index.html) as follows

1. In the `File`-menu, select `Open...` and choose the the first file
2. In the `Layers`-menu select `Import From File...` and choose the second file
3. In the `Layers`-menu select `Layer Properties` and set `Blend Mode` to `Xor` and press `OK`

And the XORed image will be displayed.

## Python solution

We can solve this in Python with the help of [Pillow](https://pypi.org/project/Pillow/) and [pwntools](https://docs.pwntools.com/en/stable/index.html)

```python
#!/usr/bin/env python3

from PIL import Image
from pwn import *

result_file = "xored.image.png"

image1 = Image.open("image1.png")
image2 = Image.open("image2.png")

xored_bytes = xor(image1.tobytes(), image2.tobytes())
xored_image = Image.frombytes(image1.mode, image2.size, xored_bytes)

print(f"Saving the xored image as: {result_file}")
xored_image.save(result_file)
```

## Resources

Exclusive or - Wikipedia: <https://en.wikipedia.org/wiki/Exclusive_or>

ImageMagick - Homepage: <https://imagemagick.org/index.php>

Paint.Net - Homepage: <https://www.getpaint.net/index.html>

pwntools - Documentation: <https://docs.pwntools.com/en/stable/index.html>

Python Imaging Library - Pillow: <https://pypi.org/project/Pillow/>
