Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_crypto.c
Go to the documentation of this file.
1
9#include "nilorea/n_log.h"
10#include "nilorea/n_list.h"
11#include "nilorea/n_str.h"
12#include "nilorea/n_base64.h"
13#include "nilorea/n_crypto.h"
14
15int main(int argc, char* argv[]) {
16 void usage(void) {
17 fprintf(stderr,
18 "%s usage:\n"
19 " -i input_file [optional] use a file as datas instead of hard coded 3 lines text\n"
20 " -v version\n"
21 " -V log level: LOG_INFO, LOG_NOTICE, LOG_ERR, LOG_DEBUG\n"
22 " -h help\n",
23 argv[0]);
24 }
25
26 int getoptret = 0, ret = 0,
27 log_level = LOG_DEBUG; /* default log level */
28
29 N_STR* input_name = NULL;
30
31 /* Arguments optionnels */
32 /* -v version
33 * -V log level
34 * -h help
35 */
36 while ((getoptret = getopt(argc, argv, "i:hvV:")) != EOF) {
37 switch (getoptret) {
38 case 'i':
39 input_name = char_to_nstr(optarg);
40 break;
41 case 'v':
42 fprintf(stderr, "Date de compilation : %s a %s.\n", __DATE__, __TIME__);
43 exit(1);
44 case 'V':
45 if (!strncmp("LOG_NULL", optarg, 5)) {
47 } else {
48 if (!strncmp("LOG_NOTICE", optarg, 6)) {
50 } else {
51 if (!strncmp("LOG_INFO", optarg, 7)) {
53 } else {
54 if (!strncmp("LOG_ERR", optarg, 5)) {
56 } else {
57 if (!strncmp("LOG_DEBUG", optarg, 5)) {
59 } else {
60 fprintf(stderr, "%s n'est pas un niveau de log valide.\n", optarg);
61 exit(-1);
62 }
63 }
64 }
65 }
66 }
67 break;
68 default:
69 case '?': {
70 if (optopt == 'V') {
71 fprintf(stderr, " => Missing log level\n");
72 } else if (optopt == 'p') {
73 fprintf(stderr, " => Missing port\n");
74 } else if (optopt == 'i') {
75 fprintf(stderr, " => Missing filename\n");
76 } else if (optopt != 's') {
77 fprintf(stderr, " => Unknow missing option %c\n", optopt);
78 }
79 usage();
80 exit(1);
81 }
82 case 'h': {
83 usage();
84 exit(1);
85 }
86 }
87 }
89
90 N_STR* EXAMPLE_TEXT = char_to_nstr(
91 "##############################################################\n"
92 "# This is an example of crypto encode/decode, with a newline #\n"
93 "# This is the end of the test #\n"
94 "##############################################################");
95
96 n_log(LOG_NOTICE, "Testing crypto, encoding text of size (%ld/%ld):\n%s", EXAMPLE_TEXT->written, EXAMPLE_TEXT->length, _nstr(EXAMPLE_TEXT));
97 /* 10 20 30 40 50 60 */
98 /* "1234567890123456789012345678901234567890123456789012345678901234" */
99 // N_STR *key = n_vigenere_get_rootkey( 128 );
100 N_STR* key = char_to_nstr("RKCHSLZWFNASULJFVPRVUUUAEUVEMSSGEVNWIMVPZVIWITBGIUBEQVEURBYEWTMCQZZYNWQMRAIBTMHDIKIZHOVZQUFONRQDSRDFNTTGVEKOSTSAEABLOXMGTTWIMPNE");
101 n_log(LOG_DEBUG, "key= %s", _nstr(key));
102
103 N_STR* B64_EXAMPLE_TEXT = n_base64_encode(EXAMPLE_TEXT);
104 N_STR* encoded_data = n_vigenere_encode(B64_EXAMPLE_TEXT, key);
105 n_log(LOG_DEBUG, "encoded data=\n%s", _nstr(encoded_data));
106
107 N_STR* decoded_data = n_vigenere_decode(encoded_data, key);
108 N_STR* unbase64 = n_base64_decode(decoded_data);
109 n_log(LOG_DEBUG, "decoded data=\n%s", _nstr(unbase64));
110 free_nstr(&unbase64);
111
112 N_STR* encoded_question = n_vigenere_get_question(128);
113 N_STR* decoded_question = n_vigenere_quick_decode(encoded_question);
114 n_log(LOG_DEBUG, "encoded_question: %s", _nstr(encoded_question));
115 n_log(LOG_DEBUG, "decoded_question: %s", _nstr(decoded_question));
116 free_nstr(&decoded_question);
117
118 N_STR* answer = n_vigenere_get_answer(key, encoded_question);
119 n_log(LOG_DEBUG, "answer: %s", _nstr(answer));
120
121 N_STR* decode_qa = n_vigenere_decode_qa(encoded_data, encoded_question, answer);
122 n_log(LOG_DEBUG, "encode root decode qa:\n%s", _nstr(decode_qa));
123 free_nstr(&decode_qa);
124
125 N_STR* encode_qa = n_vigenere_encode_qa(EXAMPLE_TEXT, encoded_question, answer);
126 n_log(LOG_DEBUG, "encode qa:\n%s", _nstr(encode_qa));
127
128 decode_qa = n_vigenere_decode_qa(encode_qa, encoded_question, answer);
129 n_log(LOG_DEBUG, "encode qa decode qa:\n%s", _nstr(decode_qa));
130 free_nstr(&decode_qa);
131
132 N_STR* decode_qa_root = n_vigenere_decode(encode_qa, key);
133 unbase64 = n_base64_decode(decode_qa_root);
134 n_log(LOG_DEBUG, "encode qa - decode root:\n%s", _nstr(unbase64));
135
136 free_nstr(&decode_qa_root);
137 free_nstr(&encode_qa);
138
139 if (input_name) {
140 N_STR* output_name_enc = NULL;
141 N_STR* output_name_dec = NULL;
142 N_STR* output_name_dec_question = NULL;
143
144 nstrprintf(output_name_enc, "%s.crypt_encoded", _nstr(input_name));
145 if (n_vigenere_encode_file(input_name, output_name_enc, key)) {
146 n_log(LOG_DEBUG, "Encoded file saved in %s", _nstr(output_name_enc));
147
148 nstrprintf(output_name_dec, "%s.crypt_decoded", _nstr(input_name));
149 if (n_vigenere_decode_file(output_name_enc, output_name_dec, key)) {
150 n_log(LOG_DEBUG, "Decoded file saved in %s", _nstr(output_name_dec));
151
152 nstrprintf(output_name_dec_question, "%s.crypt_decoded_question", _nstr(input_name));
153 if (n_vigenere_decode_file_qa(output_name_enc, output_name_dec_question, encoded_question, answer)) {
154 n_log(LOG_DEBUG, "Decoded with question/answer file saved in %s", _nstr(output_name_dec_question));
155 } else {
156 n_log(LOG_ERR, "unable to decode with question answer");
157 ret = 1;
158 }
159 } else {
160 n_log(LOG_ERR, "unable to decode with root key");
161 ret = 1;
162 }
163 } else {
164 n_log(LOG_ERR, "unable to encode with root key");
165 ret = 1;
166 }
167
168 free_nstr(&input_name);
169 free_nstr(&output_name_enc);
170 free_nstr(&output_name_dec);
171 free_nstr(&output_name_dec_question);
172 }
173
174 free_nstr(&key);
175 free_nstr(&encoded_data);
176 free_nstr(&decoded_data);
177 free_nstr(&encoded_question);
178 free_nstr(&answer);
179 free_nstr(&EXAMPLE_TEXT);
180 free_nstr(&B64_EXAMPLE_TEXT);
181 free_nstr(&unbase64);
182
183 exit(ret);
184} /* END_OF_MAIN */
void usage(void)
Definition ex_common.c:22
int main(void)
int getoptret
Definition ex_fluid.c:42
int log_level
Definition ex_fluid.c:43
char * key
#define _nstr(__PTR)
N_STR or "NULL" string for logging purposes.
Definition n_common.h:180
N_STR * n_base64_decode(N_STR *bufcoded)
decode a N_STR *string
Definition n_base64.c:169
N_STR * n_base64_encode(N_STR *input)
encode a N_STR *string
Definition n_base64.c:251
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:656
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:734
N_STR * n_vigenere_decode(N_STR *string, N_STR *key)
decode input using vigenere cypher and key
Definition n_crypto.c:417
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:535
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:691
N_STR * n_vigenere_encode(N_STR *string, N_STR *key)
encode input using vigenere cypher and key
Definition n_crypto.c:407
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:571
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:614
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:478
N_STR * n_vigenere_quick_decode(N_STR *encoded_data)
quick decode data
Definition n_crypto.c:461
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:70
#define LOG_DEBUG
debug-level messages
Definition n_log.h:65
#define LOG_ERR
error conditions
Definition n_log.h:57
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:104
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:61
#define LOG_NULL
no log output
Definition n_log.h:27
#define LOG_INFO
informational
Definition n_log.h:63
size_t written
size of the written data inside the string
Definition n_str.h:45
size_t length
length of string (in case we wanna keep information after the 0 end of string value)
Definition n_str.h:43
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:176
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
Definition n_str.c:228
#define nstrprintf(__nstr_var, __format,...)
Macro to quickly allocate and sprintf to N_STR.
Definition n_str.h:94
A box including a string and his lenght.
Definition n_str.h:39
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.