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