Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_crypto.c

Nilorea Library crypto vigenere api test.

Nilorea Library crypto vigenere api test

Author
Castagnier Mickael
Version
1.0
Date
26/05/2015
/*
* Nilorea Library
* Copyright (C) 2005-2026 Castagnier Mickael
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "nilorea/n_log.h"
#include "nilorea/n_list.h"
#include "nilorea/n_str.h"
int main(int argc, char* argv[]) {
void usage(void) {
fprintf(stderr,
"%s usage:\n"
" -i input_file [optional] use a file as datas instead of hard coded 3 lines text\n"
" -v version\n"
" -V log level: LOG_INFO, LOG_NOTICE, LOG_ERR, LOG_DEBUG\n"
" -h help\n",
argv[0]);
}
int getoptret = 0, ret = 0,
log_level = LOG_DEBUG; /* default log level */
N_STR* input_name = NULL;
/* Arguments optionnels */
/* -v version
* -V log level
* -h help
*/
while ((getoptret = getopt(argc, argv, "i:hvV:")) != EOF) {
switch (getoptret) {
case 'i':
input_name = char_to_nstr(optarg);
break;
case 'v':
fprintf(stderr, "Date de compilation : %s a %s.\n", __DATE__, __TIME__);
exit(1);
case 'V':
if (!strcmp("LOG_NULL", optarg))
else if (!strcmp("LOG_NOTICE", optarg))
else if (!strcmp("LOG_INFO", optarg))
else if (!strcmp("LOG_ERR", optarg))
else if (!strcmp("LOG_DEBUG", optarg))
else {
fprintf(stderr, "%s n'est pas un niveau de log valide.\n", optarg);
exit(-1);
}
break;
default:
case '?': {
if (optopt == 'V') {
fprintf(stderr, " => Missing log level\n");
} else if (optopt == 'p') {
fprintf(stderr, " => Missing port\n");
} else if (optopt == 'i') {
fprintf(stderr, " => Missing filename\n");
} else if (optopt != 's') {
fprintf(stderr, " => Unknow missing option %c\n", optopt);
}
usage();
exit(1);
}
case 'h': {
usage();
exit(1);
}
}
}
N_STR* EXAMPLE_TEXT = char_to_nstr(
"##############################################################\n"
"# This is an example of crypto encode/decode, with a newline #\n"
"# This is the end of the test #\n"
"##############################################################");
n_log(LOG_NOTICE, "Testing crypto, encoding text of size (%ld/%ld):\n%s", EXAMPLE_TEXT->written, EXAMPLE_TEXT->length, _nstr(EXAMPLE_TEXT));
/* 10 20 30 40 50 60 */
/* "1234567890123456789012345678901234567890123456789012345678901234" */
/* test n_vigenere_get_rootkey */
N_STR* rootkey = n_vigenere_get_rootkey(128);
n_log(LOG_DEBUG, "n_vigenere_get_rootkey(128): %s", _nstr(rootkey));
free_nstr(&rootkey);
N_STR* key = char_to_nstr("RKCHSLZWFNASULJFVPRVUUUAEUVEMSSGEVNWIMVPZVIWITBGIUBEQVEURBYEWTMCQZZYNWQMRAIBTMHDIKIZHOVZQUFONRQDSRDFNTTGVEKOSTSAEABLOXMGTTWIMPNE");
n_log(LOG_DEBUG, "key= %s", _nstr(key));
N_STR* B64_EXAMPLE_TEXT = n_base64_encode(EXAMPLE_TEXT);
N_STR* encoded_data = n_vigenere_encode(B64_EXAMPLE_TEXT, key);
n_log(LOG_DEBUG, "encoded data=\n%s", _nstr(encoded_data));
N_STR* decoded_data = n_vigenere_decode(encoded_data, key);
N_STR* unbase64 = n_base64_decode(decoded_data);
n_log(LOG_DEBUG, "decoded data=\n%s", _nstr(unbase64));
free_nstr(&unbase64);
/* test n_vigenere_quick_encode / n_vigenere_quick_decode */
N_STR* quick_src = char_to_nstr("quick encode test");
N_STR* quick_enc = n_vigenere_quick_encode(quick_src);
n_log(LOG_DEBUG, "n_vigenere_quick_encode: %s", _nstr(quick_enc));
N_STR* quick_dec = n_vigenere_quick_decode(quick_enc);
n_log(LOG_DEBUG, "n_vigenere_quick_decode: %s", _nstr(quick_dec));
free_nstr(&quick_src);
free_nstr(&quick_enc);
free_nstr(&quick_dec);
N_STR* encoded_question = n_vigenere_get_question(128);
N_STR* decoded_question = n_vigenere_quick_decode(encoded_question);
n_log(LOG_DEBUG, "encoded_question: %s", _nstr(encoded_question));
n_log(LOG_DEBUG, "decoded_question: %s", _nstr(decoded_question));
free_nstr(&decoded_question);
N_STR* answer = n_vigenere_get_answer(key, encoded_question);
n_log(LOG_DEBUG, "answer: %s", _nstr(answer));
N_STR* decode_qa = n_vigenere_decode_qa(encoded_data, encoded_question, answer);
n_log(LOG_DEBUG, "encode root decode qa:\n%s", _nstr(decode_qa));
free_nstr(&decode_qa);
N_STR* encode_qa = n_vigenere_encode_qa(EXAMPLE_TEXT, encoded_question, answer);
n_log(LOG_DEBUG, "encode qa:\n%s", _nstr(encode_qa));
decode_qa = n_vigenere_decode_qa(encode_qa, encoded_question, answer);
n_log(LOG_DEBUG, "encode qa decode qa:\n%s", _nstr(decode_qa));
free_nstr(&decode_qa);
N_STR* decode_qa_root = n_vigenere_decode(encode_qa, key);
unbase64 = n_base64_decode(decode_qa_root);
n_log(LOG_DEBUG, "encode qa - decode root:\n%s", _nstr(unbase64));
free_nstr(&decode_qa_root);
free_nstr(&encode_qa);
if (input_name) {
N_STR* output_name_enc = NULL;
N_STR* output_name_dec = NULL;
N_STR* output_name_dec_question = NULL;
nstrprintf(output_name_enc, "%s.crypt_encoded", _nstr(input_name));
if (n_vigenere_encode_file(input_name, output_name_enc, key)) {
n_log(LOG_DEBUG, "Encoded file saved in %s", _nstr(output_name_enc));
nstrprintf(output_name_dec, "%s.crypt_decoded", _nstr(input_name));
if (n_vigenere_decode_file(output_name_enc, output_name_dec, key)) {
n_log(LOG_DEBUG, "Decoded file saved in %s", _nstr(output_name_dec));
nstrprintf(output_name_dec_question, "%s.crypt_decoded_question", _nstr(input_name));
if (n_vigenere_decode_file_qa(output_name_enc, output_name_dec_question, encoded_question, answer)) {
n_log(LOG_DEBUG, "Decoded with question/answer file saved in %s", _nstr(output_name_dec_question));
} else {
n_log(LOG_ERR, "unable to decode with question answer");
ret = 1;
}
} else {
n_log(LOG_ERR, "unable to decode with root key");
ret = 1;
}
} else {
n_log(LOG_ERR, "unable to encode with root key");
ret = 1;
}
free_nstr(&input_name);
free_nstr(&output_name_enc);
free_nstr(&output_name_dec);
free_nstr(&output_name_dec_question);
}
free_nstr(&encoded_data);
free_nstr(&decoded_data);
free_nstr(&encoded_question);
free_nstr(&answer);
free_nstr(&EXAMPLE_TEXT);
free_nstr(&B64_EXAMPLE_TEXT);
free_nstr(&unbase64);
exit(ret);
} /* END_OF_MAIN */
static void usage(void)
int main(void)
int getoptret
Definition ex_fluid.c:60
int log_level
Definition ex_fluid.c:61
char * key
#define _nstr(__PTR)
N_STR or "NULL" string for logging purposes.
Definition n_common.h:198
N_STR * n_base64_decode(N_STR *bufcoded)
decode a N_STR *string
Definition n_base64.c:187
N_STR * n_base64_encode(N_STR *input)
encode a N_STR *string
Definition n_base64.c:269
N_STR * n_vigenere_encode_qa(N_STR *input_data, N_STR *question, N_STR *answer)
directly vigenere encode a file using key
Definition n_crypto.c:704
int n_vigenere_decode_file_qa(N_STR *in, N_STR *out, N_STR *question, N_STR *answer)
directly vigenere decode a file using question and answer
Definition n_crypto.c:789
N_STR * n_vigenere_quick_encode(N_STR *decoded_data)
quick encode data
Definition n_crypto.c:484
N_STR * n_vigenere_decode(N_STR *string, N_STR *key)
decode input using vigenere cypher and key
Definition n_crypto.c:455
N_STR * n_vigenere_get_answer(N_STR *root_key, N_STR *question)
get an answer from a root key and a question
Definition n_crypto.c:577
N_STR * n_vigenere_decode_qa(N_STR *input_data, N_STR *question, N_STR *answer)
directly vigenere decode a file using key
Definition n_crypto.c:746
N_STR * n_vigenere_encode(N_STR *string, N_STR *key)
encode input using vigenere cypher and key
Definition n_crypto.c:445
int n_vigenere_encode_file(N_STR *in, N_STR *out, N_STR *key)
directly vigenere encode a file using key
Definition n_crypto.c:617
int n_vigenere_decode_file(N_STR *in, N_STR *out, N_STR *key)
directly vigenere decode a file using key
Definition n_crypto.c:661
N_STR * n_vigenere_get_rootkey(size_t rootkey_size)
get a rootkey randomly generated
Definition n_crypto.c:68
N_STR * n_vigenere_get_question(size_t question_size)
get a question generated from the current machine hardware (disk&cpu)
Definition n_crypto.c:516
N_STR * n_vigenere_quick_decode(N_STR *encoded_data)
quick decode data
Definition n_crypto.c:499
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:88
#define LOG_DEBUG
debug-level messages
Definition n_log.h:83
#define LOG_ERR
error conditions
Definition n_log.h:75
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:120
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:79
#define LOG_NULL
no log output
Definition n_log.h:45
#define LOG_INFO
informational
Definition n_log.h:81
size_t written
size of the written data inside the string
Definition n_str.h:66
size_t length
length of string (in case we wanna keep information after the 0 end of string value)
Definition n_str.h:64
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:201
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
Definition n_str.c:254
#define nstrprintf(__nstr_var, __format,...)
Macro to quickly allocate and sprintf to N_STR.
Definition n_str.h:115
A box including a string and his lenght.
Definition n_str.h:60
Base64 encoding and decoding functions using N_STR.
Vigenere encoding and decoding functions using N_STR/files.
List structures and definitions.
Generic log system.
N_STR and string function declaration.