cybersecurity · intermediate · ~15 min · safe pentest lab
Brute-force all 256 keys and pick the one whose decode looks like text — how analysts crack single-byte XOR.
Given a single-byte-XOR blob, recover the key without knowing it:
int recover_xor_key(const unsigned char *buf, size_t n);
Try every key 0-255, score each decode by how many bytes are printable ASCII (0x20-0x7e, plus tab/newline), and return the key with the highest score. On a tie, return the lowest key.
buf of n bytes.
The best key (0-255).
Ties resolve to the lowest key; empty input returns 0.
#include <stddef.h>
/* Return the single-byte key (0-255) whose XOR-decode of buf has the most
printable bytes. On ties, return the lowest such key. */
int recover_xor_key(const unsigned char *buf, size_t n) {
(void)buf; (void)n;
return 0; /* TODO */
}
Using >= in the tie comparison (returns the highest key instead of the lowest); treating bytes as signed.
All-zero or tiny buffers may tie — return the lowest maximizing key.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.