“Missing Pieces” by syyntax hides a flag behind a hex string XOR’d with a short key. Fix the incomplete C program, convert hex → bytes, XOR with the key, and print the result.
MISSING PIECES
File
> Download (4 KB)
SHA1
895ca32d397db50ee2fe3ad7021a63efc5dc7582
Convert the hex-encoded ciphertext to bytes, convert the ASCII key to bytes, then XOR:
plain[i] = cipher[i] ^ key[i % len(key)]. Print as characters to reveal the flag.
#include <stdio.h>
#include <string.h>
const char *hex_string = "b52195a4a82bc5ade23e9c9c8725c79cb07d90f0ae";
const char *key = "d34df4c3";
int hex_char_to_int(char c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return -1;
}
void hex_string_to_bytes(const char *hex, unsigned char *bytes, size_t length) {
for (size_t i = 0; i < length; ++i) {
int hi = hex_char_to_int(hex[i * 2]);
int lo = hex_char_to_int(hex[i * 2 + 1]);
bytes[i] = (unsigned char)((hi << 4) | lo);
}
}
void xor_bytes(unsigned char *data, size_t data_length, const unsigned char *key, size_t key_length) {
for (size_t i = 0; i < data_length; ++i) {
data[i] ^= key[i % key_length];
}
}
int main(void) {
size_t flag_length = strlen(hex_string) / 2;
unsigned char flag[256] = {0};
unsigned char key_bytes[4] = {0};
hex_string_to_bytes(hex_string, flag, flag_length);
hex_string_to_bytes(key, key_bytes, 4);
xor_bytes(flag, flag_length, key_bytes, 4);
printf("Resulting string: ");
for (size_t i = 0; i < flag_length; ++i) {
printf("%c", flag[i]);
}
printf("\\n");
return 0;
}
flag{f1n1sh_Th3_c0d3}