From d097a503192c2699005df16a34e6d47f0aeb1cb3 Mon Sep 17 00:00:00 2001 From: Unbewohnte Date: Sat, 28 Jan 2023 20:29:16 +0300 Subject: [PATCH] XORcipher --- src/crypt/xorcipher.c | 31 +++++++++++++++++++++++++++++++ src/crypt/xorcipher.h | 17 +++++++++++++++++ testing/test.c | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 src/crypt/xorcipher.c create mode 100644 src/crypt/xorcipher.h diff --git a/src/crypt/xorcipher.c b/src/crypt/xorcipher.c new file mode 100644 index 0000000..d23fa72 --- /dev/null +++ b/src/crypt/xorcipher.c @@ -0,0 +1,31 @@ +/* +The MIT License (MIT) + +Copyright © 2023 Kasyanov Nikolay Alexeyevich (Unbewohnte) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#include "xorcipher.h" +#include + +// XOR character with a key +char xor_char(char data, char key) { + return (char) (data ^ key); +} + +// XOR data with given key +void xor_str(char data[], unsigned int data_len, const char* key, unsigned int key_len) { + unsigned int key_index = 0; + for (unsigned int i = 0; i < data_len; i++) { + data[i] = data[i] ^ key[key_index]; + key_index++; + if (key_index == key_len) { + key_index = 0; + } + } +} \ No newline at end of file diff --git a/src/crypt/xorcipher.h b/src/crypt/xorcipher.h new file mode 100644 index 0000000..184497f --- /dev/null +++ b/src/crypt/xorcipher.h @@ -0,0 +1,17 @@ +/* +The MIT License (MIT) + +Copyright © 2023 Kasyanov Nikolay Alexeyevich (Unbewohnte) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +// XOR character with a key +char xor_char(char data, char key); + +// XOR data with given key +void xor_str(char data[], unsigned int data_len, const char* key, unsigned int key_len); \ No newline at end of file diff --git a/testing/test.c b/testing/test.c index 6771bc7..3793378 100644 --- a/testing/test.c +++ b/testing/test.c @@ -27,6 +27,7 @@ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR I #include "../src/math/misc.h" #include "../src/datastruct/cvec.h" #include "../src/strings/levenshtein.h" +#include "../src/crypt/xorcipher.h" int test_rng() { lcg(76); @@ -326,6 +327,37 @@ int test_strings() { return EXIT_SUCCESS; } +int test_xorcipher() { + char xored = xor_char('a', 'k'); + if (xored != ('a' ^ 'k')) { + printf("[ERROR] XORing char does not result in XORed char: got %d; expected %d\n", xored, ('a' ^ 'k')); + return EXIT_FAILURE; + } + + char data[] = "text"; + unsigned int data_len = (unsigned int) strlen(data); + char old_data[] = "text"; + char key[] = "ключ"; + unsigned int key_len = (unsigned int) strlen(key); + + xor_str(data, data_len, key, key_len); + if (strcmp(data, old_data) == 0) { + printf("[ERROR] Failed to XOR string with key: nothing's changed\n"); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} + +int test_crypt() { + if (test_xorcipher() == EXIT_FAILURE) { + printf("[ERROR] XORcipher test failed\n"); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} + int main() { // rng printf("[INFO] Testing rng...\n"); @@ -391,5 +423,13 @@ int main() { printf("[INFO] Strings test passed\n\n"); } + // crypt + printf("[INFO] Testing crypt...\n"); + if (test_crypt() == EXIT_FAILURE) { + printf("[INFO] Crypt test failed\n\n"); + } else { + printf("[INFO] Crypt test passed\n\n"); + } + return EXIT_SUCCESS; } \ No newline at end of file