Browse Source

XORcipher

master
parent
commit
d097a50319
  1. 31
      src/crypt/xorcipher.c
  2. 17
      src/crypt/xorcipher.h
  3. 40
      testing/test.c

31
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 <stdio.h>
// 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;
}
}
}

17
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);

40
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;
}
Loading…
Cancel
Save