← Back to Writeups
CUSTOMEasyCrypto

Reversibility (Обратимость)

XESXOR9/10/20263 min read
#crypto#custom#easy#key_recovery#xor_properties

Reversibility (Обратимость)

Platform: duckerz | Category: Crypto | Type: Challenge | Difficulty: Easy | OS: NA | Author: XESXOR | Date: 2026-01-09 | Status: Solved Techniques: key_recovery, xor_properties

Summary

Task: Given XOR equations (key1, key1^key2, key2^key3, flag^key1^key2^key3). Solution: Use XOR reversibility and associativity properties to recover keys sequentially and decrypt the flag.

Reversibility (Обратимость) — Duckerz CTF

Description

"Для изучения симметричных алгоритмтов шифрования полезно знать как работает xor)."

(Translation: "To study symmetric encryption algorithms, it's useful to know how XOR works).")

Files: reversibility.txt

key1: 21af7f08b2152df088a5e72226c447d4d0d964bb46f0845b3f39f578b9748bd4440e17d04a20c0f4
key1 ^ key2: 9600e25b79ba3b5543adaad720a0343f6d1be23c7e5f95915f2f0835c4c74a8a0bb717ec6ff1d3f5
key2 ^ key3: cf27c4d4319805747929c7e9f758f3e4160b3fffa83b2b3ab0c689fb98273585b4cc534a6aabd384
flag ^ key1 ^ key3 ^ key2: aaddf897c6df72ffc5ff53fbb2ad8044f7a46a3097949b0feba00eb05760cc22c1a075f611ff6a0d

‍​‌‌​​‌​​​‌‌​​‌​​​​‌‌​‌​​​​‌‌‌​​‌​​‌‌​​​‌​​‌‌‌​​​​​‌‌​‌‌​​​‌‌​​​​‍

Analysis

The task demonstrates two fundamental properties of the XOR operation:

1. Reversibility

A ^ B ^ B = A

XOR is a self-inverse operation. Applying the same value twice cancels the effect.

2. Associativity

(A ^ B) ^ C = A ^ (B ^ C)

The order of grouping operations does not affect the result.

Solution Plan

Given:

  • key1
  • key1 ^ key2
  • key2 ^ key3
  • flag ^ key1 ^ key3 ^ key2

We can sequentially recover all keys:

  1. Get key2: key2 = key1 ^ (key1 ^ key2)
  2. Get key3: key3 = key2 ^ (key2 ^ key3)
  3. Get flag: flag = (flag ^ key1 ^ key3 ^ key2) ^ key1 ^ key2 ^ key3 ‍​‌‌​​‌​​​‌‌​​‌​​​​‌‌​‌​​​​‌‌‌​​‌​​‌‌​​​‌​​‌‌‌​​​​​‌‌​‌‌​​​‌‌​​​​‍

Solution

python
#!/usr/bin/env python3
"""
Duckerz CTF - Reversibility
XOR key recovery challenge
"""

# Data from file
key1 = bytes.fromhex("21af7f08b2152df088a5e72226c447d4d0d964bb46f0845b3f39f578b9748bd4440e17d04a20c0f4")
key1_xor_key2 = bytes.fromhex("9600e25b79ba3b5543adaad720a0343f6d1be23c7e5f95915f2f0835c4c74a8a0bb717ec6ff1d3f5")
key2_xor_key3 = bytes.fromhex("cf27c4d4319805747929c7e9f758f3e4160b3fffa83b2b3ab0c689fb98273585b4cc534a6aabd384")
flag_xor_keys = bytes.fromhex("aaddf897c6df72ffc5ff53fbb2ad8044f7a46a3097949b0feba00eb05760cc22c1a075f611ff6a0d")
‍​‌‌​​‌​​​‌‌​​‌​​​​‌‌​‌​​​​‌‌‌​​‌​​‌‌​​​‌​​‌‌‌​​​​​‌‌​‌‌​​​‌‌​​​​‍

# Step 1: Recover key2
# key2 = key1 ^ (key1 ^ key2)
key2 = bytes(a ^ b for a, b in zip(key1, key1_xor_key2))

# Step 2: Recover key3
# key3 = key2 ^ (key2 ^ key3)
key3 = bytes(a ^ b for a, b in zip(key2, key2_xor_key3))

# Step 3: Recover flag
# flag = (flag ^ key1 ^ key2 ^ key3) ^ key1 ^ key2 ^ key3
step1 = bytes(a ^ b for a, b in zip(flag_xor_keys, key1))
step2 = bytes(a ^ b for a, b in zip(step1, key2))
flag = bytes(a ^ b for a, b in zip(step2, key3))

print(f"Flag: {flag.decode()}")

Alternative Solution (one-liner)

‍​‌‌​​‌​​​‌‌​​‌​​​​‌‌​‌​​​​‌‌‌​​‌​​‌‌​​​‌​​‌‌‌​​​​​‌‌​‌‌​​​‌‌​​​​‍

python
from functools import reduce
from operator import xor

def xor_bytes(*args):
    return bytes(reduce(xor, x) for x in zip(*args))

flag = xor_bytes(flag_xor_keys, key1, key2, key3)

Theory

XOR Properties

PropertyFormulaDescription
CommutativityA ^ B = B ^ AOrder of operands doesn't matter
Associativity(A ^ B) ^ C = A ^ (B ^ C)Order of grouping doesn't matter
Self-inverseA ^ A = 0XOR with itself gives 0
Identity elementA ^ 0 = AXOR with zero doesn't change the value
ReversibilityA ^ B ^ B = ADouble XOR cancels out
‍​‌‌​​‌​​​‌‌​​‌​​​​‌‌​‌​​​​‌‌‌​​‌​​‌‌​​​‌​​‌‌‌​​​​​‌‌​‌‌​​​‌‌​​​​‍

Why This Works

flag ^ key1 ^ key2 ^ key3  (encrypted)
    ^ key1                  (cancel key1)
    ^ key2                  (cancel key2)  
    ^ key3                  (cancel key3)
= flag                      (get the flag)

Difficulty Level

Easy — basic task on understanding XOR properties. Ideal for beginners in cryptography. ‍​‌‌​​‌​​​‌‌​​‌​​​​‌‌​‌​​​​‌‌‌​​‌​​‌‌​​​‌​​‌‌‌​​​​​‌‌​‌‌​​​‌‌​​​​‍