Handle data length not multiple of key length

This commit is contained in:
Horseshoe Crab 2022-04-14 00:26:09 -07:00
parent e6f5696c73
commit 0e4aa9c416
2 changed files with 18 additions and 12 deletions

View File

@ -13,16 +13,16 @@
//#define b64_output b64_out_debug //#define b64_output b64_out_debug
size_t out_index = 0; 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); printf("Using '%c' from base64 alphabet\n", c);
out_buf[out_index] = c; // TODO: check out_index out_buf[out_index] = c; // TODO: check out_index
++out_index; ++out_index;
} }
char *b64_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; int8_t b64_alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int main() int main()

View File

@ -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; uint8_t c;
size_t i; size_t i, k;
for (i = 0; i < key_len; ++i) {
c = data[i] ^ key[i]; for (i = 0; i < data_len; i = i + key_len) {
putchar(c); 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"; uint8_t data[] = "\x1c\x01\x11\x00\x1f\x01\x01\x00\x06\x1a\x02\x4b\x53"
char key[] = "\x68\x69\x74\x20\x74\x68\x65\x20\x62\x75\x6c\x6c\x27\x73\x20\x65\x79\x65"; "\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; return EXIT_SUCCESS;
} }