Orbiter 2022
Combinatorial Objects
interface_cryptography.cpp
Go to the documentation of this file.
1// interface_cryptography.cpp
2//
3// by Anton Betten
4//
5// Colorado State University
6//
7// for M360 mathematics of information security, Fall 03
8//
9
10#include "orbiter.h"
11
12using namespace std;
13
14
15namespace orbiter {
16namespace layer5_applications {
17namespace user_interface {
18
19
21{
22 //cout << "interface_cryptography::interface_cryptography" << endl;
23 //cout << "sizeof(interface_cryptography)=" << sizeof(interface_cryptography) << endl;
24
25 f_cipher = FALSE;
27 //cout << "interface_cryptography::interface_cryptography 0" << endl;
28
29 f_decipher = FALSE;
30 //cout << "interface_cryptography::interface_cryptography 00a" << endl;
31
32 f_analyze = FALSE;
33 //cout << "interface_cryptography::interface_cryptography 00b" << endl;
34
35 f_kasiski = FALSE;
36 f_avk = FALSE;
37 key_length = 0;
38 threshold = 0;
39 //cout << "interface_cryptography::interface_cryptography 00c" << endl;
40 affine_a = 0;
41 //cout << "interface_cryptography::interface_cryptography 00d" << endl;
42 affine_b = 0;
43 //std::string ptext;
44 //std::string ctext;
45 //std::string guess;
46 //std::string key;
47 //cout << "interface_cryptography::interface_cryptography 00e" << endl;
48
49 f_RSA = FALSE;
50 RSA_d = 0;
51 RSA_m = 0;
52 //RSA_text = NULL;
53 //cout << "interface_cryptography::interface_cryptography 00f" << endl;
54
55 f_primitive_root = FALSE;
56 //std::string primitive_root_p;
57
58 f_smallest_primitive_root = FALSE;
59 smallest_primitive_root_p = 0;
60
61 f_smallest_primitive_root_interval = FALSE;
62 smallest_primitive_root_interval_min = 0;
63 smallest_primitive_root_interval_max = 0;
64
65 f_number_of_primitive_roots_interval = FALSE;
66
67 f_inverse_mod = FALSE;
68 inverse_mod_a = 0;
69 inverse_mod_n = 0;
70 //cout << "interface_cryptography::interface_cryptography 00g" << endl;
71
72 f_extended_gcd = FALSE;
73 extended_gcd_a = 0;
74 extended_gcd_b = 0;
75
76 f_power_mod = FALSE;
77 //std::string power_mod_a;
78 //std::string power_mod_k;
79 //std::string power_mod_n;
80 //cout << "interface_cryptography::interface_cryptography 00h" << endl;
81
82 f_discrete_log = FALSE;
83 //cout << "interface_cryptography::interface_cryptography 0b" << endl;
84 discrete_log_y = 0;
85 discrete_log_a = 0;
86 discrete_log_m = 0;
87
88 f_RSA_setup = FALSE;
89 RSA_setup_nb_bits = 0;
90 RSA_setup_nb_tests_solovay_strassen = 0;
91 RSA_setup_f_miller_rabin_test = 0;
92
93 f_RSA_encrypt_text = FALSE;
94 RSA_block_size = 0;
95 //RSA_encrypt_text = NULL;
96
97 f_sift_smooth = FALSE;
98 sift_smooth_from = 0;
99 sift_smooth_len = 0;
100 //cout << "interface_cryptography::interface_cryptography 1" << endl;
101 //sift_smooth_factor_base = NULL;
102
103 f_square_root = FALSE;
104 //square_root_number = NULL;
105
106 f_square_root_mod = FALSE;
107 //square_root_mod_a = NULL;
108 //square_root_mod_m = NULL;
109 //cout << "interface_cryptography::interface_cryptography 1a" << endl;
110
111 f_all_square_roots_mod_n = FALSE;
112 //std::string f_all_square_roots_mod_n_a;
113 //std::string f_all_square_roots_mod_n_n;
114
115 f_quadratic_sieve = FALSE;
116 quadratic_sieve_n = 0;
117 quadratic_sieve_factorbase = 0;
118 quadratic_sieve_x0 = 0;
119 //cout << "interface_cryptography::interface_cryptography 1b" << endl;
120
121 f_jacobi = FALSE;
122 jacobi_top = 0;
123 jacobi_bottom = 0;
124 //cout << "interface_cryptography::interface_cryptography 1c" << endl;
125
126 f_solovay_strassen = FALSE;
127 solovay_strassen_p = 0;
128 solovay_strassen_a = 0;
129 //cout << "interface_cryptography::interface_cryptography 1d" << endl;
130
131 f_miller_rabin = FALSE;
132 miller_rabin_p = 0;
133 miller_rabin_nb_times = 0;
134 //cout << "interface_cryptography::interface_cryptography 1e" << endl;
135
136 f_fermat_test = FALSE;
137 fermat_test_p = 0;
138 fermat_test_nb_times = 0;
139 //cout << "interface_cryptography::interface_cryptography 1f" << endl;
140
141 f_find_pseudoprime = FALSE;
142 find_pseudoprime_nb_digits = 0;
143 find_pseudoprime_nb_fermat = 0;
144 find_pseudoprime_nb_miller_rabin = 0;
145 find_pseudoprime_nb_solovay_strassen = 0;
146 //cout << "interface_cryptography::interface_cryptography 1g" << endl;
147
148 f_find_strong_pseudoprime = FALSE;
149
150 f_miller_rabin_text = FALSE;
151 miller_rabin_text_nb_times = 0;
152 //miller_rabin_number_text = NULL;
153 //cout << "interface_cryptography::interface_cryptography 1h" << endl;
154
155 f_random = FALSE;
156 random_nb = 0;
157 //random_fname_csv = NULL;
158
159 f_random_last = FALSE;
160 random_last_nb = 0;
161 //cout << "interface_cryptography::interface_cryptography 1i" << endl;
162
163 f_affine_sequence = FALSE;
164 affine_sequence_a = 0;
165 //cout << "interface_cryptography::interface_cryptography 2" << endl;
166 affine_sequence_c = 0;
167 affine_sequence_m = 0;
168
169}
170
171
172
173
174
175
176void interface_cryptography::print_help(int argc, std::string *argv, int i, int verbose_level)
177{
179
180 if (ST.stringcmp(argv[i], "-cipher_substitution") == 0) {
181 cout << "-cipher_substitution <ptext>" << endl;
182 }
183 else if (ST.stringcmp(argv[i], "-cipher_vigenere") == 0) {
184 cout << "-cipher_vigenere <ptext> <key>" << endl;
185 }
186 else if (ST.stringcmp(argv[i], "-cipher_affine") == 0) {
187 cout << "-cipher_affine <ptext> <int : a> <int : b>" << endl;
188 }
189 else if (ST.stringcmp(argv[i], "-analyze_substitution") == 0) {
190 cout << "-analyze_substitution <ctext>" << endl;
191 }
192 else if (ST.stringcmp(argv[i], "-analyze_vigenere") == 0) {
193 cout << "-analyze_vigenere <ctext>" << endl;
194 }
195 else if (ST.stringcmp(argv[i], "-analyze_vigenere_kasiski") == 0) {
196 cout << "-analyze_vigenere_kasiski <ctext> <int : key_length>" << endl;
197 }
198 else if (ST.stringcmp(argv[i], "-kasiski") == 0) {
199 cout << "-kasiski <ctext> <int : threshold>" << endl;
200 }
201 else if (ST.stringcmp(argv[i], "-decipher_substitution") == 0) {
202 cout << "-decipher_substitution <ctext> <guess>" << endl;
203 }
204 else if (ST.stringcmp(argv[i], "-decipher_vigenere") == 0) {
205 cout << "-decipher_vigenere <ctext> <key>" << endl;
206 }
207 else if (ST.stringcmp(argv[i], "-decipher_affine") == 0) {
208 cout << "-decipher_affine <ctext> <guess>" << endl;
209 }
210 else if (ST.stringcmp(argv[i], "-RSA") == 0) {
211 cout << "-RSA <int : d> <int : m> <text>" << endl;
212 }
213 else if (ST.stringcmp(argv[i], "-RSA_encrypt_text") == 0) {
214 cout << "-RSA_encrypt_text <int : d> <int : m> <int : block_size> <text>" << endl;
215 }
216 else if (ST.stringcmp(argv[i], "-RSA_setup") == 0) {
217 cout << "-RSA_setup <int : nb_bits> <int : nb_tests_solovay_strassen> <int : f_miller_rabin_test>" << endl;
218 }
219 else if (ST.stringcmp(argv[i], "-primitive_root") == 0) {
220 cout << "-primitive_root <int : p>" << endl;
221 }
222 else if (ST.stringcmp(argv[i], "-smallest_primitive_root") == 0) {
223 cout << "-smallest_primitive_root <int : p>" << endl;
224 }
225 else if (ST.stringcmp(argv[i], "-smallest_primitive_root_interval") == 0) {
226 cout << "-smallest_primitive_root_interval <int : p_min> <int : p_max>" << endl;
227 }
228 else if (ST.stringcmp(argv[i], "-number_of_primitive_roots_interval") == 0) {
229 cout << "-number_of_primitive_roots_interval <int : p_min> <int : p_max>" << endl;
230 }
231 else if (ST.stringcmp(argv[i], "-inverse_mod") == 0) {
232 cout << "-primitive_root <int : a> <int : n>" << endl;
233 }
234 else if (ST.stringcmp(argv[i], "-extended_gcd") == 0) {
235 cout << "-extended_gcd <int : a> <int : b>" << endl;
236 }
237 else if (ST.stringcmp(argv[i], "-power_mod") == 0) {
238 cout << "-power_mod <int : a> <int : k> <int : n>" << endl;
239 }
240 else if (ST.stringcmp(argv[i], "-discrete_log") == 0) {
241 cout << "-discrete_log <int : y> <int : a> <int : m>" << endl;
242 }
243 else if (ST.stringcmp(argv[i], "-sift_smooth") == 0) {
244 cout << "-sift_smooth <int : from> <int : ken> <string : factor_base>" << endl;
245 }
246 else if (ST.stringcmp(argv[i], "-square_root") == 0) {
247 cout << "-square_root <int : number>" << endl;
248 }
249 else if (ST.stringcmp(argv[i], "-square_root_mod") == 0) {
250 cout << "-square_root_mod <int : a> <int : m>" << endl;
251 }
252 else if (ST.stringcmp(argv[i], "-all_square_roots_mod_n") == 0) {
253 cout << "-all_square_roots_mod_n <int : a> <int : n>" << endl;
254 }
255 else if (ST.stringcmp(argv[i], "-quadratic_sieve") == 0) {
256 cout << "-quadratic_sieve <int : n> <string : factor_base> <int : x0>" << endl;
257 }
258 else if (ST.stringcmp(argv[i], "-jacobi") == 0) {
259 cout << "-jacobi <int : top> <int : bottom>" << endl;
260 }
261 else if (ST.stringcmp(argv[i], "-solovay_strassen") == 0) {
262 cout << "-solovay_strassen <int : a> <int : p>" << endl;
263 }
264 else if (ST.stringcmp(argv[i], "-miller_rabin") == 0) {
265 cout << "-miller_rabin <int : p> <int : nb_times>" << endl;
266 }
267 else if (ST.stringcmp(argv[i], "-fermat_test") == 0) {
268 cout << "-fermat_test <int : p> <int : nb_times>" << endl;
269 }
270 else if (ST.stringcmp(argv[i], "-find_pseudoprime") == 0) {
271 cout << "-find_pseudoprime <int : nb_digits> <int : nb_fermat> <int : nb_miller_rabin> <int : nb_solovay_strassen>" << endl;
272 }
273 else if (ST.stringcmp(argv[i], "-find_strong_pseudoprime") == 0) {
274 cout << "-find_strong_pseudoprime <int : nb_digits> <int : nb_fermat> <int : nb_miller_rabin>" << endl;
275 }
276 else if (ST.stringcmp(argv[i], "-miller_rabin_text") == 0) {
277 cout << "-fermat_test <int : nb_times> <string : number>" << endl;
278 }
279 else if (ST.stringcmp(argv[i], "-random") == 0) {
280 cout << "-random <int : nb_times> <string : fname_csv>" << endl;
281 }
282 else if (ST.stringcmp(argv[i], "-random_last") == 0) {
283 cout << "-random_last <int : nb_times>" << endl;
284 }
285 else if (ST.stringcmp(argv[i], "-affine_sequence") == 0) {
286 cout << "-affine_sequence <int : a> <int : c> <int : m>" << endl;
287 }
288}
289
290int interface_cryptography::recognize_keyword(int argc, std::string *argv, int i, int verbose_level)
291{
292 int f_v = (verbose_level >= 1);
294
295 if (f_v) {
296 cout << "interface_cryptography::recognize_keyword" << endl;
297 }
298 if (i >= argc) {
299 return false;
300 }
301 if (ST.stringcmp(argv[i], "-cipher_substitution") == 0) {
302 return true;
303 }
304 else if (ST.stringcmp(argv[i], "-cipher_vigenere") == 0) {
305 return true;
306 }
307 else if (ST.stringcmp(argv[i], "-cipher_affine") == 0) {
308 return true;
309 }
310 else if (ST.stringcmp(argv[i], "-analyze_substitution") == 0) {
311 return true;
312 }
313 else if (ST.stringcmp(argv[i], "-analyze_vigenere") == 0) {
314 return true;
315 }
316 else if (ST.stringcmp(argv[i], "-analyze_vigenere_kasiski") == 0) {
317 return true;
318 }
319 else if (ST.stringcmp(argv[i], "-kasiski") == 0) {
320 return true;
321 }
322 else if (ST.stringcmp(argv[i], "-decipher_substitution") == 0) {
323 return true;
324 }
325 else if (ST.stringcmp(argv[i], "-decipher_vigenere") == 0) {
326 return true;
327 }
328 else if (ST.stringcmp(argv[i], "-decipher_affine") == 0) {
329 return true;
330 }
331 else if (ST.stringcmp(argv[i], "-RSA") == 0) {
332 return true;
333 }
334 else if (ST.stringcmp(argv[i], "-RSA_encrypt_text") == 0) {
335 return true;
336 }
337 else if (ST.stringcmp(argv[i], "-RSA_setup") == 0) {
338 return true;
339 }
340 else if (ST.stringcmp(argv[i], "-primitive_root") == 0) {
341 return true;
342 }
343 else if (ST.stringcmp(argv[i], "-smallest_primitive_root") == 0) {
344 return true;
345 }
346 else if (ST.stringcmp(argv[i], "-smallest_primitive_root_interval") == 0) {
347 return true;
348 }
349 else if (ST.stringcmp(argv[i], "-number_of_primitive_roots_interval") == 0) {
350 return true;
351 }
352 else if (ST.stringcmp(argv[i], "-inverse_mod") == 0) {
353 return true;
354 }
355 else if (ST.stringcmp(argv[i], "-extended_gcd") == 0) {
356 return true;
357 }
358 else if (ST.stringcmp(argv[i], "-power_mod") == 0) {
359 return true;
360 }
361 else if (ST.stringcmp(argv[i], "-discrete_log") == 0) {
362 return true;
363 }
364 else if (ST.stringcmp(argv[i], "-sift_smooth") == 0) {
365 return true;
366 }
367 else if (ST.stringcmp(argv[i], "-square_root") == 0) {
368 return true;
369 }
370 else if (ST.stringcmp(argv[i], "-square_root_mod") == 0) {
371 return true;
372 }
373 else if (ST.stringcmp(argv[i], "-all_square_roots_mod_n") == 0) {
374 return true;
375 }
376 else if (ST.stringcmp(argv[i], "-quadratic_sieve") == 0) {
377 return true;
378 }
379 else if (ST.stringcmp(argv[i], "-jacobi") == 0) {
380 return true;
381 }
382 else if (ST.stringcmp(argv[i], "-solovay_strassen") == 0) {
383 return true;
384 }
385 else if (ST.stringcmp(argv[i], "-miller_rabin") == 0) {
386 return true;
387 }
388 else if (ST.stringcmp(argv[i], "-fermat_test") == 0) {
389 return true;
390 }
391 else if (ST.stringcmp(argv[i], "-find_pseudoprime") == 0) {
392 return true;
393 }
394 else if (ST.stringcmp(argv[i], "-find_strong_pseudoprime") == 0) {
395 return true;
396 }
397 else if (ST.stringcmp(argv[i], "-miller_rabin_text") == 0) {
398 return true;
399 }
400 else if (ST.stringcmp(argv[i], "-random") == 0) {
401 return true;
402 }
403 else if (ST.stringcmp(argv[i], "-random_last") == 0) {
404 return true;
405 }
406 else if (ST.stringcmp(argv[i], "-affine_sequence") == 0) {
407 return true;
408 }
409 return false;
410}
411
412void interface_cryptography::read_arguments(int argc, std::string *argv, int &i, int verbose_level)
413{
414 int f_v = (verbose_level >= 1);
416
417 if (f_v) {
418 cout << "interface_cryptography::read_arguments" << endl;
419 }
420
421
422 if (f_v) {
423 cout << "interface_cryptography::read_arguments the next argument is " << argv[i] << endl;
424 }
425
426 if (ST.stringcmp(argv[i], "-cipher_substitution") == 0) {
427 f_cipher = TRUE;
428 t = substitution;
429 ptext.assign(argv[++i]);
430 if (f_v) {
431 cout << "-cipher_substitution " << ptext << endl;
432 }
433 }
434 else if (ST.stringcmp(argv[i], "-cipher_vigenere") == 0) {
435 f_cipher = TRUE;
436 t = vigenere;
437 ptext.assign(argv[++i]);
438 key.assign(argv[++i]);
439 if (f_v) {
440 cout << "-cipher_vigenere " << ptext << endl;
441 }
442 }
443 else if (ST.stringcmp(argv[i], "-cipher_affine") == 0) {
444 f_cipher = TRUE;
445 t = affine;
446 ptext.assign(argv[++i]);
447 affine_a = ST.strtoi(argv[++i]);
448 affine_b = ST.strtoi(argv[++i]);
449 if (f_v) {
450 cout << "-cipher_affine " << ptext << endl;
451 }
452 }
453 else if (ST.stringcmp(argv[i], "-analyze_substitution") == 0) {
454 f_analyze = TRUE;
455 t = substitution;
456 ctext.assign(argv[++i]);
457 if (f_v) {
458 cout << "-analyze_substitution " << ctext << endl;
459 }
460 }
461 else if (ST.stringcmp(argv[i], "-analyze_vigenere") == 0) {
462 f_analyze = TRUE;
463 t = vigenere;
464 ctext.assign(argv[++i]);
465 if (f_v) {
466 cout << "-analyze_vigenere " << ctext << endl;
467 }
468 }
469 else if (ST.stringcmp(argv[i], "-analyze_vigenere_kasiski") == 0) {
470 f_avk = TRUE;
471 ctext.assign(argv[++i]);
472 key_length = ST.strtoi(argv[++i]);
473 if (f_v) {
474 cout << "-analyze_vigenere_kasiski " << ctext << endl;
475 }
476 }
477 else if (ST.stringcmp(argv[i], "-kasiski") == 0) {
478 f_kasiski = TRUE;
479 ctext.assign(argv[++i]);
480 threshold = ST.strtoi(argv[++i]);
481 if (f_v) {
482 cout << "-kasiski " << ctext << endl;
483 }
484 }
485 else if (ST.stringcmp(argv[i], "-decipher_substitution") == 0) {
486 f_decipher = TRUE;
487 t = substitution;
488 ctext.assign(argv[++i]);
489 guess.assign(argv[++i]);
490 if (f_v) {
491 cout << "-decipher_substitution " << ctext << " " << guess << endl;
492 }
493 }
494 else if (ST.stringcmp(argv[i], "-decipher_vigenere") == 0) {
495 f_decipher = TRUE;
496 t = vigenere;
497 ctext.assign(argv[++i]);
498 key.assign(argv[++i]);
499 }
500 else if (ST.stringcmp(argv[i], "-decipher_affine") == 0) {
501 f_decipher = TRUE;
502 t = affine;
503 ctext.assign(argv[++i]);
504 guess.assign(argv[++i]);
505 }
506 else if (ST.stringcmp(argv[i], "-RSA") == 0) {
507 f_RSA = TRUE;
508 RSA_d = ST.strtoi(argv[++i]);
509 RSA_m = ST.strtoi(argv[++i]);
510 RSA_block_size = ST.strtoi(argv[++i]);
511 RSA_text.assign(argv[++i]);
512 if (f_v) {
513 cout << "-RSA " << RSA_d << " " << RSA_m << " " << RSA_block_size << " " << RSA_text << endl;
514 }
515 }
516 else if (ST.stringcmp(argv[i], "-RSA_encrypt_text") == 0) {
517 f_RSA_encrypt_text = TRUE;
518 RSA_d = ST.strtoi(argv[++i]);
519 RSA_m = ST.strtoi(argv[++i]);
520 RSA_block_size = ST.strtoi(argv[++i]);
521 RSA_encrypt_text.assign(argv[++i]);
522 if (f_v) {
523 cout << "-RSA_encrypt_text " << RSA_d << " "
524 << RSA_m << " " << RSA_block_size << " " << RSA_encrypt_text << endl;
525 }
526 }
527 else if (ST.stringcmp(argv[i], "-RSA_setup") == 0) {
528 f_RSA_setup = TRUE;
529 RSA_setup_nb_bits = ST.strtoi(argv[++i]);
530 RSA_setup_nb_tests_solovay_strassen = ST.strtoi(argv[++i]);
531 RSA_setup_f_miller_rabin_test = ST.strtoi(argv[++i]);
532 if (f_v) {
533 cout << "-RSA_setup " << RSA_setup_nb_bits << " "
534 << RSA_setup_nb_tests_solovay_strassen << " "
535 << RSA_setup_f_miller_rabin_test << endl;
536 }
537 }
538 else if (ST.stringcmp(argv[i], "-primitive_root") == 0) {
539 f_primitive_root = TRUE;
540 primitive_root_p.assign(argv[++i]);
541 if (f_v) {
542 cout << "-primitive_root " << primitive_root_p << endl;
543 }
544 }
545 else if (ST.stringcmp(argv[i], "-smallest_primitive_root") == 0) {
546 f_smallest_primitive_root = TRUE;
547 smallest_primitive_root_p = ST.strtoi(argv[++i]);
548 if (f_v) {
549 cout << "-smallest_primitive_root " << smallest_primitive_root_p << endl;
550 }
551 }
552 else if (ST.stringcmp(argv[i], "-smallest_primitive_root_interval") == 0) {
553 f_smallest_primitive_root_interval = TRUE;
554 smallest_primitive_root_interval_min = ST.strtoi(argv[++i]);
555 smallest_primitive_root_interval_max = ST.strtoi(argv[++i]);
556 if (f_v) {
557 cout << "-smallest_primitive_root_interval " << smallest_primitive_root_interval_min
558 << " " << smallest_primitive_root_interval_max << endl;
559 }
560 }
561 else if (ST.stringcmp(argv[i], "-number_of_primitive_roots_interval") == 0) {
562 f_number_of_primitive_roots_interval = TRUE;
563 smallest_primitive_root_interval_min = ST.strtoi(argv[++i]);
564 smallest_primitive_root_interval_max = ST.strtoi(argv[++i]);
565 if (f_v) {
566 cout << "-number_of_primitive_roots_interval " << smallest_primitive_root_interval_min
567 << " " << smallest_primitive_root_interval_max << endl;
568 }
569 }
570 else if (ST.stringcmp(argv[i], "-inverse_mod") == 0) {
571 f_inverse_mod = TRUE;
572 inverse_mod_a = ST.strtoi(argv[++i]);
573 inverse_mod_n = ST.strtoi(argv[++i]);
574 if (f_v) {
575 cout << "-inverse_mod " << inverse_mod_a << " " << inverse_mod_n << endl;
576 }
577 }
578 else if (ST.stringcmp(argv[i], "-extended_gcd") == 0) {
579 f_extended_gcd = TRUE;
580 extended_gcd_a = ST.strtoi(argv[++i]);
581 extended_gcd_b = ST.strtoi(argv[++i]);
582 if (f_v) {
583 cout << "-extended_gcd " << extended_gcd_a << " " << extended_gcd_b << endl;
584 }
585 }
586 else if (ST.stringcmp(argv[i], "-power_mod") == 0) {
587 f_power_mod = TRUE;
588 power_mod_a.assign(argv[++i]);
589 power_mod_k.assign(argv[++i]);
590 power_mod_n.assign(argv[++i]);
591 if (f_v) {
592 cout << "-power_mod " << power_mod_a << " " << power_mod_k << " " << power_mod_n << endl;
593 }
594 }
595 else if (ST.stringcmp(argv[i], "-discrete_log") == 0) {
596 f_discrete_log = TRUE;
597 discrete_log_y = ST.strtoi(argv[++i]);
598 discrete_log_a = ST.strtoi(argv[++i]);
599 discrete_log_m = ST.strtoi(argv[++i]);
600 if (f_v) {
601 cout << "-discrete_log " << discrete_log_y << " "
602 << discrete_log_a << " " << discrete_log_m << endl;
603 }
604 }
605 else if (ST.stringcmp(argv[i], "-sift_smooth") == 0) {
606 f_sift_smooth = TRUE;
607 sift_smooth_from = ST.strtoi(argv[++i]);
608 sift_smooth_len = ST.strtoi(argv[++i]);
609 sift_smooth_factor_base = argv[++i];
610 if (f_v) {
611 cout << "-sift_smooth " << sift_smooth_from << " "
612 << sift_smooth_len << " " << sift_smooth_factor_base << endl;
613 }
614 }
615 else if (ST.stringcmp(argv[i], "-square_root") == 0) {
616 f_square_root = TRUE;
617 square_root_number.assign(argv[++i]);
618 if (f_v) {
619 cout << "-square_root " << square_root_number << endl;
620 }
621 }
622 else if (ST.stringcmp(argv[i], "-square_root_mod") == 0) {
623 f_square_root_mod = TRUE;
624 square_root_mod_a.assign(argv[++i]);
625 square_root_mod_m.assign(argv[++i]);
626 if (f_v) {
627 cout << "-square_root_mod " << square_root_mod_a << " "
628 << square_root_mod_m << endl;
629 }
630 }
631 else if (ST.stringcmp(argv[i], "-all_square_roots_mod_n") == 0) {
632 f_all_square_roots_mod_n = TRUE;
633 all_square_roots_mod_n_a.assign(argv[++i]);
634 all_square_roots_mod_n_n.assign(argv[++i]);
635 if (f_v) {
636 cout << "-all_square_roots_mod_n " << all_square_roots_mod_n_a << " "
637 << all_square_roots_mod_n_n << endl;
638 }
639 }
640 else if (ST.stringcmp(argv[i], "-quadratic_sieve") == 0) {
641 f_quadratic_sieve = TRUE;
642 quadratic_sieve_n = ST.strtoi(argv[++i]);
643 quadratic_sieve_factorbase = ST.strtoi(argv[++i]);
644 quadratic_sieve_x0 = ST.strtoi(argv[++i]);
645 if (f_v) {
646 cout << "-quadratic_sieve " << quadratic_sieve_n << " "
647 << quadratic_sieve_factorbase << " " << quadratic_sieve_x0 << endl;
648 }
649 }
650 else if (ST.stringcmp(argv[i], "-jacobi") == 0) {
651 f_jacobi = TRUE;
652 jacobi_top = ST.strtoi(argv[++i]);
653 jacobi_bottom = ST.strtoi(argv[++i]);
654 if (f_v) {
655 cout << "-jacobi " << jacobi_top << " "
656 << jacobi_bottom << endl;
657 }
658 }
659 else if (ST.stringcmp(argv[i], "-solovay_strassen") == 0) {
660 f_solovay_strassen = TRUE;
661 solovay_strassen_p = ST.strtoi(argv[++i]);
662 solovay_strassen_a = ST.strtoi(argv[++i]);
663 if (f_v) {
664 cout << "-solovay_strassen " << solovay_strassen_p << " "
665 << solovay_strassen_a << endl;
666 }
667 }
668 else if (ST.stringcmp(argv[i], "-miller_rabin") == 0) {
669 f_miller_rabin = TRUE;
670 miller_rabin_p = ST.strtoi(argv[++i]);
671 miller_rabin_nb_times = ST.strtoi(argv[++i]);
672 if (f_v) {
673 cout << "-miller_rabin " << miller_rabin_p << " " << miller_rabin_nb_times << endl;
674 }
675 }
676 else if (ST.stringcmp(argv[i], "-fermat_test") == 0) {
677 f_fermat_test = TRUE;
678 fermat_test_p = ST.strtoi(argv[++i]);
679 fermat_test_nb_times = ST.strtoi(argv[++i]);
680 if (f_v) {
681 cout << "-fermat_test " << fermat_test_p << " " << fermat_test_nb_times << endl;
682 }
683 }
684 else if (ST.stringcmp(argv[i], "-find_pseudoprime") == 0) {
685 f_find_pseudoprime = TRUE;
686 find_pseudoprime_nb_digits = ST.strtoi(argv[++i]);
687 find_pseudoprime_nb_fermat = ST.strtoi(argv[++i]);
688 find_pseudoprime_nb_miller_rabin = ST.strtoi(argv[++i]);
689 find_pseudoprime_nb_solovay_strassen = ST.strtoi(argv[++i]);
690 if (f_v) {
691 cout << "-find_pseudoprime " << find_pseudoprime_nb_digits
692 << " " << find_pseudoprime_nb_fermat
693 << " " << find_pseudoprime_nb_miller_rabin
694 << " " << find_pseudoprime_nb_solovay_strassen << endl;
695 }
696 }
697 else if (ST.stringcmp(argv[i], "-find_strong_pseudoprime") == 0) {
698 f_find_strong_pseudoprime = TRUE;
699 find_pseudoprime_nb_digits = ST.strtoi(argv[++i]);
700 find_pseudoprime_nb_fermat = ST.strtoi(argv[++i]);
701 find_pseudoprime_nb_miller_rabin = ST.strtoi(argv[++i]);
702 if (f_v) {
703 cout << "-find_strong_pseudoprime " << find_pseudoprime_nb_digits
704 << " " << find_pseudoprime_nb_fermat
705 << " " << find_pseudoprime_nb_miller_rabin
706 << endl;
707 }
708 }
709 else if (ST.stringcmp(argv[i], "-miller_rabin_text") == 0) {
710 f_miller_rabin_text = TRUE;
711 miller_rabin_text_nb_times = ST.strtoi(argv[++i]);
712 miller_rabin_number_text.assign(argv[++i]);
713 if (f_v) {
714 cout << "-miller_rabin " << miller_rabin_text_nb_times
715 << " " << miller_rabin_number_text
716 << endl;
717 }
718 }
719 else if (ST.stringcmp(argv[i], "-random") == 0) {
720 f_random = TRUE;
721 random_nb = ST.strtoi(argv[++i]);
722 random_fname_csv.assign(argv[++i]);
723 if (f_v) {
724 cout << "-random " << random_nb << " " << random_fname_csv << endl;
725 }
726 }
727 else if (ST.stringcmp(argv[i], "-random_last") == 0) {
728 f_random_last = TRUE;
729 random_last_nb = ST.strtoi(argv[++i]);
730 if (f_v) {
731 cout << "-random_last " << random_last_nb << endl;
732 }
733 }
734 else if (ST.stringcmp(argv[i], "-affine_sequence") == 0) {
735 f_affine_sequence = TRUE;
736 affine_sequence_a = ST.strtoi(argv[++i]);
737 affine_sequence_c = ST.strtoi(argv[++i]);
738 affine_sequence_m = ST.strtoi(argv[++i]);
739 if (f_v) {
740 cout << "-affine_sequence " << affine_sequence_a
741 << " " << affine_sequence_c << " " << affine_sequence_m << endl;
742 }
743 }
744 if (f_v) {
745 cout << "interface_cryptography::read_arguments done" << endl;
746 }
747}
748
750{
751 if (f_cipher && t == substitution) {
752 cout << "-cipher_substitution " << ptext << endl;
753 }
754 if (f_cipher && t == vigenere) {
755 cout << "cipher_vigenere" << ptext << " " << key << endl;
756 }
757 if (f_cipher && t == affine) {
758 cout << "-cipher_affine " << ptext << " " << affine_a << " " << affine_b << endl;
759 }
760 if (f_analyze && t == substitution) {
761 cout << "-analyze_substitution " << ctext << endl;
762 }
763 if (f_analyze && t == vigenere) {
764 cout << "-analyze_vigenere " << ctext << endl;
765 }
766 if (f_avk) {
767 cout << "-analyze_vigenere_kasiski " << ctext << " " << key_length << endl;
768 }
769 if (f_kasiski) {
770 cout << "-kasiski " << ctext << " " << threshold << endl;
771 }
772 if (f_decipher && t == substitution) {
773 cout << "-decipher_substitution " << ctext << " " << guess << endl;
774 }
775 if (f_decipher && t == vigenere) {
776 cout << "-decipher_vigenere " << ctext << " " << key << endl;
777 }
778 if (f_decipher && t == affine) {
779 cout << "-decipher_affine " << ctext << " " << guess << endl;
780 }
781 if (f_RSA) {
782 cout << "-RSA " << RSA_d << " " << RSA_m << " " << RSA_block_size << " " << RSA_text << endl;
783 }
784 if (f_RSA_encrypt_text) {
785 cout << "-RSA_encrypt_text " << RSA_d << " "
786 << RSA_m << " " << RSA_block_size << " " << RSA_encrypt_text << endl;
787 }
788 if (f_RSA_setup) {
789 cout << "-RSA_setup " << RSA_setup_nb_bits << " "
790 << RSA_setup_nb_tests_solovay_strassen << " "
791 << RSA_setup_f_miller_rabin_test << endl;
792 }
793 if (f_primitive_root) {
794 cout << "-primitive_root " << primitive_root_p << endl;
795 }
796 if (f_smallest_primitive_root) {
797 cout << "-smallest_primitive_root " << smallest_primitive_root_p << endl;
798 }
799 if (f_smallest_primitive_root_interval) {
800 cout << "-smallest_primitive_root_interval " << smallest_primitive_root_interval_min
801 << " " << smallest_primitive_root_interval_max << endl;
802 }
803 if (f_number_of_primitive_roots_interval) {
804 cout << "-number_of_primitive_roots_interval " << smallest_primitive_root_interval_min
805 << " " << smallest_primitive_root_interval_max << endl;
806 }
807 if (f_inverse_mod) {
808 cout << "-inverse_mod " << inverse_mod_a << " " << inverse_mod_n << endl;
809 }
810 if (f_extended_gcd) {
811 cout << "-extended_gcd " << extended_gcd_a << " " << extended_gcd_b << endl;
812 }
813 if (f_power_mod) {
814 cout << "-power_mod " << power_mod_a << " " << power_mod_k << " " << power_mod_n << endl;
815 }
816 if (f_discrete_log) {
817 cout << "-discrete_log " << discrete_log_y << " "
818 << discrete_log_a << " " << discrete_log_m << endl;
819 }
820 if (f_sift_smooth) {
821 cout << "-sift_smooth " << sift_smooth_from << " "
822 << sift_smooth_len << " " << sift_smooth_factor_base << endl;
823 }
824 if (f_square_root) {
825 cout << "-square_root " << square_root_number << endl;
826 }
827 if (f_square_root_mod) {
828 cout << "-square_root_mod " << square_root_mod_a << " "
829 << square_root_mod_m << endl;
830 }
831 if (f_all_square_roots_mod_n) {
832 cout << "-all_square_roots_mod_n " << all_square_roots_mod_n_a << " "
833 << all_square_roots_mod_n_n << endl;
834 }
835 if (f_quadratic_sieve) {
836 cout << "-quadratic_sieve " << quadratic_sieve_n << " "
837 << quadratic_sieve_factorbase << " " << quadratic_sieve_x0 << endl;
838 }
839 if (f_jacobi) {
840 cout << "-jacobi " << jacobi_top << " "
841 << jacobi_bottom << endl;
842 }
843 if (f_solovay_strassen) {
844 cout << "-solovay_strassen " << solovay_strassen_p << " "
845 << solovay_strassen_a << endl;
846 }
847 if (f_miller_rabin) {
848 cout << "-miller_rabin " << miller_rabin_p << " " << miller_rabin_nb_times << endl;
849 }
850 if (f_fermat_test) {
851 cout << "-fermat_test " << fermat_test_p << " " << fermat_test_nb_times << endl;
852 }
853 if (f_find_pseudoprime) {
854 cout << "-find_pseudoprime " << find_pseudoprime_nb_digits
855 << " " << find_pseudoprime_nb_fermat
856 << " " << find_pseudoprime_nb_miller_rabin
857 << " " << find_pseudoprime_nb_solovay_strassen << endl;
858 }
859 if (f_find_strong_pseudoprime) {
860 cout << "-find_strong_pseudoprime " << find_pseudoprime_nb_digits
861 << " " << find_pseudoprime_nb_fermat
862 << " " << find_pseudoprime_nb_miller_rabin
863 << endl;
864 }
865 if (f_miller_rabin_text) {
866 cout << "-miller_rabin " << miller_rabin_text_nb_times
867 << " " << miller_rabin_number_text
868 << endl;
869 }
870 if (f_random) {
871 cout << "-random " << random_nb << " " << random_fname_csv << endl;
872 }
873 if (f_random_last) {
874 cout << "-random_last " << random_last_nb << endl;
875 }
876 if (f_affine_sequence) {
877 cout << "-affine_sequence " << affine_sequence_a
878 << " " << affine_sequence_c << " " << affine_sequence_m << endl;
879 }
880}
881
882void interface_cryptography::worker(int verbose_level)
883{
884 if (f_cipher) {
885
887
888 if (t == substitution) {
889 Crypto.get_random_permutation(key);
890 cout << "ptext: " << ptext << endl;
891 Crypto.substition_cipher(ptext, ctext, key);
892 cout << "ctext: " << ctext << endl;
893 }
894 else if (t == vigenere) {
895 cout << "vigenere cipher with key " << key << endl;
896 cout << "ptext: " << ptext << endl;
897 Crypto.vigenere_cipher(ptext, ctext, key);
898 cout << "ctext: " << ctext << endl;
899 }
900 else if (t == affine) {
901 cout << "affine cipher with key (" << affine_a << "," << affine_b << ")" << endl;
902 cout << "ptext: " << ptext << endl;
903 Crypto.affine_cipher(ptext, ctext, affine_a, affine_b);
904 cout << "ctext: " << ctext << endl;
905 }
906 }
907 else if (f_analyze) {
908
910
911 if (t == substitution) {
912 cout << "ctext: " << endl << ctext << endl;
913 Crypto.analyze(ctext);
914 }
915 if (t == vigenere) {
916 cout << "ctext: " << endl << ctext << endl;
917 Crypto.vigenere_analysis(ctext);
918 }
919 }
920 else if (f_avk) {
921
923
924 Crypto.vigenere_analysis2(ctext, key_length);
925 }
926 else if (f_kasiski) {
927
929
930 int m;
931
932 m = Crypto.kasiski_test(ctext, threshold);
933 cout << "kasiski test for threshold " << threshold
934 << " yields key length " << m << endl;
935 }
936 else if (f_decipher) {
937
939
940 if (t == substitution) {
941 cout << "ctext: " << ctext << endl;
942 cout << "guess: " << guess << endl;
943 Crypto.decipher(ctext, ptext, guess);
944 cout << " " << endl;
945 Crypto.print_on_top(ptext, ctext);
946 //cout << "ptext: " << ptext << endl;
947 }
948 else if (t == vigenere) {
949 cout << "ctext: " << ctext << endl;
950 cout << "key : " << key << endl;
951 Crypto.vigenere_decipher(ctext, ptext, key);
952 cout << " " << endl;
953 Crypto.print_on_top(ptext, ctext);
954 }
955 else if (t == affine) {
956 //cout << "affine cipher with key (" << affine_a << "," << affine_b << ")" << endl;
957 cout << "affine cipher" << endl;
958 cout << "ctext: " << ctext << endl;
959 cout << "guess: " << guess << endl;
960 Crypto.affine_decipher(ctext, ptext, guess);
961 //cout << " " << endl;
962 //print_on_top(ptext, ctext);
963 }
964 }
965 else if (f_discrete_log) {
966
968
969
970 Crypto.do_discrete_log(discrete_log_y, discrete_log_a, discrete_log_m, verbose_level);
971 }
972 else if (f_primitive_root) {
973
975
976 //longinteger_domain D;
978
979 p.create_from_base_10_string(primitive_root_p);
980 Crypto.do_primitive_root_longinteger(p, verbose_level);
981 }
982 else if (f_smallest_primitive_root) {
983
985
986 Crypto.do_smallest_primitive_root(smallest_primitive_root_p, verbose_level);
987 }
988 else if (f_smallest_primitive_root_interval) {
989
991
992 Crypto.do_smallest_primitive_root_interval(smallest_primitive_root_interval_min,
993 smallest_primitive_root_interval_max, verbose_level);
994 }
995 else if (f_number_of_primitive_roots_interval) {
996
998
999 Crypto.do_number_of_primitive_roots_interval(smallest_primitive_root_interval_min,
1000 smallest_primitive_root_interval_max, verbose_level);
1001 }
1002 else if (f_inverse_mod) {
1003
1005
1006 Crypto.do_inverse_mod(inverse_mod_a, inverse_mod_n, verbose_level);
1007 }
1008 else if (f_extended_gcd) {
1009
1011
1012 Crypto.do_extended_gcd(extended_gcd_a, extended_gcd_b, verbose_level);
1013 }
1014 else if (f_power_mod) {
1015
1017
1021
1022 a.create_from_base_10_string(power_mod_a);
1023 k.create_from_base_10_string(power_mod_k);
1024 n.create_from_base_10_string(power_mod_n);
1025
1026 Crypto.do_power_mod(a, k, n, verbose_level);
1027 }
1028 else if (f_RSA) {
1029
1031
1032 Crypto.do_RSA(RSA_d, RSA_m, RSA_block_size, RSA_text, verbose_level);
1033 }
1034 else if (f_RSA_encrypt_text) {
1035
1037
1038 Crypto.do_RSA_encrypt_text(RSA_d, RSA_m, RSA_block_size,
1039 RSA_encrypt_text, verbose_level);
1040 }
1041 else if (f_RSA_setup) {
1043 ring_theory::longinteger_object n, p, q, a, b;
1044
1045 Crypto.RSA_setup(n, p, q, a, b,
1046 RSA_setup_nb_bits,
1047 RSA_setup_nb_tests_solovay_strassen,
1048 RSA_setup_f_miller_rabin_test,
1049 1 /*verbose_level */);
1050 }
1051 else if (f_sift_smooth) {
1052
1054
1055 Crypto.do_sift_smooth(sift_smooth_from,
1056 sift_smooth_len,
1057 sift_smooth_factor_base, verbose_level);
1058 }
1059 else if (f_square_root) {
1060
1062
1063 Crypto.square_root(square_root_number, verbose_level);
1064 }
1065 else if (f_square_root_mod) {
1066
1068
1069 Crypto.square_root_mod(square_root_mod_a, square_root_mod_m, verbose_level);
1070 }
1071
1072 else if (f_all_square_roots_mod_n) {
1073
1075 vector<long int> S;
1076 int i;
1077
1079 all_square_roots_mod_n_a, all_square_roots_mod_n_n, S, verbose_level);
1080
1081 cout << "We found " << S.size() << " square roots of "
1082 << all_square_roots_mod_n_a << " mod " << all_square_roots_mod_n_n << endl;
1083 cout << "They are:" << endl;
1084 for (i = 0; i < S.size(); i++) {
1085 cout << i << " : " << S[i] << endl;
1086 }
1087 }
1088
1089
1090 else if (f_quadratic_sieve) {
1091
1093
1094 Crypto.quadratic_sieve(quadratic_sieve_n,
1095 quadratic_sieve_factorbase,
1096 quadratic_sieve_x0,
1097 verbose_level);
1098 }
1099 else if (f_jacobi) {
1100
1102
1103 Crypto.do_jacobi(jacobi_top, jacobi_bottom, verbose_level);
1104 }
1105 else if (f_solovay_strassen) {
1106
1108
1109 Crypto.do_solovay_strassen(solovay_strassen_p, solovay_strassen_a, verbose_level);
1110 }
1111 else if (f_miller_rabin) {
1112
1114
1115 Crypto.do_miller_rabin(miller_rabin_p, miller_rabin_nb_times, verbose_level);
1116 }
1117 else if (f_fermat_test) {
1118
1120
1121 Crypto.do_fermat_test(fermat_test_p, fermat_test_nb_times, verbose_level);
1122 }
1123 else if (f_find_pseudoprime) {
1124
1126
1127 Crypto.do_find_pseudoprime(
1128 find_pseudoprime_nb_digits,
1129 find_pseudoprime_nb_fermat,
1130 find_pseudoprime_nb_miller_rabin,
1131 find_pseudoprime_nb_solovay_strassen,
1132 verbose_level);
1133 }
1134 else if (f_find_strong_pseudoprime) {
1135
1137
1139 find_pseudoprime_nb_digits,
1140 find_pseudoprime_nb_fermat,
1141 find_pseudoprime_nb_miller_rabin,
1142 verbose_level);
1143 }
1144 else if (f_miller_rabin_text) {
1145
1147
1148 Crypto.do_miller_rabin_text(
1149 miller_rabin_number_text, miller_rabin_text_nb_times,
1150 verbose_level);
1151 }
1152 else if (f_random) {
1153
1155
1156 Crypto.do_random(
1157 random_nb, random_fname_csv,
1158 verbose_level);
1159 }
1160 else if (f_random_last) {
1161
1163
1164 Crypto.do_random_last(
1165 random_last_nb,
1166 verbose_level);
1167 }
1168 else if (f_affine_sequence) {
1169
1171
1172 Crypto.make_affine_sequence(affine_sequence_a,
1173 affine_sequence_c, affine_sequence_m, verbose_level);
1174 }
1175}
1176
1177
1178
1179
1180}}}
1181
1182
a collection of functions related to cryptography
Definition: cryptography.h:25
void do_miller_rabin_text(std::string &number_text, int nb_miller_rabin, int verbose_level)
void print_on_top(std::string &text1, std::string &text2)
void do_jacobi(int jacobi_top, int jacobi_bottom, int verbose_level)
void vigenere_cipher(std::string &ptext, std::string &ctext, std::string &key)
void do_sift_smooth(int sift_smooth_from, int sift_smooth_len, std::string &sift_smooth_factor_base, int verbose_level)
void do_find_strong_pseudoprime(int nb_digits, int nb_fermat, int nb_miller_rabin, int verbose_level)
void do_discrete_log(long int y, long int a, long int p, int verbose_level)
void do_fermat_test(int p, int nb_times, int verbose_level)
void make_affine_sequence(int a, int c, int m, int verbose_level)
void all_square_roots_mod_n_by_exhaustive_search_lint(std::string &square_root_a, std::string &square_root_mod_n, std::vector< long int > &S, int verbose_level)
void do_RSA(long int RSA_d, long int RSA_m, int RSA_block_size, std::string &RSA_text, int verbose_level)
void decipher(std::string &ctext, std::string &ptext, std::string &guess)
void do_random(int random_nb, std::string &fname_csv, int verbose_level)
void do_power_mod(ring_theory::longinteger_object &a, ring_theory::longinteger_object &k, ring_theory::longinteger_object &n, int verbose_level)
void substition_cipher(std::string &ptext, std::string &ctext, std::string &key)
void RSA_setup(ring_theory::longinteger_object &n, ring_theory::longinteger_object &p, ring_theory::longinteger_object &q, ring_theory::longinteger_object &a, ring_theory::longinteger_object &b, int nb_bits, int nb_tests_solovay_strassen, int f_miller_rabin_test, int verbose_level)
void affine_cipher(std::string &ptext, std::string &ctext, int a, int b)
void do_miller_rabin(int p, int nb_times, int verbose_level)
void do_number_of_primitive_roots_interval(long int p_min, long int p_max, int verbose_level)
void do_inverse_mod(long int a, long int n, int verbose_level)
void do_RSA_encrypt_text(long int RSA_d, long int RSA_m, int RSA_block_size, std::string &RSA_encrypt_text, int verbose_level)
void square_root(std::string &square_root_number, int verbose_level)
void affine_decipher(std::string &ctext, std::string &ptext, std::string &guess)
void square_root_mod(std::string &square_root_number, std::string &mod_number, int verbose_level)
void vigenere_decipher(std::string &ctext, std::string &ptext, std::string &key)
void do_primitive_root_longinteger(ring_theory::longinteger_object &p, int verbose_level)
void do_smallest_primitive_root_interval(long int p_min, long int p_max, int verbose_level)
void quadratic_sieve(int n, int factorbase, int x0, int verbose_level)
void do_find_pseudoprime(int nb_digits, int nb_fermat, int nb_miller_rabin, int nb_solovay_strassen, int verbose_level)
functions related to strings and character arrays
a class to represent arbitrary precision integers
Definition: ring_theory.h:366
void create_from_base_10_string(const char *str, int verbose_level)
void read_arguments(int argc, std::string *argv, int &i, int verbose_level)
int recognize_keyword(int argc, std::string *argv, int i, int verbose_level)
void print_help(int argc, std::string *argv, int i, int verbose_level)
#define TRUE
Definition: foundations.h:231
#define FALSE
Definition: foundations.h:234
the orbiter library for the classification of combinatorial objects