DEV Community

Victor
Victor

Posted on

Order — Breaking Repeating-Key XOR with a Known-Plaintext Attack

1. Introduction

In this write-up, I will document my experience completing the Order room on TryHackMe, part of the Hackfinity series.

The scenario involves intercepting an encrypted message from a group called Cipher. The group uses a repeating-key XOR cipher to protect its communications. However, they make a critical mistake: every message starts with the same predictable header:

ORDER:
Enter fullscreen mode Exit fullscreen mode

My objective was to analyse the ciphertext, recover the encryption key, and decrypt the intercepted message to discover Cipher's next target.

The main concepts covered in this challenge are:

  • Hexadecimal encoding
  • XOR encryption
  • Repeating-key XOR
  • Known-plaintext attacks
  • Python scripting for cryptanalysis

What I found interesting was how a small piece of known information could be used to recover the encryption key without brute-forcing it.

2. Understanding the Challenge

After opening the room, I reviewed the challenge description.

We were given an intercepted message represented as hexadecimal data:

1c1c01041963730f31352a3a386e24356b3d32392b6f6b0d323c22243f6373
1a0d0c302d3b2b1a292a3a38282c2f222d2a112d282c31202d2d2e24352e60
Enter fullscreen mode Exit fullscreen mode

The challenge provided two important clues:

  1. The encryption algorithm was repeating-key XOR.
  2. Every message started with the known plaintext ORDER:.

At this point, I had enough information to start analysing the ciphertext.

Instead of immediately trying online decryption tools, I wanted to understand why the encryption could be broken mathematically.

3. Understanding Repeating-Key XOR

Before writing a script, I reviewed how XOR encryption works.

XOR is a bitwise operation that compares two binary values.

Its basic truth table is:

A B A XOR B
0 0 0
0 1 1
1 0 1
1 1 0

One important property of XOR is that applying the same key twice restores the original value:

[ (P \oplus K) \oplus K = P ]

Where:

  • (P) represents the plaintext.
  • (K) represents the encryption key.
  • (C) represents the ciphertext.

Encryption:

[ C = P \oplus K ]

Decryption:

[ P = C \oplus K ]

In a repeating-key XOR cipher, the key is repeated until it is long enough to encrypt the entire message.

For example, if the key is ABC, the key stream looks like this:

Plaintext:  HELLOWORLD
Key:        ABCABCABCA
Enter fullscreen mode Exit fullscreen mode

Each plaintext byte is XORed with the corresponding key byte.

The problem with this approach is that if an attacker knows part of the plaintext, they may be able to recover the corresponding key bytes.

That is exactly the weakness exploited in this challenge.

4. Identifying the Known Plaintext

The most important clue was the predictable message header:

ORDER:
Enter fullscreen mode Exit fullscreen mode

This is known as a known-plaintext attack.

A known-plaintext attack occurs when an attacker has access to both:

  • A portion of the original plaintext.
  • The corresponding encrypted ciphertext.

Because XOR is reversible, we can calculate the encryption key using:

[ K = C \oplus P ]

In this challenge, the first six plaintext characters were already known.

I converted the header into its hexadecimal ASCII representation:

ORDER:
Enter fullscreen mode Exit fullscreen mode
4f 52 44 45 52 3a
Enter fullscreen mode Exit fullscreen mode

The first six ciphertext bytes were:

1c 1c 01 04 19 63
Enter fullscreen mode Exit fullscreen mode

I XORed the corresponding bytes:

Plaintext Ciphertext XOR result
O (4f) 1c 53 (S)
R (52) 1c 4e (N)
D (44) 01 45 (E)
E (45) 04 41 (A)
R (52) 19 4b (K)
: (3a) 63 59 (Y)

This gave me the repeating XOR key.

The important point is that I did not need to guess the key or test thousands of possibilities. The predictable header provided enough known plaintext to recover it.

5. Writing a Python Decryption Script

After understanding the mathematical relationship, I decided to automate the process using Python.

My approach consisted of four steps:

  1. Combine the two hexadecimal ciphertext sections.
  2. Convert the hexadecimal string into bytes.
  3. Recover the XOR key using the known plaintext.
  4. Apply the repeating key to decrypt the complete message.

Here is the script I used:

import re

# Intercepted ciphertext
ciphertext_hex = (
    "1c1c01041963730f31352a3a386e24356b3d32392b6f6b0d323c22243f6373"
    "1a0d0c302d3b2b1a292a3a38282c2f222d2a112d282c31202d2d2e24352e60"
)

# Convert hexadecimal data into bytes
ciphertext = bytes.fromhex(ciphertext_hex)

# Known plaintext
known_plaintext = b"ORDER:"

# Recover the XOR key
key = bytes(
    ciphertext[i] ^ known_plaintext[i]
    for i in range(len(known_plaintext))
)

print("[+] Recovered key:", key.decode())

# Decrypt using repeating-key XOR
plaintext = bytes(
    ciphertext[i] ^ key[i % len(key)]
    for i in range(len(ciphertext))
)

message = plaintext.decode()

# Redact any flag before displaying the output
message = re.sub(
    r"THM\{[^}]*\}",
    "THM{[REDACTED]}",
    message
)

print("[+] Decrypted message:")
print(message)
Enter fullscreen mode Exit fullscreen mode

5.1 Understanding the important parts

Converting hexadecimal to bytes

ciphertext = bytes.fromhex(ciphertext_hex)
Enter fullscreen mode Exit fullscreen mode

The intercepted data is represented as hexadecimal text. Before performing XOR operations, I converted it into its corresponding byte values.

Recovering the key

key = bytes(
    ciphertext[i] ^ known_plaintext[i]
    for i in range(len(known_plaintext))
)
Enter fullscreen mode Exit fullscreen mode

This is the most important part of the solution.

I XORed the first six ciphertext bytes with the six known plaintext bytes.

Because:

[ C = P \oplus K ]

We can rearrange the operation:

[ K = C \oplus P ]

The result gives us the key bytes.

Decrypting the complete message

plaintext = bytes(
    ciphertext[i] ^ key[i % len(key)]
    for i in range(len(ciphertext))
)
Enter fullscreen mode Exit fullscreen mode

The modulo operator % ensures that the key repeats when the ciphertext is longer than the key.

For example:

Ciphertext position: 0 1 2 3 4 5 6 7 8 9
Key position:       0 1 2 3 4 5 0 1 2 3
Enter fullscreen mode Exit fullscreen mode

This allows the same key to decrypt the entire message.

Redacting the flag

I added a regular expression to replace any THM{...} value before printing the result. This keeps the technical output useful without exposing the room's flag.

6. Analysing the Decrypted Message

After running the script, I could read the intercepted message.

The output revealed an instruction containing an attack time and a target.

For this write-up, I am intentionally redacting the sensitive target value:

ORDER: Attack at dawn. Target: [REDACTED]
Enter fullscreen mode Exit fullscreen mode

This confirmed that the decryption process was successful.

The challenge was not about discovering a complicated cryptographic implementation. It was about recognising that the encryption scheme had a predictable structure that could be exploited.

7. Why Was the Encryption Vulnerable?

The fundamental problem was the use of repeating-key XOR with a predictable plaintext header.

There are several reasons why this design is insecure.

7.1 Predictable plaintext

The messages always started with:

ORDER:
Enter fullscreen mode Exit fullscreen mode

This gave an attacker a known plaintext segment.

If the attacker obtains the corresponding ciphertext bytes, they can recover the key bytes used for that segment.

7.2 Repeating encryption key

The same key was reused throughout the message.

Once the key was recovered, the attacker could decrypt every byte encrypted with that repeating key.

7.3 XOR is reversible

XOR itself is not inherently insecure. It is used in many cryptographic constructions.

However, repeating a short XOR key does not provide secure encryption.

A one-time pad can provide strong confidentiality when the key is truly random, as long as the plaintext, used only once, and kept secret. Reusing a short key removes those security properties.

8. How Could This Be Prevented?

If I were reviewing this implementation from a security engineering perspective, I would recommend replacing the custom repeating-key XOR scheme with a standard authenticated encryption algorithm.

For example:

Weakness Recommended improvement
Repeating-key XOR Use AES-256-GCM or ChaCha20-Poly1305
Short, reusable key Use securely generated cryptographic keys
Predictable plaintext structure Use a secure encryption construction rather than relying on message formatting
No integrity protection Use authenticated encryption to detect tampering
Poor key management Store and rotate keys using a secure key-management system

A secure encryption design should provide both confidentiality and integrity.

It should also use proper key generation, nonce management, and secure key storage.

9. Key Takeaways

This challenge reinforced several important cryptography concepts for me.

1. Understand the encoding before attacking the encryption.

Hexadecimal representation is not encryption. It is simply a way to represent binary data as readable characters.

2. Known plaintext can expose encryption keys.

A predictable message header can provide enough information to recover a repeating XOR key.

3. XOR is reversible.

The same operation can be used for encryption and decryption when the key is known.

4. Reusing a short key is dangerous.

Repeating-key XOR is vulnerable to cryptanalysis, particularly when the plaintext contains predictable patterns.

5. Python is useful for automating cryptanalysis.

A small script can perform byte conversion, key recovery, and decryption much faster and more reliably than manual calculations.

10. Conclusion

The Order room was a short but valuable introduction to practical cryptanalysis.

What I found most interesting was how a simple piece of information — the predictable ORDER: header — was enough to recover the repeating XOR key and decrypt the intercepted communication.

My biggest takeaway is that encryption should never rely on obscurity or predictable key reuse. Even a seemingly small implementation mistake can completely undermine the confidentiality of an encrypted message.

This challenge helped me better understand XOR operations, known-plaintext attacks, and how to use Python to automate cryptographic analysis.


Author: Victor Hoang
Platform: TryHackMe
Room: Order
Category: Cryptography
Exploit Reference: Known-Plaintext Attack · Repeating-Key XOR

Top comments (0)