cybersecurity · beginner · ~15 min · safe pentest lab
XOR every byte of a buffer with a one-byte key — the core of the simplest payload obfuscation.
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.
in (n bytes), a length n, a key byte, and an out buffer of at least n bytes.
out filled with the XOR-transformed bytes.
0 <= n; key is any byte 0-255; out has room for n bytes.
#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 */
}
Casting the result to char and sign-extending; forgetting that XOR twice with the same key is the identity.
n==0 writes nothing; key 0 copies the input unchanged.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.