-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaesar_Cipher.java
More file actions
1296 lines (864 loc) · 34.6 KB
/
Copy pathCaesar_Cipher.java
File metadata and controls
1296 lines (864 loc) · 34.6 KB
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
package Package;
import java.text.Normalizer;
import java.text.Normalizer.Form;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.IntStream;
@SuppressWarnings("unused")
public class Caesar_Cipher_Final {
static char[] Alphab = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
static char[] Rev_Alphab = { 'z', 'y', 'x', 'w', 'v', 'u', 't', 's', 'r', 'q', 'p', 'o', 'n', 'm', 'l', 'k', 'j',
'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a' };
static char[] Cap_Alphab = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
static char[] Rev_Cap_Alphab = { 'Z', 'Y', 'X', 'W', 'V', 'U', 'T', 'S', 'R', 'Q', 'P', 'O', 'N', 'M', 'L', 'K',
'J', 'I', 'H', 'G', 'F', 'E', 'D', 'C', 'B', 'A' };
static int[] Numerical = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
static int[] Rev_Numerical = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
// private static char[] Alpha_Numeric = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
// 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
// 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4',
// '5', '6', '7', '8', '9' };
static char[] Symbole;
static char[] Rev_Symbole;
// { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }
// h e l l o M o n k e
// 8 5 12 12 16 13 16 14 11 5
// 1 2 3 4 5 6 7 8 9 10
// |
//
// 1{ '7', '1', '2', '3', '4', '5', '6', '0', '8', '9' }
//
// 2{ '7', '4', '2', '3', '1', '5', '6', '0', '8', '9' }
//
// 3{ '7', '2', '4', '3', '1', '5', '6', '0', '8', '9' }
//
// 4{ '7', '3', '5', '1', '2', '4', '6', '0', '8', '9' }
//
// 5{ '7', '3', '5', '1','4' , '2', '6', '0', '8', '9' }
//
// 6{ '7', '3', '2' ,'1','4' , '5', '6', '0', '8', '9' }
//
// 7{ '7', '3', '2' ,'1','4' , '6', '5', '0', '8', '9' }
//
// 8{ '8', '3', '2' ,'0','4' , '6', '5', '1', '7', '9' }
//
// 9{ '8', '3', '2' ,'0','4' , '6', '5', '1', '7', '9' }
//
//
//
// SHUFLE THE POSITION BY THE VALUE OF THE LETTER
//_________________________________________________________________________________________________________
public static void main(String[] args) {
// TODO Auto-generated method stub
String key = "Izakenaz12&";
String Source = "test123&&& 、ⅲ#┅抖兑乙乙鬃鬃鬃揶揶揶�";
Symbole = Generate_Symboles();
// System.out.println(Arrays.toString(Symbole));
System.out.println(Symbole.length);
Shuflle_Num_Alpha_Symb(key);
String temp = Encryp(Source, key);
temp = String_Shuflle(temp, key);
temp = String_UnShuflle(temp, key);
String temp1 = DeEncryp(temp, key);
if (Source.equals(temp1)) {
System.err.println("Yes");
} else {
System.err.println("No");
System.out.println(Source);
System.err.println(temp1);
}
}
public final static String Encryp(String Source, String Secret_Word) {
// TODO Auto-generated method stub
// String Secret_Word = "HelloMonkey//3";
// String Source = "0123456789Monkey is a common name that may refer to most mammals of the infraorder Simiiformes, "
// + "also known as the simians. Traditionally, all animals in the group now known as simians are"
// + " counted as monkeys except the apes, a grouping known as paraphyletic; however, in the broader"
// + " sense based on cladistics, apes (Hominoidea) are also included, making the terms monkeys and"
// + " simians synonyms in regards to their scope.[citation needed][3] ";
Symbole = Generate_Symboles();
// System.out.println(Symbole.length);
Shuflle_Num_Alpha_Symb(Secret_Word);
char[] key = (Clean_Cipher_Key(Secret_Word));
// System.out.println(key);
char[] Chiper_Ref = (Creat_Cipher_Ref(key, Alphab));
// System.out.println(Chiper_Ref);
String Final = Apply_Alpha_Cipher(Source, Chiper_Ref);
// System.out.println(Final);
int[] num_key = Clean_Cipher_Numerical_Key(Secret_Word);
// System.out.println(Arrays.toString(num_key));
int[] num_ref = Creat_Cipher_Numerical_Ref(num_key, Numerical);
// System.out.println(Arrays.toString(num_ref));
String Final_Num = Apply_Numerical_Cipher(Final, num_ref);
// System.out.println(Final_Num);
int[] symbole_key = Clean_Cipher_Symbole_Key(Secret_Word);
// System.out.println(Arrays.toString(symbole_key));
char[] symbole_Ref = Creat_Cipher_Symbole_Ref(symbole_key, Symbole);
// System.out.println(Arrays.toString(symbole_Ref));
String Final_Sym = Apply_Symbole_Cipher(Final_Num, symbole_Ref);
// Final_Sym = String_Shuflle(Final_Sym, Secret_Word);
Final_Sym = String_Shuflle(Final_Sym, Secret_Word);
return Final_Sym;
}
public final static String DeEncryp(String Source, String Secret_Word) {
// TODO Auto-generated method stub
// |||====================================Solving=====================================|||
Symbole = Generate_Symboles();
// System.out.println(Symbole.length);
Shuflle_Num_Alpha_Symb(Secret_Word);
Source = String_UnShuflle(Source, Secret_Word);
int[] symbole_key = Clean_Cipher_Symbole_Key(Secret_Word);
char[] key = (Clean_Cipher_Key(Secret_Word));
int[] num_key = Clean_Cipher_Numerical_Key(Secret_Word);
// Source = String_UnShuflle(Source, Secret_Word);
String Solved_Sym = Reverse_Symbole_Cipher(Source, symbole_key);
// System.out.println(Solved_Sym);
String Solved_Num = Reverse_Numerical_Cipher(Solved_Sym, num_key);
// System.out.println(Solved_Num);
String Solver = Reverse_Alpha_Cipher(Solved_Num, key);
// System.out.println(Solver);
return Solver;
}
private final static void Combined_Types_Test() {
String Secret_Word = "HelloMonkey//3";
String test = "0123456789Monkey is a common name that may refer to most mammals of the infraorder Simiiformes, "
+ "also known as the simians. Traditionally, all animals in the group now known as simians are"
+ " counted as monkeys except the apes, a grouping known as paraphyletic; however, in the broader"
+ " sense based on cladistics, apes (Hominoidea) are also included, making the terms monkeys and"
+ " simians synonyms in regards to their scope.[citation needed][3] ";
char[] key = (Clean_Cipher_Key(Secret_Word));
// System.out.println(key);
char[] Chiper_Ref = (Creat_Cipher_Ref(key, Alphab));
// System.out.println(Chiper_Ref);
String Final = Apply_Alpha_Cipher(test, Chiper_Ref);
// System.out.println(Final);
int[] num_key = Clean_Cipher_Numerical_Key(Secret_Word);
// System.out.println(Arrays.toString(num_key));
int[] num_ref = Creat_Cipher_Numerical_Ref(num_key, Numerical);
// System.out.println(Arrays.toString(num_ref));
String Final_Num = Apply_Numerical_Cipher(Final, num_ref);
// System.out.println(Final_Num);
Symbole = Generate_Symboles();
// System.out.println(Symbole.length);
int[] symbole_key = Clean_Cipher_Symbole_Key(Secret_Word);
// System.out.println(Arrays.toString(symbole_key));
char[] symbole_Ref = Creat_Cipher_Symbole_Ref(symbole_key, Symbole);
// System.out.println(Arrays.toString(symbole_Ref));
String Final_Sym = Apply_Symbole_Cipher(Final_Num, symbole_Ref);
// System.out.println(Final_Sym);
// System.out.println(Final_Sym);
// |||====================================Solving=====================================|||
String Solved_Sym = Reverse_Symbole_Cipher(Final_Sym, symbole_key);
// System.out.println(Solved_Sym);
String Solved_Num = Reverse_Numerical_Cipher(Solved_Sym, num_key);
// System.out.println(Solved_Num);
String Solver = Reverse_Alpha_Cipher(Solved_Num, key);
// System.out.println(Solver);
if (Solver.equals(test)) {
System.err.println("Identical !!");
} else {
System.out.println("________________________________________________");
System.err.println("Errore");
}
}
private final static void All_Types_Test() {
String Secret_Word = "HelloMonkey";
String test = "0123456789Monkey is a common name that may refer to most mammals of the infraorder Simiiformes, "
+ "also known as the simians. Traditionally, all animals in the group now known as simians are"
+ " counted as monkeys except the apes, a grouping known as paraphyletic; however, in the broader"
+ " sense based on cladistics, apes (Hominoidea) are also included, making the terms monkeys and"
+ " simians synonyms in regards to their scope.[citation needed][3] ";
char[] key = (Clean_Cipher_Key(Secret_Word));
// System.out.println(key);
char[] Chiper_Ref = (Creat_Cipher_Ref(key, Alphab));
// System.out.println(Chiper_Ref);
String Final = Apply_Alpha_Cipher(test, Chiper_Ref);
// System.out.println(Final);
String Solver = Reverse_Alpha_Cipher(Final, key);
// System.out.println(Solver);
if (Solver.equals(test)) {
System.err.println("Alpha Identical !!");
}
// System.out.println(Generate_Symboles().length);
int[] num_key = Clean_Cipher_Numerical_Key(Secret_Word);
// System.out.println(Arrays.toString(num_key));
int[] num_ref = Creat_Cipher_Numerical_Ref(num_key, Numerical);
// System.out.println(Arrays.toString(num_ref));
String Final_Num = Apply_Numerical_Cipher(test, num_ref);
// System.out.println(Final_Num);
String Solved_Num = Reverse_Numerical_Cipher(Final_Num, num_key);
// System.out.println(Solved_Num);
if (Solved_Num.equals(test)) {
System.err.println("Num Identical !!");
}
Symbole = Generate_Symboles();
// System.out.println(Symbole.length);
int[] symbole_key = Clean_Cipher_Symbole_Key(Secret_Word);
// System.out.println(Arrays.toString(symbole_key));
char[] symbole_Ref = Creat_Cipher_Symbole_Ref(symbole_key, Symbole);
// System.out.println(Arrays.toString(symbole_Ref));
String Final_Sym = Apply_Symbole_Cipher(test, symbole_Ref);
// System.out.println(Final_Sym);
String Solved_Sym = Reverse_Symbole_Cipher(Final_Sym, symbole_key);
// System.out.println(Solved_Sym);
if (Solved_Sym.equals(test)) {
System.err.println("Sym Identical !!");
}
}
// _________________________________________________________________________________________________________
private static void Shuflle_Num_Alpha_Symb(String Key) {
// Numerical =Numerical_Capsule();
// Symbole_Capsule_Char();
// System.out.println(Arrays.toString(Char_Array_To_Ascii_Array(Char_Numerical)));
// System.out.println(Arrays.toString(Char_Array_To_Int_Array(Char_Numerical)));
// System.out.println(Arrays.toString(Int_Array_To_Char_Array(Numerical)));
Numerical = Char_Array_To_Int_Array(Numerical_Capsule_Char(Int_Array_To_Char_Array(Numerical), Key));
Alphab = Numerical_Capsule_Char(Alphab, Key);
Symbole = Symbole_Capsule_Char(Symbole, Key);
}
private static void UnShuflle_Num_Alpha_Symb(String Key) {
Numerical = Char_Array_To_Int_Array(Reverse_Numerical_Capsule_Char(Int_Array_To_Char_Array(Numerical), Key));
Alphab = Reverse_Numerical_Capsule_Char(Alphab, Key);
Symbole = Reverse_Symbole_Capsule_Char(Symbole, Key);
}
// _________________________________________________________________________________________________________
private static String String_Shuflle(String Source, String Key) {
Source = new String(Numerical_Capsule_Char(Source.toCharArray(), Key));
return Source;
}
private static String String_UnShuflle(String Source, String Key) {
Source = new String(Reverse_Numerical_Capsule_Char(Source.toCharArray(), Key));
return Source;
}
// _________________________________________________________________________________________________________
private final static char[] Clean_Cipher_Key(String Source) {
// in this case we clean the password by removing every duplicate letters as in
// hello = helo
// any password biger than 26 get cut to it first 26 letters
// and all caps letters are turned to lower case
// example Hello123 => helo
String Original = Source;
Original = Original.toLowerCase();
if (Original.length() > Alphab.length) {
Original = Original.substring(0, Alphab.length);
}
// convert String to char[]
char[] String_to_char = Retrieve_Alphabets(Original);
// clear all the duplicates
// example : he lo
int ref = 0;
for (int i = 0; i < String_to_char.length; i++) {
int avoid = i;
for (int t = 0; t < String_to_char.length; t++) {
if (String_to_char[i] == String_to_char[t] && t != avoid && String_to_char[i] != ' ') {
String_to_char[t] = ' ';
ref++;
}
}
}
// Delete the created space
// example : helo
String Cleaned = "";
char[] Finaltchar = new char[String_to_char.length - ref];
int index = 0;
for (int i = 0; i < String_to_char.length; i++) {
if (String_to_char[i] != ' ') {
Cleaned += String_to_char[i];
Finaltchar[index] = String_to_char[i];
index++;
}
}
return Finaltchar;
}
private final static char[] Creat_Cipher_Ref(char[] key, char[] Ref) {
// this is ower password "helo" in its cleaned form
// we first start by removing every character in the password from the
// alphabetic reference
// abcdefghijklmnopqrstuvwxyz => abcdfgijkmnpqrstuvwxyz
// the we move the password cleaned alphabets to the end of the alphabetic
// reference
// >abcdfgijkmnpqrstuvwxyzhelo wich is ower new alphabetic reference
char[] alpha_Ref = Ref.clone();
// example : hello
// clear all the duplicates
// example : he lo
int ref = 0;
for (int i = 0; i < key.length; i++) {
for (int t = 0; t < alpha_Ref.length; t++) {
if (key[i] == alpha_Ref[t]) {
alpha_Ref[t] = ' ';
ref++;
}
}
}
// Delete the created space
// example : helo
String Cleaned = "";
char[] Finaltchar = new char[alpha_Ref.length - ref];
int index = 0;
for (int i = 0; i < alpha_Ref.length; i++) {
if (alpha_Ref[i] != ' ') {
Cleaned += alpha_Ref[i];
Finaltchar[index] = alpha_Ref[i];
index++;
}
}
char[] Final = new char[alpha_Ref.length];
for (int i = 0; i < Finaltchar.length; i++) {
Final[i] = Finaltchar[i];
}
for (int i = 0; i < key.length; i++) {
Final[i + Finaltchar.length] = key[i];
}
return Final;
}
private final static String Apply_Alpha_Cipher(String Source, char[] Cipher_Ref) {
// so lets say this is ower reference alphabetical "abcdfgijkmnpqrstuvwxyzhelo"
// now we start making all caps form of it to be able to handle upper case
// characters
// then we iterate throw every character of ower text
// lets say ower text is "hello Monkey"
// the first letter is h and h is the 7th letter of the alphabet
// now if we go to the cipher alpha ref and we look for the 7th letter
// it becomes j as in Cipher_Ref[7]=j meaning every h becomes a j
char[] Cipher_Ref_Upper_case = new char[Cipher_Ref.length];
// char fUpper = Character.toUpperCase(f);
for (int i = 0; i < Cipher_Ref.length; i++) {
Cipher_Ref_Upper_case[i] = Character.toUpperCase(Cipher_Ref[i]);
}
String Final = "";
for (int i = 0; i < Source.length(); i++) {
char temp_char = Source.charAt(i);
if (Is_Alphabet(temp_char)) {
if (Is_Alphabet_Lower(temp_char)) {
int temp1 = Retrieve_Char_Index1(temp_char, Alphab);
Final += Cipher_Ref[(int) temp1];
} else if (Is_Alphabet_Upper(temp_char)) {
int temp2 = Retrieve_Char_Index1(temp_char, Cap_Alphab);
Final += Cipher_Ref_Upper_case[(int) temp2];
} else {
Final += temp_char;
}
} else {
Final += temp_char;
}
}
return Final;
}
private final static String Reverse_Alpha_Cipher(String Source, char[] key) {
// we start by building the cipher reference from the cleaned password
// so lets say this is ower reference alphabetical "abcdfgijkmnpqrstuvwxyzhelo"
// now we start making all caps form of it to be able to handle upper case
// characters
// then we iterate throw every character of ower text
// lets say ower text is "jfpps Qsrnfl"
// the first letter is j and j is the 7th letter inside the cipher ref
// abcdefghijklmnopqrstuvwxyz this is the alphabet the 7th letter in this is h
// as in
// meaning every j becomes h
// meaning every f becomes e and so on
char[] Cipher_Ref = Creat_Cipher_Ref(key, Alphab);
char[] Cipher_Ref_Upper_case = new char[Cipher_Ref.length];
// char fUpper = Character.toUpperCase(f);
for (int i = 0; i < Cipher_Ref.length; i++) {
Cipher_Ref_Upper_case[i] = Character.toUpperCase(Cipher_Ref[i]);
}
String Final = "";
for (int i = 0; i < Source.length(); i++) {
char temp_char = Source.charAt(i);
Object[] temp1 = Retrieve_Char_Index(temp_char, Cipher_Ref);
Object[] temp2 = Retrieve_Char_Index(temp_char, Cipher_Ref_Upper_case);
if ((boolean) temp1[0]) {
Final += Alphab[(int) temp1[1]];
} else if ((boolean) temp2[0]) {
Final += Cap_Alphab[(int) temp2[1]];
} else {
Final += temp_char;
}
}
return Final;
}
// _________________________________________________________________________________________________________
private final static int[] Clean_Cipher_Numerical_Key(String Key) {
// I take the letters inside the password remove all other characters
// then i put them to lower case,i then get the reference of eache letter
// so basicly if the alphabetical order is a b c ... then a =1 b =2 ...
// if the pass word countains a , b then this methode returns 1 , 2
if (Key.length() > Numerical.length) {
Key = Key.substring(0, Numerical.length);
}
String temp = "";
Key = new String(Retrieve_Alphabets(Key));
String Temp_Source = Key.toLowerCase();
// String to char[]
char[] Numerical_Key = Temp_Source.toCharArray();
// int[] of alphabetic reference of the key
int[] Final_Key = new int[Numerical_Key.length];
for (int i = 0; i < Numerical_Key.length; i++) {
Object[] temp1 = Retrieve_Char_Index(Numerical_Key[i], Alphab);
if ((boolean) temp1[0]) {
// Final_Key[i] = Alphab[(int) temp1[1]];
Final_Key[i] = Clamp_Lopp((int) temp1[1], Numerical.length);
}
}
return Final_Key;
}
private final static int[] Creat_Cipher_Numerical_Ref(int[] key, int[] Ref) {
// by cleaning the password we creat a int array reference
// that reference will be used to switch the position of the first element with
// the next element wich index is the value of the first elemnt of int array
// say key is [7, 4, 1, 1, 4] ref is number 0=>9
// we start by switching 0 whit the seventh element wich is 6
// then 1 by the second element wich is 3 ....
int[] Num_Ref = Ref.clone();
// example : 1 , 2 ,3
// ===> : 2 , 1 ,3
// ===> : 3 , 1 ,2
// .....
for (int i = 0; i < key.length; i++) {
int First = Num_Ref[i];
int Second = Num_Ref[key[i]];
Num_Ref[key[i]] = First;
Num_Ref[i] = Second;
}
return Num_Ref;
}
private final static String Apply_Numerical_Cipher(String Source, int[] Cipher_Ref) {
// After creating the reference and cleaning the password
// We Iterate throw the string to be shufled go throw each
// if the character is a number then we use Retrieve_Char_Index check if number
// exist in the Cipher Reference if it does we retrieve that numbers position
// for example this is a cipher reference [7, 3, 4, 2, 1, 5, 6, 0, 8, 9]
// and this is ower text "123" so the first number in the text is 1 and we use
// that value in the cipher reference index so 1 is the index of the number 3
// stored in the cipher reference as such ref[1]=3 therfore
// every 1 becomes 3 | every 2 becomes 4 | every 3 becomes 2
String Final = "";
char[] Char_Numerical = Int_Array_To_Char_Array(Numerical);
for (int i = 0; i < Source.length(); i++) {
char temp_char = Source.charAt(i);
if (Character.isDigit(temp_char)) {
int temp1 = Retrieve_Char_Index1(temp_char, Char_Numerical);
Final += Cipher_Ref[temp1];
} else {
Final += temp_char;
}
}
return Final;
}
private final static String Reverse_Numerical_Cipher(String Source, int[] key) {
// We receive the encrypted Text then we use the cleaned version of the pass
// word call the Creat_Cipher_Numerical_Ref
// to find the original reference used to shufle the text then we reverse
// engeneer it
// for example this is a cipher reference [7, 3, 4, 2, 1, 5, 6, 0, 8, 9]
// and this is the numerical reference [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ];
// and this is ower text "342" so we iterate throw the text and the first
// number is 3 now 3 is stored in the cipher reference and it has a index of 1
// as ref[1]=3 meaning we turn every 3 to 1
// ref[2]=4 meaning we turn every 4 to 2
// ref[3]=2 meaning we turn every 2 to 3
int[] Cipher_Ref = Creat_Cipher_Numerical_Ref(key, Numerical);
char[] Temp_Cipher_Ref = Int_Array_To_Char_Array(Cipher_Ref);
char[] Char_Numerical = Int_Array_To_Char_Array(Numerical);
String Final = "";
for (int i = 0; i < Source.length(); i++) {
char temp_char = Source.charAt(i);
if (Character.isDigit(temp_char)) {
int temp1 = Retrieve_Char_Index1(temp_char, Temp_Cipher_Ref);
Final += Char_Numerical[temp1];
} else {
Final += temp_char;
}
}
return Final;
}
// _________________________________________________________________________________________________________
private final static int[] Clean_Cipher_Symbole_Key(String Key) {
// I take the letters inside the password remove all other characters
// then i put them to lower case,i then get the reference of eache letter
// so basicly if the alphabetical order is a b c ... then a =1 b =2 ...
// if the pass word countains a , b then this methode returns 97 , 98
// normaly a = 1 but in this case we used the ascii table so a to z is 97 to 122
if (Key.length() > Symbole.length) {
Key = Key.substring(0, Symbole.length);
}
String temp = "";
Key = new String(Retrieve_Alphabets(Key));
String Temp_Source = Key.toLowerCase();
// String to char[]
char[] Numerical_Key = Temp_Source.toCharArray();
// int[] of alphabetic reference of the key
int[] Final_Key = new int[Numerical_Key.length];
for (int i = 0; i < Numerical_Key.length; i++) {
Object[] temp1 = Retrieve_Char_Index(Numerical_Key[i], Alphab);
if ((boolean) temp1[0]) {
// Final_Key[i] = Alphab[(int) temp1[1]];
Final_Key[i] = Alphab[Clamp_Lopp((int) temp1[1], Alphab.length)];
}
}
return Final_Key;
}
private final static char[] Creat_Cipher_Symbole_Ref(int[] key, char[] Ref) {
// by cleaning the password we creat a int array reference
// that reference will be used to switch the position of the first element with
// the next element wich index is the value of the first elemnt of int array
// say key is [104, 101, 108, 111] ref All the symboles
// we start by switching the first symbole whit the 104th symbole then the
// second element whit the 101th element
// and so on
int[] Temp_key = Match_Array_Size(key, Ref.length);
char[] Num_Ref = Ref.clone();
// example : 1 , 2 ,3
// ===> : 2 , 1 ,3
// ===> : 3 , 1 ,2
// .....
for (int i = 0; i < Temp_key.length; i++) {
int index = Clamp_Lopp(Temp_key[i], Num_Ref.length);
char First = Num_Ref[i];
char Second = Num_Ref[index];
Num_Ref[index] = First;
Num_Ref[i] = Second;
}
return Num_Ref;
}
private final static String Apply_Symbole_Cipher(String Source, char[] Cipher_Ref) {
// so lets say this is ower symbole reference ower "&*~#{[|`\^@]}}"
// we iterate throw every symbole in ower text
// lets say ower text is "&&&&&"
// the first symbole is & and & is the 3th symbole
// now if we go to the cipher alpha ref and we look for the 3th letter
// it becomes ~ as in Cipher_Ref[3]=~ meaning every & becomes a ~
char[] Cipher_Ref_Upper_case = new char[Cipher_Ref.length];
// char fUpper = Character.toUpperCase(f);
for (int i = 0; i < Cipher_Ref.length; i++) {
Cipher_Ref_Upper_case[i] = Character.toUpperCase(Cipher_Ref[i]);
}
String Final = "";
for (int i = 0; i < Source.length(); i++) {
char temp_char = Source.charAt(i);
Object[] temp1 = Retrieve_Char_Index(temp_char, Symbole);
if ((boolean) temp1[0]) {
Final += Cipher_Ref[(int) temp1[1]];
} else {
Final += temp_char;
}
}
return Final;
}
private final static String Reverse_Symbole_Cipher(String Source, int[] key) {
// so lets say this is ower symbole reference ower "&*~#{[|`\^@]}}"
// we iterate throw every symbole in ower text
// lets say ower text is "~~~~~"
// the first symbole is ~ and ~ is the 3th symbole in ower reference
// so if we go to ower symbole array and look for the 3rd element we find &
// meaning every ~ becomes &
char[] Cipher_Ref = Creat_Cipher_Symbole_Ref(key, Symbole);
char[] Cipher_Ref_Upper_case = new char[Cipher_Ref.length];
// char fUpper = Character.toUpperCase(f);
for (int i = 0; i < Cipher_Ref.length; i++) {
Cipher_Ref_Upper_case[i] = Character.toUpperCase(Cipher_Ref[i]);
}
String Final = "";
for (int i = 0; i < Source.length(); i++) {
char temp_char = Source.charAt(i);
Object[] temp1 = Retrieve_Char_Index(temp_char, Cipher_Ref);
if ((boolean) temp1[0]) {
Final += Symbole[(int) temp1[1]];
} else {
Final += temp_char;
}
}
return Final;
}
// _________________________________________________________________________________________________________
// _________________________________________________________________________________________________________
private final static char[] Numerical_Capsule_Char(char[] Source, String key) {
char[] holder = null;
if (Contains_Digits(key)) {
int[] char_digits = Retrieve_Digits(key);
if (char_digits.length != 0) {
int sum = IntStream.of(char_digits).sum();
if (sum > 0) {
holder = Shifting_Cipher.Combine_Shift(Source, sum);
}
}
}
return holder;
}
private final static char[] Reverse_Numerical_Capsule_Char(char[] Source, String key) {
char[] holder = null;
if (Contains_Digits(key)) {
int[] char_digits = Retrieve_Digits(key);
if (char_digits.length != 0) {
int sum = IntStream.of(char_digits).sum();
if (sum > 0) {
holder = Shifting_Cipher.Reverse_Combine_Shift(Source, sum);
}
}
}
return holder;
}
private final static char[] Symbole_Capsule_Char(char[] Source, String key) {
if (Contains_Symboles(key)) {
char[] char_digits = Retrieve_Symboles(key);
int[] top = Char_Array_To_Ascii_Array(char_digits);
int sum = 0;
for (int i = 0; i < top.length; i++) {
sum += top[i];
}
if (char_digits.length != 0) {
Source = Shifting_Cipher.Combine_Shift(Source, sum);
}
}
return Source;
}
private final static char[] Reverse_Symbole_Capsule_Char(char[] Source, String key) {
if (Contains_Symboles(key)) {
char[] char_digits = Retrieve_Symboles(key);
int[] top = Char_Array_To_Ascii_Array(char_digits);
int sum = 0;
for (int i = 0; i < top.length; i++) {
sum += top[i];
}
if (char_digits.length != 0) {
Source = Shifting_Cipher.Reverse_Combine_Shift(Source, sum);
}
}
return Source;
}
// _________________________________________________________________________________________________________
private final static boolean Contains_Digits(String Source) {
char[] Numerical_Key = Source.toCharArray();
for (char c : Numerical_Key) {
if (Character.isDigit(c)) {
return true;
}
}
return false;
}
private final static boolean Contains_Alphabets(String Source) {
char[] Numerical_Key = Source.toCharArray();
for (char c : Numerical_Key) {
if (Character.isAlphabetic(c)) {
return true;
}
}
return false;
}
private final static boolean Contains_Symboles(String Source) {
char[] Numerical_Key = Source.toCharArray();
for (char c : Numerical_Key) {
if (Character.isDigit(c) == false && Character.isAlphabetic(c) == false) {
return true;
}
}
return false;
}