From 0e4aa9c416921fbac9ee1d855621fb45292d4980 Mon Sep 17 00:00:00 2001 From: x1phosura Date: Thu, 14 Apr 2022 00:26:09 -0700 Subject: [PATCH] Handle data length not multiple of key length --- set1/src/base64.c | 6 +++--- set1/src/fixed-xor.c | 24 +++++++++++++++--------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/set1/src/base64.c b/set1/src/base64.c index f9d0960..c00bc9a 100644 --- a/set1/src/base64.c +++ b/set1/src/base64.c @@ -13,16 +13,16 @@ //#define b64_output b64_out_debug size_t out_index = 0; -uint8_t out_buf[BUF_LEN]; +int8_t out_buf[BUF_LEN]; -void b64_out_debug(char c) +void b64_out_debug(int8_t c) { printf("Using '%c' from base64 alphabet\n", c); out_buf[out_index] = c; // TODO: check out_index ++out_index; } -char *b64_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +int8_t b64_alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; int main() diff --git a/set1/src/fixed-xor.c b/set1/src/fixed-xor.c index 0f95f02..9ff90ff 100644 --- a/set1/src/fixed-xor.c +++ b/set1/src/fixed-xor.c @@ -9,22 +9,28 @@ void fixed_xor_stream(uint8_t *key, size_t key_len) } */ -void fixed_xor(const uint8_t *data, const uint8_t *key, const size_t key_len) +void fixed_xor(const uint8_t *data, const size_t data_len, + const uint8_t *key, const size_t key_len) { uint8_t c; - size_t i; - for (i = 0; i < key_len; ++i) { - c = data[i] ^ key[i]; - putchar(c); + size_t i, k; + + for (i = 0; i < data_len; i = i + key_len) { + for (k = 0; k < key_len && i+k < data_len; ++k) { + c = data[i+k] ^ key[k]; + putchar(c); + } } } -int main(int argc, char *argv[]) +int main() { - char data[] = "\x1c\x01\x11\x00\x1f\x01\x01\x00\x06\x1a\x02\x4b\x53\x53\x50\x09\x18\x1c"; - char key[] = "\x68\x69\x74\x20\x74\x68\x65\x20\x62\x75\x6c\x6c\x27\x73\x20\x65\x79\x65"; + uint8_t data[] = "\x1c\x01\x11\x00\x1f\x01\x01\x00\x06\x1a\x02\x4b\x53" + "\x53\x50\x09\x18\x1c\x00\x00\x00\x00\x00\x00\x00"; + uint8_t key[] = "\x68\x69\x74\x20\x74\x68\x65\x20\x62\x75\x6c\x6c\x27" + "\x73\x20\x65\x79\x65"; - fixed_xor(data, key, sizeof(key)); + fixed_xor(data, sizeof(data)-1, key, sizeof(key)-1); return EXIT_SUCCESS; }