-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenigmax.js
More file actions
1236 lines (901 loc) · 30 KB
/
enigmax.js
File metadata and controls
1236 lines (901 loc) · 30 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
var crypto = require('crypto');
/*try {
var buf = crypto.randomBytes(32);
console.log(buf.length, buf.readUInt32BE(0));
} catch (ex) {
// handle error
console.log('most likely, entropy sources are drained');
}*/
//.......................Rotor prototype..........................
function Rotor(subSet) {
//the input output set is just the regular non moving alphabet
var ioSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//the basic set is matched to the substitution set to caculate the ofsets of the rotor
//it rotates with the subsitution set
var basicSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// "EKMFLGDQVZNTOWYHXUSPAIBRCJ";
//tells us what's in the ring window and
//when we're at our turnover
var ringSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//retrieve the notch setting and chop off the setting from the substitution set
var notch = subSet[26];
subSet = subSet.slice(0, 26);
//stores outputs sent from the rotor functions
var output;
//these four functions are for looking at the internal state of the rotor
this.getBasicSet = function() {
return basicSet;
};
this.getSubSet = function() {
return subSet;
};
this.getRingSet = function() {
return ringSet;
};
this.getNotch = function(){
return notch;
};
//takes an input char to the rotor and returns an output char
this.getSubChar = function(input){
output = ioSet[ basicSet.indexOf( subSet[ ioSet.indexOf(input) ] ) ];
return output;
};
// takes a reverse input char to the rotor and returns an output char
this.getRevSubChar = function(input){
output = ioSet[ subSet.indexOf( basicSet[ ioSet.indexOf(input) ] ) ];
return output;
};
//rotates the rotor one click
this.rotate = function(){
ringSet = ringSet + ringSet[0];
ringSet = ringSet.slice(1);
basicSet = basicSet + basicSet[0];
basicSet = basicSet.slice(1);
subSet = subSet + subSet[0];
subSet = subSet.slice(1);
};
//used to tell if the notch has been passed
this.checkNotch = function(){
if(ringSet[25] === notch) {
output = true;
}
else {
output = false;
}
return output;
};
//used to tell if notch is about to be passed
//useful for double stepping feature
this.checkPreNotch = function() {
if(ringSet[0] === notch){
output = true;
}
else {
output = false;
}
return output;
};
this.setStartPosition = function(input){
while(ringSet[0] !== input) {
this.rotate();
}
};
this.setRing = function(input) {
//rotate the ring set until it matches the
//input setting, if it never matches exit the loop
var x = 0;
while(ringSet[0] !== input && x < 26) {
ringSet = ringSet + ringSet[0];
ringSet = ringSet.slice(1);
x++;
}
};
}
//................Enigma Core Machine prototype..................................
function EnigmaCore() {
var rotorArray = [];
var startSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var basicSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var plugboard ="LPGSZMHAEOQKVXRFYBUTNICJDW";
var ringSet = "";
//double is step used for the regular enigma
//double stepping feature
var doubleStepOn = false;
this.turnOnDoubleStep = function() {
doubleStepOn = true;
};
this.turnOffDoubleStep = function() {
doubleStepOn = false;
};
//loads a new rotor Array into the machine
this.loadRotors = function(inputRotorArray){
rotorArray = inputRotorArray();
};
//sets all the rotors to their start positions specified in startSet
this.setRotors = function() {
//console.log(rotorArray[0].getBasicSet());
//console.log(rotorArray.length);
for(var i=0; i< rotorArray.length; i++) {
rotorArray[i].setStartPosition(startSet[i]);
//console.log(rotorArray[i].getBasicSet());
}
};
this.setRings = function() {
for(var i=0; i< rotorArray.length; i++) {
rotorArray[i].setRing(ringSet[i]);
//console.log(rotorArray[i].getRingSet());
}
};
//loads a new rotor start positions into the machine
this.loadStartSet= function(inputStartSet){
startSet = inputStartSet;
};
//logs info about state of core machine
//used for testing purposes
this.getState = function() {
var position ="";
var notchState = "";
for(x = 0; x < rotorArray.length; x++) {
position = position + rotorArray[x].getBasicSet()[0];
notchState = notchState + rotorArray[x].checkNotch();
}
return position;
//console.log("notch:"+ notchState);
//console.log(rotorArray[0].checkNotch());
};
//rotates rotor one and any rotors that are currently notched
var rotate = function() {
//if second rotor is right before it's notch hit it
//it should go ahead and rotate (if doubletep is on)
if(doubleStepOn && rotorArray[1].checkPreNotch()) {
rotorArray[1].rotate();
rotorArray[2].rotate();
}
var x = 0;
do
{
rotorArray[x].rotate();
x++;
}
while(x < rotorArray.length && rotorArray[x-1].checkNotch());
};
//for testing the rotate function
this.rotateTest = function(rotateNum){
for(var x = 0; x < rotateNum; x++) {
rotate();
}
};
//feeds a charachter through the machine and returns the result
this.getSubChar = function(input){
//used as return variable and in below loop
var output;
//rotate rotor 0 and notched rotors
rotate();
//feed input charachter through all rotors
for(var x =0; x< rotorArray.length; x++) {
output = rotorArray[x].getSubChar(input);
input = output;
}
return output;
};
//reverse feeeds a charachter through the machine and returns the result
this.getRevSubChar =function(input){
//used as return variable and in below loop
var output;
//rotate rotor 0 and notched rotors
rotate();
//feed charchater backward through all rotors
for(var x =(rotorArray.length -1); x >= 0; x--) {
output = rotorArray[x].getRevSubChar(input);
input = output;
}
return output;
};
//encrypt an input string
this.encrypt = function(inputMessage){
var outputMessage = "";
//console.log("Input Message: " + inputMessage);
//put message through plugboard
outputMessage = this.plugboardIn(inputMessage);
inputMessage = outputMessage;
outputMessage="";
//console.log("Plugboard message: " + inputMessage);
//feeds inputMessage through machine and fills outputMessage
for(var i = 0; i < inputMessage.length; i++) {
//take next characther from the message
input = inputMessage[i];
//feed charachter through machine
output = this.getSubChar(input);
//add output charachter to the output message
outputMessage = outputMessage + output;
}
//console.log("Final Message: " + outputMessage);
return outputMessage;
};
//decrypt an input string
this.decrypt = function(inputMessage){
var outputMessage = "";
//reverse feeds inputMessage through machine and fills outputMessage
for(var i = 0; i < inputMessage.length; i++) {
//take next characther from the message
input = inputMessage[i];
//feed charachter through machine
output = this.getRevSubChar(input);
//add output charachter to the output message
outputMessage = outputMessage + output;
}
//reverse message through plugboard
inputMessage = outputMessage;
outputMessage = this.plugboardOut(inputMessage);
return outputMessage;
};
//takes a rotor set and builds a rotor Array that can be passed to EX Machine
this.buildRotorArray = function(rotorSet) {
var basicSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var rotorBlock = "LPGSZMHAEOQKVXRFYBUTNICJDWY\n" +
"SLVGBTFXJQOHEWIRZYAMKPCNDUE\n" +
"CJGDPSHKTURAWZXFMYNQOBVLIEN\n" +
"PEZUOHXSCVFMTBGLRINQJWAYDKY\n" +
"ZOUESYDKFWPCIQXHMVBLGNJRATE\n" +
"EHRVXGAOBQUSIMZFLYNWKTPDJCN\n" +
"JGDQOXUSCAMIFRVTPNEWKBLZYHN\n" +
"NTZPSFBOKMWRCJDIVLAEYUXHGQE\n" +
"JVIUBHTCDYAKEQZPOSGXNRMWFLY\n" +
"EKMFLGDQVZNTOWYHXUSPAIBRCJQ\n" +
"AJDKSIRUXBLHWTMCQGZNPYFVOEE\n" +
"BDFHJLCPRTXVZNYEIWGAKMUSQOV\n" +
"ESOVPZJAYQUIRHXLNFTGKDCMWBJ\n" +
"VZBRGITYUPSDNHLXAWMJQOFECKZ\n" +
"LVAWDKEJGMIHORNSQYPBZCFTXUQ\n" +
"WYKEXFINHQOPJRLTMSACVGBUZDV\n" +
"WTOKASUYVRBXJHQCPZEFMDINLGQ\n" +
"GJLPUBSWEMCTQVHXAOFZDRKYNIE\n" +
"JWFMHNBPUSDYTIXVZGRQLAOEKCV\n" +
"FGZJMVXEPBWSHQTLIUDYKCNRAOJ\n" +
"HEJXQOTZBVFDASCILWPGYNMURKZ\n" +
"KEDXVBSQHNCZTRUFLOAYWIPMJGF\n" +
"NUJPHWFMGDOBAVZQTXECLKYSIRO\n" +
"CIAHFQOYBXNUWJLVGEMSZKPDTRZ\n" +
"FKOQBLHNAPWDRUYSVGJEXMTCZIY\n" +
"VMWJNPAUTIFXBYGDZCRQKHOLSEE\n";
var rotorSettingArray = rotorBlock.split("\n");
var rotorArray = [];
//takes given rotor choice and find it in the rotorSettingArray
//adding it to the rotor set
for(var i=0; i < rotorSet.length; i++){
rotorArray[i] = new Rotor(rotorSettingArray[basicSet.indexOf(rotorSet[i])]);
//console.log(rotorArray[i].getSubSet());
}
return rotorArray;
};
//takes a set of letters, builds a rotor set from it and loads it in the machine
this.loadRotorSet = function(rotorSet) {
rotorArray = this.buildRotorArray(rotorSet);
};
//loads an enigma core key with all it's parts
this.setKey = function(key) {
var rotorSet = key.rotorSet;
this.loadRotorSet(rotorSet);
startSet = key.startSet;
plugboard = key.plugboard;
ringSet = key.ringSet;
//console.log(rotorSet);
//console.log(startSet);
//console.log(plugboard);
};
//simple plugboard substitution
this.plugboardIn = function(input) {
output = "";
for(var i=0; i<input.length; i++){
output = output + plugboard[basicSet.indexOf(input[i])];
}
return output;
};
//simple reverse plugboard substitution
this.plugboardOut = function(input) {
output = "";
for(var i=0; i<input.length; i++){
output = output + basicSet[plugboard.indexOf(input[i])];
}
return output;
};
//test the pluboard to make sure it has
//all 26 letters of the aphabet
this.plugsAreGood = function(plugboard) {
var basicSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var output = true;
//check for all letters of the alphabet
for(var i=0; i< 26; i++)
{
if( plugboard.indexOf(basicSet[i]) === -1) {
output = false;
}
}
return output;
};
//when new machine made set default rotors
this.loadRotorSet("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
//when machine starts set default startSet;
startSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//when new machine made set rotor array to the startset
this.setRotors();
}
//...............Enigma Regular Prototype.....................
function EnigmaRegular() {
var enigmaCore = new EnigmaCore();
enigmaCore.turnOnDoubleStep();
//default key
var key = {
rotorSet: "JKL",
startSet: "AAA",
ringSet: "AAA",
plugboard: "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
};
//reflector built from a non moving rotor
var reflector = new Rotor("YRUHQSLDPXNGOKMIEBFZCWVJATA");
//takes a message string and pushes
//through the relector
reflector.getSubStr = function(message) {
var output = "";
for(var i=0; i < message.length; i++){
output = output +this.getSubChar(message[i]);
}
return output;
};
//takes a message string and reverse
//pushes it through the reflector
reflector.getRevSubStr = function(message) {
var output = "";
for(var i=0; i < message.length; i++){
output = output +this.getRevSubChar(message[i]);
}
return output;
};
this.crypt = function(message) {
//convert everything to uppercase
message = message.toUpperCase();
//find any enigma charachters (alphabet)
message = message.match(/[A-Z]/g);
if(message !== null) {
//turn message back in to a single string
message = message.join('');
//set the rotors to their start position
enigmaCore.setRotors();
//push through rotors
message = enigmaCore.encrypt(message);
//push through reflector
message = reflector.getSubStr(message);
//reset rotors
enigmaCore.setRotors();
//reverse push through rotors
message = enigmaCore.decrypt(message);
}
else {
message = "invalid\0";
}
return message;
};
//takes a key object and set it in enigmaCore
this.setKey = function(key){
var revKey = {rotorSet:"", startSet:"", ringSet:"", plugboard:""};
//rotors are in reverse order in Enigma Regular vs
//Enigma Core, this reverses them
revKey.rotorSet = key.rotorSet.split("").reverse().join("");
revKey.startSet = key.startSet.split("").reverse().join("");
revKey.ringSet = key.ringSet.split("").reverse().join("");
revKey.plugboard = key.plugboard;
enigmaCore.setKey(revKey);
enigmaCore.setRings();
};
this.loadKey = function(keyString) {
var output = "";
//convert keystring to uppercase
keyString = keyString.toUpperCase();
//capture only the key part
keyString = keyString.match(/[A-Z]{3}-[A-Z]{3}-[A-Z]{3}-[A-Z]{26}/);
//if keystring was not found
if(keyString === null) {
output="invalid\0";
}
//break the key down and check the plugboard
else {
keyString = keyString[0];
keyString = keyString.split('\n');
keyString = keyString[0].split('-');
key.rotorSet = keyString[0];
key.startSet = keyString[1];
key.ringSet = keyString[2];
key.plugboard = keyString[3];
//if pluboard is good set the key
if(enigmaCore.plugsAreGood(key.plugboard)){
output ="rotors:"+ key.rotorSet + '\n' +
"start:" + key.startSet + " rings:" + key.ringSet + '\n' +
"plugboard:" + key.plugboard;
enigmaRegular.setKey(key);
}
else {
output="invalid\0";
}
}
return output;
};
//set the default settings on new Enigma Regular
this.setKey(key);
}
//................ASCII / Enigma Code Converter prototype.....................
function AsciiEnigmaConverter() {
//takes an ASCII char and returns two enigma machine chars
var toEnigmaChar = function(input) {
var output = "";
var basicSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var charCode = input.charCodeAt();
var remainder = charCode % 26;
var quotient = (charCode - remainder) / 26;
if(quotient > 25) {
quotient = 25;
remainder = 25;
}
output = basicSet[quotient] + basicSet[remainder];
//console.log(charCode, quotient, remainder, output);
return output;
};
//takes two enigma machine chars and returns an ASCII char
var toAsciiChar = function(input) {
var output ="";
var basicSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var firstCharNum = basicSet.indexOf(input[0]);
var secondCharNum = basicSet.indexOf(input[1]);
var charCode = (firstCharNum * 26) + (secondCharNum);
output = String.fromCharCode(charCode);
//console.log(firstCharNum, secondCharNum, charCode, output);
return output;
};
//takes a string of ASCII chars and returns enigma code
this.toEnigmaCode = function(input) {
var output = "";
//get enigma codes for each ASCII char
for(var i=0; i < input.length; i++) {
output = output + toEnigmaChar(input[i]);
}
return output;
};
//takes enigma codes and returns ASCII chars
this.toAsciiCode = function(input) {
var output = "";
//get ASCII char for each set of enigma codes
for(var i=0; i < input.length; i=i+2) {
//console.log(input.substr(i,2));
output = output + toAsciiChar(input.substr(i,2));
}
return output;
};
}
//....................Think Ding Converter prototype.....................
function ThinkDinger() {
var basicSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var thinkDingSet ="♆☢♗☯☠✈♞❂☭✂☏☾♠✿☮❉♕✪♙☸☹✸♬★♖☂";
var output = "";
//converts Enigma Code to Think Ding code
this.toThinkDing = function(input) {
output = "";
for(var i=0; i<input.length; i++){
output = output + thinkDingSet[basicSet.indexOf(input[i])];
}
return output;
};
//converts Think Ding Code to Enigma Code
this.toEnigmaCode = function(input) {
output = "";
for(var i=0; i<input.length; i++){
output = output + basicSet[thinkDingSet.indexOf(input[i])];
}
return output;
};
}
//......................String Tester Prototype................
function StringTester() {
var basicSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var thinkDingSet ="♆☢♗☯☠✈♞❂☭✂☏☾♠✿☮❉♕✪♙☸☹✸♬★♖☂";
this.isEnigmaCode = function(input) {
for(var i=0; i<input.length; i++){
// this function needs fixin, does nothin righ tnow
// but is not being used by enigmaX
}
};
//test to make sure string is in ascii range
this.isAsciiCode = function(input) {
var output = true;
//note this is testing for my ascii range not real ascii range
for(var i=0; i<input.length; i++){
if(input.charCodeAt(i) > 675) {
output = false;
break;
}
}
return output;
};
//checks if string is composed of thinkDingChars
this.isThinkDing = function(input) {
var output = true;
for(var i=0; i<input.length; i++){
if(thinkDingSet.indexOf(input[i]) === -1){
output = false;
break;
}
}
return output;
};
}
//..............Random Generator Protoype.............
function RandomGen()
{
//0 to 4294967295 - range of a 32 bit integer- (2^32)-1
var max32BitInt = 4294967295;
//holds 100 sjcl random numbers
//this.randomArray = sjcl.random.randomWords(100);
//gets a random 32 bit positive integer
this.getRandom32BitInt= function() {
/*
//get random number from randomArray
var randomNum = this.randomArray.shift();
//console.log(randomNum);
//if random array is now empty reload it
if(this.randomArray.length === 0) {
this.randomArray = sjcl.random.randomWords(100);
}
//if random number is negative convert it to a postive
//integer without creating a bit bias
if(randomNum < 0) {
randomNum = Math.abs(randomNum);
randomNum = randomNum + 0x80000000;
}
*/
//get 32 random bits and convert them to a number
//using nodes crypto library instead of sjcl
var randomNum = crypto.randomBytes(32).readUInt32BE(0);
//breaking the random to test this without sjcl
//var randomNum = 10;
return randomNum;
};
//gets a random int from 0 to specified range
this.getRandomNum =function(range) {
var randomNum = 0;
//gets number and puts it in range, if out
//of distribution range throw out and try again
do {
randomNum = this.getRandom32BitInt();
randomNum = toRange(randomNum, range);
} while (randomNum === null);
return randomNum;
};
//takes a 32 bit integer and retunrs a number in a range from 0 to range
//specified, returning null for numbers that would creat an uneven distribution
var toRange =function(number, range) {
var output = 0;
//add 1 so number%range returns numbers up to range
range++;
//get the range within max32BitInt for an even distribution
var maxForEvenDist = (max32BitInt - (max32BitInt % range)) -1;
//console.log("evenDistRange: "+ evenDistRange);
if(number > maxForEvenDist) {
output = null;
}
else {
output = number % range;
}
return output;
};
this.toRange = toRange;
//test toRange function checking for even distribution
this.testDist = function() {
var array = [];
var exceptionCount = 0;
var range = 0;
//test value since real value is not testable
max32BitInt = 6000;
for(var i = 0; i <=max32BitInt; i++) {
if(toRange(i, range) !== null ) {
if(array[toRange(i, range)] === null) {
array[toRange(i, range)] = 1;
}
else {
array[toRange(i, range)] = array[toRange(i, range)] + 1;
}
}
else {
exceptionCount++;
}
}
//console.log(array);
//console.log(exceptionCount);
//return max32BitInt to it's correct value
max32BitInt = 4294967295;
};
}
//............Key Generator Prototype..............
function KeyGen() {
var randomGen = new RandomGen();
var basicSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
this.getKey = function(){
var key = {rotorSet: "", startSet: "", plugboard: ""};
for(var i = 0; i<26; i++) {
key.rotorSet = key.rotorSet + basicSet[randomGen.getRandomNum(25)];
}
for(var i = 0; i<26; i++) {
key.startSet = key.startSet + basicSet[randomGen.getRandomNum(25)];
}
var randomChar = "";
for(var i = 25; i>=0; i--) {
randomNum = randomGen.getRandomNum(i);
randomChar= basicSet[randomNum];
key.plugboard = key.plugboard + randomChar;
basicSet = basicSet.replace(randomChar,'');
}
basicSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
return key;
};
this.getTinyKey = function() {
var tinyKey = { rotorSet: "", startSet: "", plugboard: basicSet};
for(var i = 0; i<3; i++) {
tinyKey.rotorSet = tinyKey.rotorSet + basicSet[randomGen.getRandomNum(25)];
}
for(var i = 0; i<3; i++) {
tinyKey.startSet = tinyKey.startSet + basicSet[randomGen.getRandomNum(25)];
}
return tinyKey;
};
}
//...........EnigmaX Machine Prototype.............
function EnigmaXMachine(){
var basicSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var inputMessage = "";
var outputMessage ="";
//build all components of the machine
var enigmaCore = new EnigmaCore();
var enigmaTiny = new EnigmaCore();
var aeConverter = new AsciiEnigmaConverter();
var tdConverter = new ThinkDinger();
var sTester = new StringTester();
var keyGen = new KeyGen();
var tinyKey = keyGen.getTinyKey();
//generate a random enigmaCore key
var key = keyGen.getKey();
//set the key in the core machine
enigmaCore.setKey(key);
//enigmaX key in thinkDing
var thinkDingKey ="";
this.newKey = function() {
//generate a new enigma key
key = keyGen.getKey();
//set key in enigma core machine
enigmaCore.setKey(key);
//translate to a thinkDing key
thinkDingKey = tdConverter.toThinkDing(key.rotorSet) + '\n' +
tdConverter.toThinkDing(key.startSet) + '\n' +
tdConverter.toThinkDing(key.plugboard);
//return the thinkDing version
return thinkDingKey;
};
//load a thinkDing key into the machine
this.loadKey = function(keyString) {
var output ="";
//thinkDing key will hold 78 thinkDing Chars
var thinkDingKey ="";
//find any thinkDing chars and put them in the thinkDing
//string until it reaches 78 chars or end of string is reached
for(var i=0; thinkDingKey.length < 78 && i < keyString.length; i++) {
if(sTester.isThinkDing(keyString[i])) {
thinkDingKey = thinkDingKey + keyString[i];
}
}
if(thinkDingKey.length < 78) {
output = "invalid\0";
}
else {
key.rotorSet = tdConverter.toEnigmaCode(thinkDingKey.substr(0, 26));
key.startSet = tdConverter.toEnigmaCode(thinkDingKey.substr(26, 26));
key.plugboard = tdConverter.toEnigmaCode(thinkDingKey.substr(52, 26));
output = thinkDingKey.substr(0, 26) + '\n' +
thinkDingKey.substr(26, 26) + '\n' +
thinkDingKey.substr(52, 26);
}
//finally check the plugboard to make sure it's
//good then set the key in Enigma Core
if(enigmaCore.plugsAreGood(key.plugboard)){
enigmaCore.setKey(key);
}
else {
output = "invalid\0";
}
return output;
};
//process thinkDing Keys for encryption
var processTDKeys = function(message) {
var startsWithNewLine = false;
//capture first thinkDing key in the message
var group = /((\n|^)([♆☢♗☯☠✈♞❂☭✂☏☾♠✿☮❉♕✪♙☸☹✸♬★♖☂]{26})(?=\n|$)){3}/.exec(message);
//do while the thinkDing group has actually captured something
while(group !== null) {
//strip regex down to just the string
group = group[0];
//console.log(group);
//take note if the group starts with a new line
if(group[0] === "\n") {startsWithNewLine = true;}
//remove all endline chars and create one string
group = group.replace(/\n/g, "");
//console.log(group);
//convert to Enigma Code
group = tdConverter.toEnigmaCode(group);
//console.log(group);
//convert to ascii