cybersecurity · beginner · ~15 min · safe pentest lab

Apply a single-byte XOR key

XOR every byte of a buffer with a one-byte key — the core of the simplest payload obfuscation.

Challenge

Malware droppers often hide a payload with a single-byte XOR. Implement the transform:

void xor_apply(const unsigned char *in, size_t n, unsigned char key, unsigned char *out);

Write in[i] ^ key into out[i] for every byte. XOR is its own inverse, so the same call decodes what it encoded.

Input format

in (n bytes), a length n, a key byte, and an out buffer of at least n bytes.

Output format

out filled with the XOR-transformed bytes.

Constraints

0 <= n; key is any byte 0-255; out has room for n bytes.

Starter code

#include <stddef.h>
/* Apply a single-byte XOR key to every byte of `in` (length n) into `out`. */
void xor_apply(const unsigned char *in, size_t n, unsigned char key, unsigned char *out) {
    (void)key;
    for (size_t i = 0; i < n; i++) out[i] = in[i];   /* TODO: XOR with key */
}

Common mistakes

Casting the result to char and sign-extending; forgetting that XOR twice with the same key is the identity.

Edge cases to handle

n==0 writes nothing; key 0 copies the input unchanged.

Background lessons

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.