Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_crypto.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
28#include "nilorea/n_log.h"
29#include "nilorea/n_list.h"
30#include "nilorea/n_str.h"
31#include "nilorea/n_base64.h"
32#include "nilorea/n_crypto.h"
33
34int main(int argc, char* argv[]) {
35 void usage(void) {
36 fprintf(stderr,
37 "%s usage:\n"
38 " -i input_file [optional] use a file as datas instead of hard coded 3 lines text\n"
39 " -v version\n"
40 " -V log level: LOG_INFO, LOG_NOTICE, LOG_ERR, LOG_DEBUG\n"
41 " -h help\n",
42 argv[0]);
43 }
44
45 int getoptret = 0, ret = 0,
46 log_level = LOG_DEBUG; /* default log level */
47
48 N_STR* input_name = NULL;
49
50 /* Arguments optionnels */
51 /* -v version
52 * -V log level
53 * -h help
54 */
55 while ((getoptret = getopt(argc, argv, "i:hvV:")) != EOF) {
56 switch (getoptret) {
57 case 'i':
58 input_name = char_to_nstr(optarg);
59 break;
60 case 'v':
61 fprintf(stderr, "Date de compilation : %s a %s.\n", __DATE__, __TIME__);
62 exit(1);
63 case 'V':
64 if (!strcmp("LOG_NULL", optarg))
66 else if (!strcmp("LOG_NOTICE", optarg))
68 else if (!strcmp("LOG_INFO", optarg))
70 else if (!strcmp("LOG_ERR", optarg))
72 else if (!strcmp("LOG_DEBUG", optarg))
74 else {
75 fprintf(stderr, "%s n'est pas un niveau de log valide.\n", optarg);
76 exit(-1);
77 }
78 break;
79 default:
80 case '?': {
81 if (optopt == 'V') {
82 fprintf(stderr, " => Missing log level\n");
83 } else if (optopt == 'p') {
84 fprintf(stderr, " => Missing port\n");
85 } else if (optopt == 'i') {
86 fprintf(stderr, " => Missing filename\n");
87 } else if (optopt != 's') {
88 fprintf(stderr, " => Unknow missing option %c\n", optopt);
89 }
90 usage();
91 exit(1);
92 }
93 case 'h': {
94 usage();
95 exit(1);
96 }
97 }
98 }
100
101 N_STR* EXAMPLE_TEXT = char_to_nstr(
102 "##############################################################\n"
103 "# This is an example of crypto encode/decode, with a newline #\n"
104 "# This is the end of the test #\n"
105 "##############################################################");
106
107 n_log(LOG_NOTICE, "Testing crypto, encoding text of size (%ld/%ld):\n%s", EXAMPLE_TEXT->written, EXAMPLE_TEXT->length, _nstr(EXAMPLE_TEXT));
108 /* 10 20 30 40 50 60 */
109 /* "1234567890123456789012345678901234567890123456789012345678901234" */
110 /* test n_vigenere_get_rootkey */
111 N_STR* rootkey = n_vigenere_get_rootkey(128);
112 n_log(LOG_DEBUG, "n_vigenere_get_rootkey(128): %s", _nstr(rootkey));
113 free_nstr(&rootkey);
114
115 N_STR* key = char_to_nstr("RKCHSLZWFNASULJFVPRVUUUAEUVEMSSGEVNWIMVPZVIWITBGIUBEQVEURBYEWTMCQZZYNWQMRAIBTMHDIKIZHOVZQUFONRQDSRDFNTTGVEKOSTSAEABLOXMGTTWIMPNE");
116 n_log(LOG_DEBUG, "key= %s", _nstr(key));
117
118 N_STR* B64_EXAMPLE_TEXT = n_base64_encode(EXAMPLE_TEXT);
119 N_STR* encoded_data = n_vigenere_encode(B64_EXAMPLE_TEXT, key);
120 n_log(LOG_DEBUG, "encoded data=\n%s", _nstr(encoded_data));
121
122 N_STR* decoded_data = n_vigenere_decode(encoded_data, key);
123 N_STR* unbase64 = n_base64_decode(decoded_data);
124 n_log(LOG_DEBUG, "decoded data=\n%s", _nstr(unbase64));
125 free_nstr(&unbase64);
126
127 /* test n_vigenere_quick_encode / n_vigenere_quick_decode */
128 N_STR* quick_src = char_to_nstr("quick encode test");
129 N_STR* quick_enc = n_vigenere_quick_encode(quick_src);
130 n_log(LOG_DEBUG, "n_vigenere_quick_encode: %s", _nstr(quick_enc));
131 N_STR* quick_dec = n_vigenere_quick_decode(quick_enc);
132 n_log(LOG_DEBUG, "n_vigenere_quick_decode: %s", _nstr(quick_dec));
133 free_nstr(&quick_src);
134 free_nstr(&quick_enc);
135 free_nstr(&quick_dec);
136
137 N_STR* encoded_question = n_vigenere_get_question(128);
138 N_STR* decoded_question = n_vigenere_quick_decode(encoded_question);
139 n_log(LOG_DEBUG, "encoded_question: %s", _nstr(encoded_question));
140 n_log(LOG_DEBUG, "decoded_question: %s", _nstr(decoded_question));
141 free_nstr(&decoded_question);
142
143 N_STR* answer = n_vigenere_get_answer(key, encoded_question);
144 n_log(LOG_DEBUG, "answer: %s", _nstr(answer));
145
146 N_STR* decode_qa = n_vigenere_decode_qa(encoded_data, encoded_question, answer);
147 n_log(LOG_DEBUG, "encode root decode qa:\n%s", _nstr(decode_qa));
148 free_nstr(&decode_qa);
149
150 N_STR* encode_qa = n_vigenere_encode_qa(EXAMPLE_TEXT, encoded_question, answer);
151 n_log(LOG_DEBUG, "encode qa:\n%s", _nstr(encode_qa));
152
153 decode_qa = n_vigenere_decode_qa(encode_qa, encoded_question, answer);
154 n_log(LOG_DEBUG, "encode qa decode qa:\n%s", _nstr(decode_qa));
155 free_nstr(&decode_qa);
156
157 N_STR* decode_qa_root = n_vigenere_decode(encode_qa, key);
158 unbase64 = n_base64_decode(decode_qa_root);
159 n_log(LOG_DEBUG, "encode qa - decode root:\n%s", _nstr(unbase64));
160
161 free_nstr(&decode_qa_root);
162 free_nstr(&encode_qa);
163
164 if (input_name) {
165 N_STR* output_name_enc = NULL;
166 N_STR* output_name_dec = NULL;
167 N_STR* output_name_dec_question = NULL;
168
169 nstrprintf(output_name_enc, "%s.crypt_encoded", _nstr(input_name));
170 if (n_vigenere_encode_file(input_name, output_name_enc, key)) {
171 n_log(LOG_DEBUG, "Encoded file saved in %s", _nstr(output_name_enc));
172
173 nstrprintf(output_name_dec, "%s.crypt_decoded", _nstr(input_name));
174 if (n_vigenere_decode_file(output_name_enc, output_name_dec, key)) {
175 n_log(LOG_DEBUG, "Decoded file saved in %s", _nstr(output_name_dec));
176
177 nstrprintf(output_name_dec_question, "%s.crypt_decoded_question", _nstr(input_name));
178 if (n_vigenere_decode_file_qa(output_name_enc, output_name_dec_question, encoded_question, answer)) {
179 n_log(LOG_DEBUG, "Decoded with question/answer file saved in %s", _nstr(output_name_dec_question));
180 } else {
181 n_log(LOG_ERR, "unable to decode with question answer");
182 ret = 1;
183 }
184 } else {
185 n_log(LOG_ERR, "unable to decode with root key");
186 ret = 1;
187 }
188 } else {
189 n_log(LOG_ERR, "unable to encode with root key");
190 ret = 1;
191 }
192
193 free_nstr(&input_name);
194 free_nstr(&output_name_enc);
195 free_nstr(&output_name_dec);
196 free_nstr(&output_name_dec_question);
197 }
198
199 free_nstr(&key);
200 free_nstr(&encoded_data);
201 free_nstr(&decoded_data);
202 free_nstr(&encoded_question);
203 free_nstr(&answer);
204 free_nstr(&EXAMPLE_TEXT);
205 free_nstr(&B64_EXAMPLE_TEXT);
206 free_nstr(&unbase64);
207
208 exit(ret);
209} /* END_OF_MAIN */
static void usage(void)
int main(void)
int getoptret
Definition ex_fluid.c:59
int log_level
Definition ex_fluid.c:60
char * key
#define _nstr(__PTR)
N_STR or "NULL" string for logging purposes.
Definition n_common.h:199
N_STR * n_base64_decode(N_STR *bufcoded)
decode a N_STR *string
Definition n_base64.c:188
N_STR * n_base64_encode(N_STR *input)
encode a N_STR *string
Definition n_base64.c:270
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:705
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:790
N_STR * n_vigenere_quick_encode(N_STR *decoded_data)
quick encode data
Definition n_crypto.c:485
N_STR * n_vigenere_decode(N_STR *string, N_STR *key)
decode input using vigenere cypher and key
Definition n_crypto.c:456
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:578
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:747
N_STR * n_vigenere_encode(N_STR *string, N_STR *key)
encode input using vigenere cypher and key
Definition n_crypto.c:446
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:618
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:662
N_STR * n_vigenere_get_rootkey(size_t rootkey_size)
get a rootkey randomly generated
Definition n_crypto.c:69
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:517
N_STR * n_vigenere_quick_decode(N_STR *encoded_data)
quick decode data
Definition n_crypto.c:500
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:89
#define LOG_DEBUG
debug-level messages
Definition n_log.h:84
#define LOG_ERR
error conditions
Definition n_log.h:76
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:121
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:80
#define LOG_NULL
no log output
Definition n_log.h:46
#define LOG_INFO
informational
Definition n_log.h:82
size_t written
number of meaningful bytes in data, excluding the null terminator; the size including the null termin...
Definition n_str.h:68
size_t length
total allocation (in bytes) of the data buffer, padding included
Definition n_str.h:65
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:203
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
Definition n_str.c:255
#define nstrprintf(__nstr_var, __format,...)
Macro to quickly allocate and sprintf to N_STR.
Definition n_str.h:117
A box including a string and his lenght.
Definition n_str.h:61
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.