-
Notifications
You must be signed in to change notification settings - Fork 0
/
teorie.html
1407 lines (1156 loc) · 81.4 KB
/
teorie.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<!--Bootstrap files-->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<title>Teorie</title>
</head>
<body style="background: #283845">
<nav class="navbar navbar-expand-lg navbar-dark bg-dark sticky-top">
<div class="container-fluid">
<a class="navbar-brand" href="index.html">Acasă</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="teorie.html">Teorie</a>
</li>
<li class="nav-item">
<a class="nav-link" href="criptare.html" >Criptează</a>
</li>
<li class="nav-item"></li>
<a class="nav-link" href="undeSeFolosesc.html">Unde se folosesc?</a>
</li>
<li class="nav-item"></li>
<a class="nav-link" href="register.html">Înregistrează-te</a>
</li>
</ul>
</div>
</div>
</nav>
<br>
<div class="card center "style="width: 90%; background-color:#28242c;">
<div class="card-body">
<h2>Criptare simetrica vs asimetrica</h2>
<p><br>
<b>Criptarea simetrică </b>utilizează aceeași cheie atât pentru criptare, cât și pentru decriptare. Ambele părți trebuie să aibă acces la aceeași cheie secretă pentru a comunica în siguranță.
<br> <br> <b>Criptarea asimetrică</b> folosește o pereche de chei: o cheie publică (pentru criptare) și o cheie privată (pentru decriptare). Cheia publică poate fi distribuită oricui, iar cea privată este ținută secretă. <br> Exemple: RSA, ECC, Diffie-Hellman.
<br> <br> <b>Diferențe principale: </b>
<br> • <b>Chei</b>: Simetrică folosește o singură cheie, asimetrică două.<br>
• <b>Viteză</b>: Criptarea simetrică este mai rapidă, asimetrică este mai lentă.<br>
• <b>Securitate</b>: Criptarea asimetrică oferă o securitate mai bună pentru schimbul de chei.<br>
</p>
<h2>Comparatie Algoritmi de Criptare</h2>
<div class="table-responsive">
<table>
<thead>
<tr>
<th>Algoritm</th>
<th>Viteză</th>
<th>Securitate</th>
<th>Simetric/Asimetric</th>
<th>Tip de input</th>
</tr>
</thead>
<tbody>
<tr>
<td>Cifrul Caesar</td>
<td>Foarte rapid (O(1))</td>
<td>Foarte scăzută</td>
<td>Simetric</td>
<td>Text</td>
</tr>
<tr>
<td>Codificare HEXA</td>
<td>Foarte rapid (O(1))</td>
<td>Niciuna (Doar codificare)</td>
<td>N/A (Codificare)</td>
<td>Text/Binar</td>
</tr>
<tr>
<td>RSA (Simplificat)</td>
<td>Lent</td>
<td>Foarte ridicată</td>
<td>Asimetric</td>
<td>Numere întregi/Binar</td>
</tr>
<tr>
<td>Blowfish</td>
<td>Rapid</td>
<td>Ridicată</td>
<td>Simetric</td>
<td>Binar (Cifru pe blocuri)</td>
</tr>
<tr>
<td>AES</td>
<td>Rapid</td>
<td>Foarte ridicată</td>
<td>Simetric</td>
<td>Binar (Cifru pe blocuri)</td>
</tr>
<tr>
<td>ECC</td>
<td>Moderat</td>
<td>Foarte ridicată</td>
<td>Asimetric</td>
<td>Numere întregi/Binar</td>
</tr>
<tr>
<td>TwoFish</td>
<td>Rapid</td>
<td>Ridicată</td>
<td>Simetric</td>
<td>Binar (Cifru pe blocuri)</td>
</tr>
<tr>
<td>DES</td>
<td>Rapid</td>
<td>Scăzută (Depășită)</td>
<td>Simetric</td>
<td>Binar (Cifru pe blocuri)</td>
</tr>
<tr>
<td>Diffie-Hellman</td>
<td>Lent</td>
<td>Ridicată</td>
<td>Asimetric</td>
<td>Numere întregi/Binar</td>
</tr>
</tbody>
</table>
</div>
</div><br>
<!--Cifrul lui Ceasar-->
<div style="padding: 20px;">
<h1>Algoritmul de criptare Caesar Cipher în C++</h1>
<p>Acest cod C++ implementează un simplu cifru Caesar pentru criptare și decriptare a textului.</p>
<h2>Codul C++:</h2>
<button class="accordion">Cifrul lui Ceasar</button>
<div class="panel">
<pre><code>
<span class="keyword">#include</span> <iostream>
<span class="keyword">#include</span> <string>
<span class="keyword">using namespace</span> std;
<span class="comment">// Funcție pentru criptare</span>
<span class="keyword">string</span> <span class="function">encrypt</span>(<span class="keyword">string</span> text, <span class="keyword">int</span> shift) {
<span class="keyword">string</span> result = <span class="string">""</span>;
<span class="comment">// Parcugem fiecare caracter din text</span>
<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < text.length(); i++) {
<span class="comment">// Criptăm doar literele mari și mici</span>
<span class="keyword">if</span> (isupper(text[i])) {
<span class="comment">// Pentru litere mari (A-Z)</span>
result += <span class="keyword">char</span>(<span class="keyword">int</span>(text[i] + shift - <span class="number">65</span>) % <span class="number">26</span> + <span class="number">65</span>);
}
<span class="keyword">else if</span> (islower(text[i])) {
<span class="comment">// Pentru litere mici (a-z)</span>
result += <span class="keyword">char</span>(<span class="keyword">int</span>(text[i] + shift - <span class="number">97</span>) % <span class="number">26</span> + <span class="number">97</span>);
}
<span class="keyword">else</span> {
<span class="comment">// Orice alt caracter rămâne neschimbat</span>
result += text[i];
}
}
<span class="keyword">return</span> result;
}
<span class="comment">// Funcție pentru decriptare (opusul criptării)</span>
<span class="keyword">string</span> <span class="function">decrypt</span>(<span class="keyword">string</span> text, <span class="keyword">int</span> shift) {
<span class="keyword">return</span> <span class="function">encrypt</span>(text, <span class="number">26</span> - shift); <span class="comment">// Deplasăm invers</span>
}
<span class="keyword">int</span> <span class="function">main</span>() {
<span class="keyword">string</span> text = <span class="string">"Salut Lume!"</span>; <span class="comment">// Textul de criptat</span>
<span class="keyword">int</span> shift = <span class="number">4</span>; <span class="comment">// Cheia de criptare (deplasarea)</span>
<span class="keyword">string</span> encrypted = <span class="function">encrypt</span>(text, shift);
<span class="keyword">string</span> decrypted = <span class="function">decrypt</span>(encrypted, shift);
<span class="function">cout</span> << <span class="string">"Text original: "</span> << text << <span class="function">endl</span>;
<span class="function">cout</span> << <span class="string">"Text criptat: "</span> << encrypted << <span class="function">endl</span>;
<span class="function">cout</span> << <span class="string">"Text decriptat: "</span> << decrypted << <span class="function">endl</span>;
<span class="keyword">return</span> <span class="number">0</span>;
}
</code></pre>
</div>
<h2>Codul Python:</h2>
<button class="accordion">Cifrul lui Ceasar</button>
<div class="panel">
<pre><code>
<span class="keyword">def</span> <span class="function">caesar_cipher</span>(message, shift):
<span class="comment"># Funcția pentru criptarea mesajului folosind cifra lui Caesar</span>
encrypted_message = <span class="string">""</span>
<span class="keyword">for</span> char <span class="keyword">in</span> message:
<span class="keyword">if</span> char.<span class="function">isalpha</span>():
shift_base = <span class="keyword">ord</span>(<span class="string">'a'</span>) <span class="keyword">if</span> char.<span class="function">islower</span>() <span class="keyword">else</span> <span class="keyword">ord</span>(<span class="string">'A'</span>)
encrypted_char = <span class="keyword">chr</span>(<span class="keyword">ord</span>(char) - shift_base + shift) % <span class="number">26</span> + shift_base
encrypted_message += encrypted_char
<span class="keyword">else</span>:
encrypted_message += char
<span class="keyword">return</span> encrypted_message
<span class="keyword"># Exemplu de utilizare</span>
message = <span class="string">"Mesaj de test."</span>
shift = <span class="number">3</span>
encrypted = caesar_cipher(message, shift)
print(<span class="string">"Mesaj criptat:"</span>, encrypted)
</code></pre>
</div>
</div>
<!--HEXA-->
<div style="padding: 20px;">
<h1>Algoritmul de criptare HEXA în C++</h1>
<p>Acest cod C++ implementează un algoritm HEXA pentru criptare și decriptare a textului.</p>
<h2>Codul C++:</h2>
<button class="accordion">HEXA</button>
<div class="panel">
<pre><code>
<span class="keyword">#include</span> <iostream>
<span class="keyword">#include</span> <sstream>
<span class="keyword">#include</span> <iomanip>
<span class="keyword">#include</span> <string>
<span class="keyword">using namespace</span> std;
<span class="comment">// Funcție care convertește un șir de caractere într-o reprezentare hexazecimală</span>
<span class="keyword">std::string</span> <span class="function">stringToHex</span>(<span class="keyword">const std::string&</span> input) {
<span class="keyword">std::stringstream</span> ss;
ss << std::hex << std::setfill(<span class="string">'0'</span>);
<span class="keyword">for</span> (<span class="keyword">const char&</span> ch : input) {
ss << std::setw(<span class="number">2</span>) << static_cast<<span class="keyword">int</span>>(static_cast<<span class="keyword">unsigned char</span>>(ch));
}
<span class="keyword">return</span> ss.str();
}
<span class="comment">// Funcție care convertește un șir de caractere hexazecimal înapoi în reprezentarea sa originală</span>
<span class="keyword">std::string</span> <span class="function">hexToString</span>(<span class="keyword">const std::string&</span> hexStr) {
<span class="keyword">std::string</span> output;
<span class="keyword">for</span> (<span class="keyword">size_t</span> i = <span class="number">0</span>; i < hexStr.length(); i += <span class="number">2</span>) {
<span class="keyword">std::string</span> part = hexStr.substr(i, <span class="number">2</span>);
<span class="keyword">char</span> ch = static_cast<<span class="keyword">char</span>>(std::stoi(part, <span class="keyword">nullptr</span>, <span class="number">16</span>));
output.push_back(ch);
}
<span class="keyword">return</span> output;
}
<span class="comment">// Funcție de criptare/decriptare folosind XOR</span>
<span class="keyword">std::string</span> <span class="function">xorEncryptDecrypt</span>(<span class="keyword">const std::string&</span> input, <span class="keyword">char</span> key) {
<span class="keyword">std::string</span> output = input;
<span class="keyword">for</span> (<span class="keyword">size_t</span> i = <span class="number">0</span>; i < input.size(); ++i) {
output[i] = input[i] ^ key; <span class="comment">// Aplică operația XOR cu cheia</span>
}
<span class="keyword">return</span> output;
}
<span class="comment">// Funcție de criptare</span>
<span class="keyword">std::string</span> <span class="function">hexaEncrypt</span>(<span class="keyword">const std::string&</span> message, <span class="keyword">char</span> key) {
<span class="keyword">std::string</span> xorEncrypted = xorEncryptDecrypt(message, key); <span class="comment">// Pasul 1: Criptare XOR</span>
<span class="keyword">return</span> stringToHex(xorEncrypted); <span class="comment">// Pasul 2: Convertire în hexazecimal</span>
}
<span class="comment">// Funcție de decriptare</span>
<span class="keyword">std::string</span> <span class="function">hexaDecrypt</span>(<span class="keyword">const std::string&</span> hexMessage, <span class="keyword">char</span> key) {
<span class="keyword">std::string</span> xorDecrypted = hexToString(hexMessage); <span class="comment">// Pasul 1: Convertire din hexazecimal în șir normal</span>
<span class="keyword">return</span> xorEncryptDecrypt(xorDecrypted, key); <span class="comment">// Pasul 2: Decriptare XOR</span>
}
<span class="keyword">int</span> <span class="function">main</span>() {
<span class="keyword">std::string</span> message;
<span class="keyword">char</span> key;
<span class="comment">// Citim mesajul de criptat de la utilizator</span>
<span class="function">std::cout</span> << <span class="string">"Introdu mesajul pentru criptare: "</span>;
<span class="function">std::getline</span>(std::cin, message);
<span class="comment">// Citim cheia pentru criptare (un singur caracter)</span>
<span class="function">std::cout</span> << <span class="string">"Introdu cheia (un singur caracter): "</span>;
<span class="function">std::cin</span> >> key;
<span class="comment">// Criptăm mesajul</span>
<span class="keyword">std::string</span> encrypted = hexaEncrypt(message, key);
<span class="function">std::cout</span> << <span class="string">"Mesaj criptat (Hexa Crypt): "</span> << encrypted << <span class="function">std::endl</span>;
<span class="comment">// Decriptăm mesajul</span>
<span class="keyword">std::string</span> decrypted = hexaDecrypt(encrypted, key);
<span class="function">std::cout</span> << <span class="string">"Mesaj decriptat (Mesaj original): "</span> << decrypted << <span class="function">std::endl</span>;
<span class="keyword">return</span> <span class="number">0</span>;
}
</code></pre>
</div>
<h2>Codul Python:</h2>
<button class="accordion">HEXA</button>
<div class="panel">
<pre><code>
<span class="keyword">def</span> <span class="function">string_to_hex</span>(input_string):
<span class="comment"># Funcție care convertește un șir de caractere în hexazecimal</span>
return <span class="keyword">''.join</span>(<span class="function">format</span>(ord(c), <span class="string">'02x'</span>) <span class="keyword">for</span> c <span class="keyword">in</span> input_string)
<span class="keyword">def</span> <span class="function">hex_to_string</span>(hex_string):
<span class="comment"># Funcție care convertește un șir hexazecimal înapoi în caractere</span>
return <span class="keyword">''.join</span>(<span class="keyword">chr</span>(int(hex_string[i:i + <span class="number">2</span>], <span class="number">16</span>)) <span class="keyword">for</span> i <span class="keyword">in</span> range(0, <span class="keyword">len</span>(hex_string), <span class="number">2</span>))
<span class="keyword"># Exemplu de utilizare</span>
message = <span class="string">"Mesaj de test."</span>
hex_value = string_to_hex(message)
print(<span class="string">"Mesaj în hexazecimal:"</span>, hex_value)
decrypted_message = hex_to_string(hex_value)
print(<span class="string">"Mesaj decriptat:"</span>, decrypted_message)
</code></pre>
</div>
</div>
<!--RSA-->
<div style="padding: 20px;">
<h1>RSA Simplificat în C++</h1>
<p>Acest cod C++ implementează un algoritm simplificat pentru criptarea și decriptarea unui mesaj numeric folosind RSA.</p>
<h2>Codul C++:</h2>
<button class="accordion">RSA</button>
<div class="panel">
<pre><code>
<span class="keyword">#include</span> <iostream>
<span class="keyword">#include</span> <cmath>
<span class="keyword">#include</span> <cstdlib>
<span class="keyword">#include</span> <ctime>
<span class="keyword">using</span> <span class="keyword">namespace</span> std;
<span class="comment">// Funcție pentru a calcula cel mai mare divizor comun (GCD)</span>
<span class="keyword">int</span> <span class="function">gcd</span>(<span class="keyword">int</span> a, <span class="keyword">int</span> b) {
<span class="keyword">while</span> (b != <span class="number">0</span>) {
<span class="keyword">int</span> temp = b;
b = a % b;
a = temp;
}
<span class="keyword">return</span> a;
}
<span class="comment">// Funcție pentru a calcula (a^b) % c</span>
<span class="keyword">long long</span> <span class="function">modExp</span>(<span class="keyword">long long</span> a, <span class="keyword">long long</span> b, <span class="keyword">long long</span> c) {
<span class="keyword">long long</span> result = <span class="number">1</span>;
a = a % c;
<span class="keyword">while</span> (b > <span class="number">0</span>) {
<span class="keyword">if</span> (b % <span class="number">2</span> == <span class="number">1</span>)
result = (result * a) % c;
b = b / <span class="number">2</span>;
a = (a * a) % c;
}
<span class="keyword">return</span> result;
}
<span class="comment">// Funcție pentru a găsi inversul modular al lui e mod phi(n) folosind metoda extinsă Euclid</span>
<span class="keyword">int</span> <span class="function">modInverse</span>(<span class="keyword">int</span> e, <span class="keyword">int</span> phi) {
<span class="keyword">int</span> t = <span class="number">0</span>, newT = <span class="number">1</span>;
<span class="keyword">int</span> r = phi, newR = e;
<span class="keyword">while</span> (newR != <span class="number">0</span>) {
<span class="keyword">int</span> quotient = r / newR;
<span class="keyword">int</span> tempT = t;
t = newT;
newT = tempT - quotient * newT;
<span class="keyword">int</span> tempR = r;
r = newR;
newR = tempR - quotient * newR;
}
<span class="keyword">if</span> (t < <span class="number">0</span>)
t += phi;
<span class="keyword">return</span> t;
}
<span class="keyword">int</span> <span class="function">main</span>() {
srand(time(<span class="number">0</span>));
<span class="comment">// Alegerea a două numere prime (pentru simplificare, sunt numere mici)</span>
<span class="keyword">int</span> p = <span class="number">61</span>;
<span class="keyword">int</span> q = <span class="number">53</span>;
<span class="comment">// Calcularea lui n = p * q</span>
<span class="keyword">int</span> n = p * q;
<span class="comment">// Calcularea funcției Euler φ(n) = (p-1) * (q-1)</span>
<span class="keyword">int</span> phi = (p - <span class="number">1</span>) * (q - <span class="number">1</span>);
<span class="comment">// Alegerea cheii publice e, unde 1 < e < φ(n) și gcd(e, φ(n)) = 1</span>
<span class="keyword">int</span> e;
<span class="keyword">do</span> {
e = rand() % (phi - <span class="number">2</span>) + <span class="number">2</span>; <span class="comment">// e este ales aleator între 2 și phi - 1</span>
} <span class="keyword">while</span> (<span class="function">gcd</span>(e, phi) != <span class="number">1</span>);
<span class="comment">// Calcularea cheii private d, astfel încât (d * e) % φ(n) = 1</span>
<span class="keyword">int</span> d = <span class="function">modInverse</span>(e, phi);
<span class="comment">// Afișarea cheilor publice și private</span>
<span class="function">cout</span> << <span class="string">"Cheia publică: ("</span> << e << <span class="string">", "</span> << n << <span class="string">")"</span> << <span class="function">endl</span>;
<span class="function">cout</span> << <span class="string">"Cheia privată: ("</span> << d << <span class="string">", "</span> << n << <span class="string">")"</span> << <span class="function">endl</span>;
<span class="comment">// Mesajul de criptat (un număr mai mic decât n)</span>
<span class="keyword">int</span> mesaj;
<span class="function">cout</span> << <span class="string">"Introdu un mesaj numeric (mai mic decât "</span> << n << <span class="string">"):"</span>;
<span class="function">cin</span> >> mesaj;
<span class="comment">// Criptarea mesajului: C = M^e % n</span>
<span class="keyword">int</span> criptat = <span class="function">modExp</span>(mesaj, e, n);
<span class="function">cout</span> << <span class="string">"Mesaj criptat: "</span> << criptat << <span class="function">endl</span>;
<span class="comment">// Decriptarea mesajului: M = C^d % n</span>
<span class="keyword">int</span> decriptat = <span class="function">modExp</span>(criptat, d, n);
<span class="function">cout</span> << <span class="string">"Mesaj decriptat: "</span> << decriptat << <span class="function">endl</span>;
<span class="keyword">return</span> <span class="number">0</span>;
}
</code></pre>
</div>
<h2>Codul Python:</h2>
<button class="accordion">RSA</button>
<div class="panel">
<pre><code>
<span class="keyword">def</span> <span class="function">gcd</span>(a, b):
<span class="comment"># Funcție pentru calculul celui mai mare divizor comun</span>
<span class="keyword">while</span> b != <span class="number">0</span>:
a, b = b, a % b
<span class="keyword">return</span> a
<span class="keyword">def</span> <span class="function">mod_inverse</span>(a, m):
<span class="comment"># Funcție pentru calculul inversului modular</span>
m0, x0, x1 = m, <span class{number}>0</span>, <span class{number}>1</span>
<span class="keyword">while</span> a > <span{number}>1</span>:
q = a // m
m, a = a % m, m
x0, x1 = x1 - q * x0, x0
<span class="keyword">return</span> x1 + m0 <span class="keyword">if</span> x1 < <span class="number">0</span> <span class="keyword">else</span> x1
<span class="keyword">def</span> <span class="function">rsa_encrypt</span>(plaintext, e, n):
<span class="comment"># Funcție pentru criptarea mesajului folosind RSA</span>
<span class="keyword">return</span> pow(plaintext, e, n)
<span class="keyword">def</span> <span class="function">rsa_decrypt</span>(ciphertext, d, n):
<span class="comment"># Funcție pentru decriptarea mesajului folosind RSA</span>
<span class="keyword">return</span> pow(ciphertext, d, n)
<span class="keyword"># Exemplu de utilizare</span>
p = <span class="number">61</span>
q = <span class="number">53</span>
n = p * q
phi = (p - <span class="number">1</span>) * (q - <span class="number">1</span>)
e = <span class="number">17</span> <span class="keyword"># Cheia publică</span>
d = mod_inverse(e, phi)
message = <span class="number">65</span> <span class="comment"># Mesaj de criptat</span>
encrypted_message = rsa_encrypt(message, e, n)
print(<span class="string">"Mesaj criptat:"</span>, encrypted_message)
decrypted_message = rsa_decrypt(encrypted_message, d, n)
print(<span class="string">"Mesaj decriptat:"</span>, decrypted_message)
</code></pre>
</div>
</div>
<!--Blowfish-->
<div style="padding: 20px;">
<h1>Algoritmul Blowfish în C++</h1>
<p>Acest cod C++ implementează algoritmul de criptare și decriptare Blowfish.</p>
<h2>Codul C++:</h2>
<button class="accordion">Blowfish</button>
<div class="panel">
<pre><code>
<span class="keyword">#include</span> <iostream>
<span class="keyword">#include</span> <vector>
<span class="keyword">#include</span> <cstring> <span class="comment">// pentru memcpy</span>
<span class="keyword">using namespace</span> std;
<span class="comment">// Dimensiunea unui bloc în Blowfish (în biți)</span>
<span class="keyword">const int</span> BLOCK_SIZE = <span class="number">8</span>;
<span class="comment">// S-Box-uri pentru Blowfish (inițializate cu valori fictive, ar trebui inițializate corect conform specificației)</span>
<span class="keyword">unsigned long</span> S[<span class="number">4</span>][<span class="number">256</span>] = {
{ <span class="comment">/* S-Box 1 valori */</span> },
{ <span class="comment">/* S-Box 2 valori */</span> },
{ <span class="comment">/* S-Box 3 valori */</span> },
{ <span class="comment">/* S-Box 4 valori */</span> }
};
<span class="comment">// P-array pentru Blowfish (valori inițiale, acestea vor fi modificate de cheie)</span>
<span class="keyword">unsigned long</span> P[<span class="number">18</span>] = {
<span class="number">0x243F6A88</span>, <span class="number">0x85A308D3</span>, <span class="number">0x13198A2E</span>, <span class="number">0x03707344</span>,
<span class="number">0xA4093822</span>, <span class="number">0x299F31D0</span>, <span class="number">0x082EFA98</span>, <span class="number">0xEC4E6C89</span>,
<span class="number">0x452821E6</span>, <span class="number">0x38D01377</span>, <span class="number">0xBE5466CF</span>, <span class="number">0x34E90C6C</span>,
<span class="number">0xC0AC29B7</span>, <span class="number">0xC97C50DD</span>, <span class="number">0x3F84D5B5</span>, <span class="number">0xB5470917</span>,
<span class="number">0x9216D5D9</span>, <span class="number">0x8979FB1B</span>
};
<span class="comment">// Funcția F a algoritmului Blowfish</span>
<span class="keyword">unsigned long</span> <span class="function">F</span>(<span class="keyword">unsigned long</span> x) {
<span class="keyword">unsigned short</span> a, b, c, d;
a = (x >> <span class="number">24</span>) & <span class="number">0xFF</span>;
b = (x >> <span class="number">16</span>) & <span class="number">0xFF</span>;
c = (x >> <span class="number">8</span>) & <span class="number">0xFF</span>;
d = x & <span class="number">0xFF</span>;
<span class="keyword">return</span> ((S[<span class="number">0</span>][a] + S[<span class="number">1</span>][b]) ^ S[<span class="number">2</span>][c]) + S[<span class="number">3</span>][d];
}
<span class="comment">// Cifrul Blowfish - rotirea blocurilor de date</span>
<span class="keyword">void</span> <span class="function">BlowfishEncrypt</span>(<span class="keyword">unsigned long</span> &left, <span class="keyword">unsigned long</span> &right) {
<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < <span class="number">16</span>; i++) {
left ^= P[i];
right ^= <span class="function">F</span>(left);
<span class="function">swap</span>(left, right);
}
<span class="function">swap</span>(left, right); <span class="comment">// Inversarea finală</span>
right ^= P[<span class="number">16</span>];
left ^= P[<span class="number">17</span>];
}
<span class="comment">// Decifrarea Blowfish - operația inversă cifrării</span>
<span class="keyword">void</span> <span class="function">BlowfishDecrypt</span>(<span class="keyword">unsigned long</span> &left, <span class="keyword">unsigned long</span> &right) {
<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">17</span>; i > <span class="number">1</span>; i--) {
left ^= P[i];
right ^= <span class="function">F</span>(left);
<span class="function">swap</span>(left, right);
}
<span class="function">swap</span>(left, right); <span class="comment">// Inversarea finală</span>
right ^= P[<span class="number">1</span>];
left ^= P[<span class="number">0</span>];
}
<span class="comment">// Inițializarea cheii în Blowfish</span>
<span class="keyword">void</span> <span class="function">BlowfishKeyExpansion</span>(<span class="keyword">const vector<unsigned char></span> &key) {
<span class="keyword">int</span> keyIndex = <span class="number">0</span>;
<span class="comment">// XOR fiecare valoare din P-array cu cheia</span>
<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < <span class="number">18</span>; i++) {
<span class="keyword">unsigned long</span> data = <span class="number">0x00000000</span>;
<span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j < <span class="number">4</span>; j++) {
data = (data << <span class="number">8</span>) | key[keyIndex];
keyIndex = (keyIndex + <span class="number">1</span>) % key.size();
}
P[i] ^= data;
}
<span class="comment">// Criptarea blocurilor nule pentru a finaliza inițializarea</span>
<span class="keyword">unsigned long</span> left = <span class="number">0x00000000</span>;
<span class="keyword">unsigned long</span> right = <span class="number">0x00000000</span>;
<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < <span class="number">18</span>; i += <span class="number">2</span>) {
<span class="function">BlowfishEncrypt</span>(left, right);
P[i] = left;
P[i + <span class="number">1</span>] = right;
}
<span class="comment">// Inițializarea S-box-urilor (aceeași tehnică folosită pentru P-array)</span>
<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < <span class="number">4</span>; i++) {
<span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j < <span class="number">256</span>; j += <span class="number">2</span>) {
<span class="function">BlowfishEncrypt</span>(left, right);
S[i][j] = left;
S[i][j + <span class="number">1</span>] = right;
}
}
}
<span class="comment">// Funcție pentru a cripta un bloc de 64 de biți (8 octeți)</span>
<span class="keyword">void</span> <span class="function">EncryptBlock</span>(<span class="keyword">unsigned char</span> *block) {
<span class="keyword">unsigned long</span> left, right;
<span class="function">memcpy</span>(&left, block, <span class="number">4</span>); <span class="comment">// Primele 4 octeți -> partea stângă</span>
<span class="function">memcpy</span>(&right, block + <span class="number">4</span>, <span class="number">4</span>); <span class="comment">// Următorii 4 octeți -> partea dreaptă</span>
<span class="function">BlowfishEncrypt</span>(left, right);
<span class="function">memcpy</span>(block, &left, <span class="number">4</span>);
<span class="function">memcpy</span>(block + <span class="number">4</span>, &right, <span class="number">4</span>);
}
<span class="comment">// Funcție pentru a decripta un bloc de 64 de biți (8 octeți)</span>
<span class="keyword">void</span> <span class="function">DecryptBlock</span>(<span class="keyword">unsigned char</span> *block) {
<span class="keyword">unsigned long</span> left, right;
<span class="function">memcpy</span>(&left, block, <span class="number">4</span>); <span class="comment">// Primele 4 octeți -> partea stângă</span>
<span class="function">memcpy</span>(&right, block + <span class="number">4</span>, <span class="number">4</span>); <span class="comment">// Următorii 4 octeți -> partea dreaptă</span>
<span class="function">BlowfishDecrypt</span>(left, right);
<span class="function">memcpy</span>(block, &left, <span class="number">4</span>);
<span class="function">memcpy</span>(block + <span class="number">4</span>, &right, <span class="number">4</span>);
}
<span class="keyword">int</span> <span class="function">main</span>() {
<span class="comment">// Cheia de criptare (exemplu)</span>
<span class="keyword">vector<unsigned char></span> key = { <span class="string">'K'</span>, <span class="string">'e'</span>, <span class="string">'y'</span>, <span class="string">'S'</span>, <span class="string">'e'</span>, <span class="string">'c'</span>, <span class="string">'r'</span>, <span class="string">'e'</span>, <span class="string">'t'</span> };
<span class="comment">// Expansiunea cheii Blowfish</span>
<span class="function">BlowfishKeyExpansion</span>(key);
<span class="comment">// Exemplu de bloc de 8 octeți (64 de biți) de criptat</span>
<span class="keyword">unsigned char</span> block[BLOCK_SIZE] = { <span class="string">'T'</span>, <span class="string">'e'</span>, <span class="string">'s'</span>, <span class="string">'t'</span>, <span class="string">'B'</span>, <span class="string">'l'</span>, <span class="string">'o'</span>, <span class="string">'c'</span> };
<span class="comment">// Afișarea blocului înainte de criptare</span>
<span class="function">cout</span> << <span class="string">"Bloc înainte de criptare: "</span>;
<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < BLOCK_SIZE; i++) {
<span class="function">cout</span> << block[i];
}
<span class="function">cout</span> << <span class="function">endl</span>;
<span class="comment">// Criptarea blocului</span>
<span class="function">EncryptBlock</span>(block);
<span class="comment">// Afișarea blocului după criptare</span>
<span class="function">cout</span> << <span class="string">"Bloc după criptare: "</span>;
<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < BLOCK_SIZE; i++) {
<span class="function">printf</span>(<span class="string">"%02X "</span>, block[i]);
}
<span class="function">cout</span> << <span class="function">endl</span>;
<span class="comment">// Decriptarea blocului</span>
<span class="function">DecryptBlock</span>(block);
<span class="comment">// Afișarea blocului după decriptare</span>
<span class="function">cout</span> << <span class="string">"Bloc după decriptare: "</span>;
<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < BLOCK_SIZE; i++) {
<span class="function">cout</span> << block[i];
}
<span class="function">cout</span> << <span class="function">endl</span>;
<span class="keyword">return</span> <span class="number">0</span>;
}
</code></pre>
</div>
<h2>Codul Python:</h2>
<button class="accordion">Blowfish</button>
<div class="panel">
<pre><code>
<span class="keyword">from</span> Crypto.Cipher <span class="keyword">import</span> Blowfish
<span class="keyword">from</span> Crypto.Util.Padding <span class="keyword">import</span> pad, unpad
<span class="keyword">def</span> <span class="function">blowfish_encrypt</span>(key, plaintext):
<span class="comment"># Funcție de criptare folosind Blowfish</span>
cipher = Blowfish.new(key, Blowfish.MODE_CBC)
iv = cipher.iv
ciphertext = cipher.encrypt(pad(plaintext.encode(), Blowfish.block_size))
<span class="keyword">return</span> iv + ciphertext
<span class="keyword">def</span> <span class="function">blowfish_decrypt</span>(key, ciphertext):
<span class="comment"># Funcție de decriptare folosind Blowfish</span>
iv = ciphertext[:Blowfish.block_size]
cipher = Blowfish.new(key, Blowfish.MODE_CBC, iv)
decrypted = unpad(cipher.decrypt(ciphertext[Blowfish.block_size:]), Blowfish.block_size)
<span class="keyword">return</span> decrypted.decode()
<span class="keyword"># Exemplu de utilizare</span>
key = b'SecretK' <span class="comment"># Cheie de criptare</span>
plaintext = <span class="string">"Mesaj de test."</span>
ciphertext = blowfish_encrypt(key, plaintext)
print(<span class="string">"Mesaj criptat:"</span>, ciphertext)
decrypted_message = blowfish_decrypt(key, ciphertext)
print(<span class="string">"Mesaj decriptat:"</span>, decrypted_message)
</code></pre>
</div>
</div>
<!--AES-->
<div style="padding: 20px;">
<h1>Algoritmul de criptare AES în C++</h1>
<p>Acest cod C++ implementează un algoritm AES pentru criptare și decriptare a textului.</p>
<h2>Codul C++:</h2>
<button class="accordion">AES</button>
<div class="panel">
<pre><code>
<span class="keyword">#include</span> <iostream>
<span class="keyword">#include</span> <string>
<span class="keyword">#include</span> <openssl/aes.h>
<span class="keyword">#include</span> <openssl/rand.h>
<span class="keyword">using namespace</span> std;
<span class="comment">// Funcție pentru criptare AES</span>
<span class="keyword">void</span> <span class="function">aesEncrypt</span>(<span class="keyword">const unsigned char*</span> input, <span class="keyword">unsigned char*</span> output, <span class="keyword">const unsigned char*</span> key) {
AES_KEY encryptKey;
AES_set_encrypt_key(key, <span class="number">128</span>, &encryptKey); <span class="comment">// Setăm cheia AES</span>
AES_encrypt(input, output, &encryptKey); <span class="comment">// Criptăm folosind AES</span>
}
<span class="comment">// Funcție pentru decriptare AES</span>
<span class="keyword">void</span> <span class="function">aesDecrypt</span>(<span class="keyword">const unsigned char*</span> input, <span class="keyword">unsigned char*</span> output, <span class="keyword">const unsigned char*</span> key) {
AES_KEY decryptKey;
AES_set_decrypt_key(key, <span class="number">128</span>, &decryptKey); <span class="comment">// Setăm cheia AES pentru decriptare</span>
AES_decrypt(input, output, &decryptKey); <span class="comment">// Decriptăm folosind AES</span>
}
<span class="keyword">int</span> <span class="function">main</span>() {
<span class="comment">// Cheia trebuie să fie de exact 16 octeți pentru AES-128</span>
<span class="keyword">unsigned char</span> key[AES_BLOCK_SIZE] = {<span class="number">0x01</span>, <span class="number">0x02</span>, <span class="number">0x03</span>, <span class="number">0x04</span>,
<span class="number">0x05</span>, <span class="number">0x06</span>, <span class="number">0x07</span>, <span class="number">0x08</span>,
<span class="number">0x09</span>, <span class="number">0x0A</span>, <span class="number">0x0B</span>, <span class="number">0x0C</span>,
<span class="number">0x0D</span>, <span class="number">0x0E</span>, <span class="number">0x0F</span>, <span class="number">0x10</span>};
<span class="keyword">unsigned char</span> input[AES_BLOCK_SIZE] = {<span class="string">'T'</span>, <span class="string">'e'</span>, <span class="string">'s'</span>, <span class="string">'t'</span>,
<span class="string">'I'</span>, <span class="string">'n'</span>, <span class="string">'p'</span>, <span class="string">'u'</span>,
<span class="string">'t'</span>, <span class="string">'1'</span>, <span class="string">'2'</span>, <span class="string">'3'</span>,
<span class="string">'4'</span>, <span class="string">'5'</span>, <span class="string">'6'</span>, <span class="string">'7'</span>};
<span class="keyword">unsigned char</span> encryptedOutput[AES_BLOCK_SIZE];
<span class="keyword">unsigned char</span> decryptedOutput[AES_BLOCK_SIZE];
<span class="comment">// Criptare</span>
aesEncrypt(input, encryptedOutput, key);
<span class="function">cout</span> << <span class="string">"Mesaj criptat: "</span>;
<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < AES_BLOCK_SIZE; i++) {
<span class="function">printf</span>(<span class="string">"%02x "</span>, encryptedOutput[i]);
}
<span class="function">cout</span> << <span class="function">endl</span>;
<span class="comment">// Decriptare</span>
aesDecrypt(encryptedOutput, decryptedOutput, key);
<span class="function">cout</span> << <span class="string">"Mesaj decriptat: "</span>;
<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < AES_BLOCK_SIZE; i++) {
<span class="function">cout</span> << decryptedOutput[i];
}
<span class="function">cout</span> << <span class="function">endl</span>;
<span class="keyword">return</span> <span class="number">0</span>;
}
</code></pre>
</div>
<h2>Codul Python:</h2>
<button class="accordion">AES</button>
<div class="panel">
<pre><code>
<span class="keyword">from</span> Crypto.Cipher <span class="keyword">import</span> AES
<span class="keyword">from</span> Crypto.Util.Padding <span class="keyword">import</span> pad, unpad
<span class="keyword">def</span> <span class="function">aes_encrypt</span>(key, plaintext):
<span class="comment"># Funcție de criptare folosind AES</span>
cipher = AES.new(key, AES.MODE_CBC)
iv = cipher.iv
ciphertext = cipher.encrypt(pad(plaintext.encode(), AES.block_size))
<span class="keyword">return</span> iv + ciphertext
<span class="keyword">def</span> <span class="function">aes_decrypt</span>(key, ciphertext):
<span class="comment"># Funcție de decriptare folosind AES</span>
iv = ciphertext[:AES.block_size]
cipher = AES.new(key, AES.MODE_CBC, iv)
decrypted = unpad(cipher.decrypt(ciphertext[AES.block_size:]), AES.block_size)
<span class="keyword">return</span> decrypted.decode()
<span class="keyword"># Exemplu de utilizare</span>
key = b'Sixteen byte key' <span class="comment"># Cheie de 16 bytes pentru AES</span>
plaintext = <span class="string">"Mesaj de test."</span>
ciphertext = aes_encrypt(key, plaintext)
print(<span class="string">"Mesaj criptat:"</span>, ciphertext)
decrypted_message = aes_decrypt(key, ciphertext)
print(<span class="string">"Mesaj decriptat:"</span>, decrypted_message)
</code></pre>
</div>
</div>
<!--ECC-->
<div style="padding: 20px;">
<h1>Algoritmul de criptare ECC în C++</h1>
<p>Acest cod C++ implementează ECC pentru criptare și decriptare a textului.</p>
<h2>Codul C++:</h2>
<button class="accordion">ECC</button>
<div class="panel">
<pre><code>
<span class="keyword">#include</span> <iostream>
<span class="keyword">#include</span> <utility> <span class="comment">// pentru std::pair</span>
<span class="keyword">#include</span> <cmath> <span class="comment">// pentru funcții matematice de bază</span>
<span class="keyword">using namespace</span> std;
<span class="comment">// Structură pentru a reprezenta punctele pe curba eliptică</span>
<span class="keyword">struct</span> Point {
<span class="keyword">long long</span> x, y;
};
<span class="comment">// Parametrii curbei eliptice</span>
<span class="keyword">struct</span> CurveParams {
<span class="keyword">long long</span> a, b, p; <span class="comment">// a și b sunt coeficienții curbei y^2 = x^3 + ax + b, p este câmpul finit</span>
};
<span class="comment">// Funcție pentru a calcula inversul modular folosind Algoritmul Euclidian Extins</span>
<span class="keyword">long long</span> <span class="function">modInverse</span>(<span class="keyword">long long</span> k, <span class="keyword">long long</span> p) {
<span class="keyword">long long</span> t = <span class="number">0</span>, newT = <span class="number">1</span>;
<span class="keyword">long long</span> r = p, newR = k;
<span class="keyword">while</span> (newR != <span class="number">0</span>) {
<span class="keyword">long long</span> quotient = r / newR;
<span class="comment">// Actualizăm t și newT fără a folosi tie()</span>
<span class="keyword">long long</span> tempT = newT;
newT = t - quotient * newT;
t = tempT;
<span class="comment">// Actualizăm r și newR fără tie()</span>
<span class="keyword">long long</span> tempR = newR;
newR = r - quotient * newR;
r = tempR;
}
<span class="keyword">if</span> (r > <span class="number">1</span>) <span class="keyword">return</span> <span class="number">-1</span>; <span class="comment">// Invers nu există</span>
<span class="keyword">if</span> (t < <span class="number">0</span>) t += p; <span class="comment">// Ajustăm valoarea t dacă este negativă</span>
<span class="keyword">return</span> t;
}
<span class="comment">// Funcția pentru adunarea a două puncte pe o curbă eliptică</span>
<span class="keyword">Point</span> <span class="function">addPoints</span>(<span class="keyword">Point</span> P, <span class="keyword">Point</span> Q, <span class="keyword">CurveParams</span> curve) {
<span class="keyword">if</span> (P.x == Q.x && P.y == Q.y) {
<span class="keyword">long long</span> s = (<span class="number">3</span> * P.x * P.x + curve.a) * <span class="function">modInverse</span>(<span class="number">2</span> * P.y, curve.p) % curve.p;
<span class="keyword">long long</span> x_r = (s * s - <span class="number">2</span> * P.x) % curve.p;
<span class="keyword">long long</span> y_r = (s * (P.x - x_r) - P.y) % curve.p;
<span class="comment">// Ajustăm dacă rezultatul este negativ</span>
<span class="keyword">if</span> (x_r < <span class="number">0</span>) x_r += curve.p;
<span class="keyword">if</span> (y_r < <span class="number">0</span>) y_r += curve.p;
<span class="keyword">return</span> {x_r, y_r};
} <span class="keyword">else</span> {
<span class="keyword">long long</span> s = (Q.y - P.y) * <span class="function">modInverse</span>(Q.x - P.x, curve.p) % curve.p;
<span class="keyword">long long</span> x_r = (s * s - P.x - Q.x) % curve.p;
<span class="keyword">long long</span> y_r = (s * (P.x - x_r) - P.y) % curve.p;
<span class="comment">// Ajustăm dacă rezultatul este negativ</span>
<span class="keyword">if</span> (x_r < <span class="number">0</span>) x_r += curve.p;
<span class="keyword">if</span> (y_r < <span class="number">0</span>) y_r += curve.p;
<span class="keyword">return</span> {x_r, y_r};
}
}
<span class="comment">// Funcția pentru înmulțirea unui punct cu un scalar pe o curbă eliptică</span>
<span class="keyword">Point</span> <span class="function">scalarMult</span>(<span class="keyword">Point</span> P, <span class="keyword">long long</span> k, <span class="keyword">CurveParams</span> curve) {
<span class="keyword">Point</span> result = P;
k = k - <span class="number">1</span>; <span class="comment">// Deja avem un punct, deci repetăm k-1 ori adunarea</span>
<span class="keyword">while</span> (k > <span class="number">0</span>) {
result = <span class="function">addPoints</span>(result, P, curve);
k--;
}
<span class="keyword">return</span> result;
}
<span class="comment">// Funcția principală</span>
<span class="keyword">int</span> <span class="function">main</span>() {
<span class="comment">// Definim o curbă eliptică simplă: y^2 = x^3 + ax + b (mod p)</span>
<span class="keyword">CurveParams</span> curve = {<span class="number">2</span>, <span class="number">3</span>, <span class="number">97</span>}; <span class="comment">// Exemplu: y^2 = x^3 + 2x + 3 (mod 97)</span>
<span class="comment">// Punctul generator G de pe curbă</span>
<span class="keyword">Point</span> G = {<span class="number">3</span>, <span class="number">6</span>}; <span class="comment">// Exemplu de punct de pe curbă</span>
<span class="comment">// Cheia privată a destinatarului</span>
<span class="keyword">long long</span> privateKey = <span class="number">7</span>; <span class="comment">// Exemplu de cheie privată</span>
<span class="comment">// Cheia publică a destinatarului: PublicKey = privateKey * G</span>
<span class="keyword">Point</span> publicKey = <span class="function">scalarMult</span>(G, privateKey, curve);
<span class="comment">// Expeditorul alege o cheie aleatorie k pentru criptare</span>
<span class="keyword">long long</span> k = <span class="number">5</span>; <span class="comment">// Cheie aleatorie</span>
<span class="comment">// Calcularea punctului R = k * G</span>
<span class="keyword">Point</span> R = <span class="function">scalarMult</span>(G, k, curve);
<span class="comment">// Mesajul de criptat (reprezentat ca punct pe curba eliptică)</span>
<span class="keyword">Point</span> M = {<span class="number">10</span>, <span class="number">22</span>}; <span class="comment">// Exemplu de mesaj ca punct de pe curbă</span>
<span class="comment">// Criptarea mesajului: C1 = R, C2 = M + k * publicKey</span>
<span class="keyword">Point</span> C1 = R;
<span class="keyword">Point</span> C2 = <span class="function">addPoints</span>(M, <span class="function">scalarMult</span>(publicKey, k, curve), curve);
<span class="comment">// Afișăm rezultatele criptării</span>
<span class="function">cout</span> << <span class="string">"Mesaj criptat:"</span> << <span class="function">endl</span>;
<span class="function">cout</span> << <span class="string">"C1: ("</span> << C1.x << <span class="string">", "</span> << C1.y << <span class="string">")"</span> << <span class="function">endl</span>;
<span class="function">cout</span> << <span class="string">"C2: ("</span> << C2.x << <span class="string">", "</span> << C2.y << <span class="string">")"</span> << <span class="function">endl</span>;
<span class="comment">// Decriptarea mesajului: M = C2 - privateKey * C1</span>
<span class="keyword">Point</span> decryptedMessage = <span class="function">addPoints</span>(C2, <span class="function">scalarMult</span>(C1, privateKey, curve), curve);
<span class="function">cout</span> << <span class="string">"Mesaj decriptat: ("</span> << decryptedMessage.x << <span class="string">", "</span> << decryptedMessage.y << <span class="string">")"</span> << <span class="function">endl</span>;
<span class="keyword">return</span> <span class="number">0</span>;
}
</code></pre>
</div>
<h2>Codul Python:</h2>
<button class="accordion">ECC</button>
<div class="panel">
<pre><code>
<span class="keyword"># Definirea unei clase pentru a reprezenta punctele pe curba eliptică</span>
<span class="keyword">class</span> Point:
<span class="keyword">def</span> <span class="function">__init__</span>(self, x, y):
self.x = x
self.y = y
<span class="keyword"># Parametrii curbei eliptice</span>
<span class="keyword">class</span> CurveParams:
<span class="keyword">def</span> <span class="function">__init__</span>(self, a, b, p):
self.a = a <span class="comment"># Coeficientul 'a' al curbei</span>
self.b = b <span class="comment"># Coeficientul 'b' al curbei</span>
self.p = p <span class="comment"># Modulo p (câmp finit)</span>
<span class="keyword"># Funcție pentru a calcula inversul modular folosind Algoritmul Euclidian Extins</span>
<span class="keyword">def</span> <span class="function">mod_inverse</span>(k, p):
t, new_t = 0, 1
r, new_r = p, k
<span class="keyword">while</span> new_r != 0:
quotient = r // new_r
t, new_t = new_t, t - quotient * new_t
r, new_r = new_r, r - quotient * new_r
<span class="keyword">if</span> r > 1:
<span class="keyword">return</span> -1 <span class="comment"># Invers nu există</span>
<span class="keyword">if</span> t < 0:
t += p
<span class="keyword">return</span> t
<span class="keyword"># Funcție pentru adunarea a două puncte pe o curbă eliptică</span>
<span class="keyword">def</span> <span class="function">add_points</span>(P, Q, curve):
<span class="keyword">if</span> P.x == Q.x and P.y == Q.y:
s = (3 * P.x * P.x + curve.a) * <span class="function">mod_inverse</span>(2 * P.y, curve.p) % curve.p
x_r = (s * s - 2 * P.x) % curve.p
y_r = (s * (P.x - x_r) - P.y) % curve.p
<span class="keyword">else</span>:
s = (Q.y - P.y) * <span class="function">mod_inverse</span>(Q.x - P.x, curve.p) % curve.p
x_r = (s * s - P.x - Q.x) % curve.p
y_r = (s * (P.x - x_r) - P.y) % curve.p
<span class="keyword">if</span> x_r < 0:
x_r += curve.p
<span class="keyword">if</span> y_r < 0:
y_r += curve.p
<span class="keyword">return</span> Point(x_r, y_r)
<span class="keyword"># Funcție pentru înmulțirea unui punct cu un scalar pe o curbă eliptică</span>
<span class="keyword">def</span> <span class="function">scalar_mult</span>(P, k, curve):
result = P
k -= 1
<span class="keyword">while</span> k > 0:
result = <span class="function">add_points</span>(result, P, curve)
k -= 1
<span class="keyword">return</span> result
<span class="keyword"># Funcția principală</span>
<span class="keyword">if</span> __name__ == "<span class="string">__main__</span>":
<span class="comment"># Definim o curbă eliptică simplă: y^2 = x^3 + ax + b (mod p)</span>
curve = CurveParams(2, 3, 97) <span class="comment"># Exemplu: y^2 = x^3 + 2x + 3 (mod 97)</span>
<span class="comment"># Punctul generator G de pe curbă</span>
G = Point(3, 6)
<span class="comment"># Cheia privată a destinatarului</span>
private_key = 7 <span class="comment"># Exemplu de cheie privată</span>
<span class="comment"># Cheia publică a destinatarului: PublicKey = privateKey * G</span>
public_key = <span class="function">scalar_mult</span>(G, private_key, curve)
<span class="comment"># Expeditorul alege o cheie aleatorie k pentru criptare</span>
k = 5
<span class="comment"># Calcularea punctului R = k * G</span>
R = <span class="function">scalar_mult</span>(G, k, curve)
<span class="comment"># Mesajul de criptat (reprezentat ca punct pe curba eliptică)</span>
M = Point(10, 22)
<span class="comment"># Criptarea mesajului: C1 = R, C2 = M + k * publicKey</span>
C1 = R
C2 = <span class="function">add_points</span>(M, <span class="function">scalar_mult</span>(public_key, k, curve), curve)
<span class="comment"># Afișăm rezultatele criptării</span>
<span class="function">print</span>(<span class="string">"Mesaj criptat:"</span>)