forked from macmcmeans/isaacCSPRNG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enigma+7ciphers+PRNG.html
833 lines (712 loc) · 35.4 KB
/
Enigma+7ciphers+PRNG.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
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<!--
Isaac CSPRNG - generate Vernam key with length equals of message length,
according the start value of "seed".
-->
<script src="isaacCSPRNG-1.1.js"></script>
<!-- As seed can be used the sha512 hash, not clear string -->
<script src="files/sha512.js"></script>
<!-- <script src="sha256.js">/*or sha256 hash*/ </script> -->
<div align="center">
<img src="files/EnigmaLogo.png" align="center" style="display: inline-block;"
title='Analog of Enigma for custom alphabets
(dynamic reversive "shifted אתבש" + six ciphers with isaacCSPRNG)
Text + seed (+ alphabet) = Text + key (+ alphabet) = Cypher;
Cypher + seed (+ alphabet) = Cypher + key (+ alphabet) = Text;
CSPRNG key = seed + message.length'><br>
<img src="files/disk_cesar.png" style="width: 128px; height: 128px;" title="Shifted atbash cipher included here">
<br>
<hr>
<font title="This can be exchanged securely, using Diffie-Hellman protocol.">1. Before encryption/decryption - set identical seed-string, as key:</font><br>
<input id="generate_seed" type="button" value="Generate another seed." onclick="generate_seed();">
<input id="seed" type="text" value="my_super_secret_seed" size="80%" onchange="change_seed(this);" title="Leave this textarea empty - to don't shuffle alphabet...">
<input id="set_seed" type="button" value="Set this seed, update alphabet and shuffle it." onclick="change_seed(seed_element);">
<br>
<hr>
2. Customize alphabet (only unique allowed symbols):<br>
<textarea id="alphabet" cols="150" rows="3" title="Input unique string with allowed characters for message and cyphertext." style="text-align: center;">ABCDEFGHIJKLMNOPQRSTUVWXYZ</textarea>
<br>
<!--<input id="set_alphabet" type="button" value="Set this alphabet." onclick="setAlphabet(document.getElementById('alphabet'));">-->
<label><input type="radio" name="shuffle_or_not" id="shuffle" value="shuffle" checked="checked"> Shuffle alphabet, using CSPRNG (after set seed).</label>
<label title="To decrypt cyphertext, by copy and paste this with generated key value.">
<input type="radio" name="shuffle_or_not" id="not_shuffle" value="not_shuffle">
Do not shuffle alphabet
</label>
<br>
<input id="set_alphabet" type="button" value="Set this alphabet, update seed again and shuffle alphabet." onclick="change_seed(seed_element);"
title="To decrypt - just copy seed and cyphertext.">
<br>Shuffled alphabet for encryption/decryption:<br>
<pre id="shuffled_alphabet" style="border: 1px solid black;" title="Alphabet, after shuffle."></pre>
<br>
<hr>
4. Select cipher algorithm:
<br>
<font color="black"><b>Reversive (one button for encrypt/decrypt): </b></font>
| <label>Vernam <input type="radio" id="Vernam_mode" name="cypher" value="Vernam" checked="checked" onclick="set_cypher();"/></label>
<input id="XOR_char" value="" maxlength="1" size="1"
title="Input XOR_char, if code of symbol is greater than (2^N - 1).
This symbol must not to be in the alphabet, and just will be using as marker."
oninput="change_XOR_char();"></input>
| <label>Shifted atbash <input type="radio" id="shifted_atbash_mode" name="cypher" value="shifted_atbash" onclick="set_cypher();"/></label>
| <label>Beaufort <input type="radio" id="Beaufort_mode" name="cypher" value="Beaufort" onclick="set_cypher();"/></label>
| <label>Atbash <input type="radio" id="Atbash_mode" name="cypher" value="Atbash" onclick="set_cypher();"/></label>
| <br><font color="black"><b> Not Reversive (two buttons for encrypt and decrypt): </b></font>
| <label>Tritemius <input type="radio" id="Tritemius_mode" name="cypher" value="Tritemius" onclick="set_cypher();"/></label>
| <label>Gronsfeld <input type="radio" id="Gronsfeld_mode" name="cypher" value="Gronsfeld" onclick="set_cypher();"/> </label>
| <label>Vizhener <input type="radio" id="Vizhener_mode" name="cypher" value="Vizhener" onclick="set_cypher();"/></label>
| <br>
<hr>
3. Now you can encrypt message/decrypt cyphertext.
<table align="center">
<tr>
<td colspan="2" align="center" class="typeletter" style="background: url(files/entertext.png) #fff no-repeat bottom;">
<label for="textin" accesskey="t"
title="This field to show symbol-by-symbol encryption/decryption.
Better to using 'Block of text' to encrypt/decrypt, if you typing text - fast."
>Type Letter:</label><br>
<input type="text" id="textin" size="3" maxlength="1" value="" style="text-align: center;"
title="Encrypt/decrypt symbol-by-symbol.
This letter must be from alphabet.
Any string that contains no line feed (U+000A, “LF”) or carriage return (U+000D, “CR”) characters.
Else, use text input.
">
</td>
</tr>
<tr>
<td>
<label for="msg_in">(Clear Text/Cipher Text) In:</label><br>
<textarea id="msg_in" cols="30" rows="4" value="" readonly="readonly"></textarea>
</td>
<td>
<label for="msg_out">(CipherText/Clear Text) Out:</label><br>
<textarea id="msg_out" cols="30" rows="4" value="" readonly="readonly"></textarea>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<label><input type="radio" name="show_hide_key" id="show_key" value="show_key">Show key</label>
<label><input type="radio" name="show_hide_key" id="hide_key" value="hide_key" checked="checked">Hide key</label><br>
<label id="generated_key_label" for="generated_key_value" style="display: none;">Generated key:<br>
<textarea id="generated_key_value" cols="30" rows="4" value="" readonly="readonly"></textarea><br>
</label>
<input id="clear" type="button" value="Clear textareas." onclick="clear_textareas();">
</td>
</tr>
<tr>
<td colspan="2" align="center">Input Method:
<label><input type="radio" name="input_method" id="single" value="single"> Single letter</label>
<label><input type="radio" name="input_method" id="block" value="block" checked="checked"> Block of text</label>
</td>
</tr>
<tr align="center" id="blockinput">
<td colspan="2">
<textarea id="txt_in" cols="60" rows="6"></textarea><br>
<label><input type="radio" name="text_key" id="generate_key" value="generate_key">Generate key</label>
<label><input type="radio" name="text_key" id="input_key" value="input_key" checked="checked">Input key</label><br>
<label id="input_key_label" for="input_key_value" style="display: none;">Key:<br>
<textarea id="input_key_value" cols="30" rows="4" value="" title="Import key. Also, if alphabet was been shuffled - import the shuffled alphabet, or seed with not shuffled alphabet."></textarea><br>
</label>
<!-- Reversive ciphers -->
<input type="button" id="encryptdecrypt" value="Vernam Encrypt/Decrypt" onclick="progress_text();">
<input type="button" id="Shifted_atbash" value="Shifted atbash Encrypt/decrypt" onclick="progress_text( prng.param_get('algo') );">
<input type="button" id="Beaufort" value="Beaufort encrypt/decrypt" onclick="progress_text( prng.param_get('algo') );">
<input type="button" id="Atbash" value="Atbash encrypt/decrypt" onclick="progress_text( prng.param_get('algo') );">
<!-- This two buttons need for not reversive ciphers, because encipher and decipher in this case is different actions. -->
<input type="button" id="tritemius_encrypt" value="Tritemius Encrypt" onclick="progress_text('Encipher');">
<input type="button" id="tritemius_decrypt" value="Tritemius Decrypt" onclick="progress_text('Decipher');">
<input type="button" id="Vizhener_encrypt" value="Vizhener Encrypt" onclick="progress_text('Encipher');">
<input type="button" id="Vizhener_decrypt" value="Vizhener Decrypt" onclick="progress_text('Decipher');">
<input type="button" id="Gronsfeld_encrypt" value="Gronsfeld Encrypt" onclick="progress_text('Encipher');">
<input type="button" id="Gronsfeld_decrypt" value="Gronsfeld Decrypt" onclick="progress_text('Decipher');">
</td>
</tr>
</table>
</div>
</body>
</html>
<script>
/*
1. All algorithms are working with custom alphabets, with different alphabet length's.
2. Encryption/decryption symbol-by-symbol, like in Enigma is available.
3. function prng.symbol(m, param, key, change_algo, change_alphabet, Vernam_XOR_symbol)
can working with custom key, if key will be specified.
alphabet, cipher algo, and Vernam_XOR_symbol - can be updated there.
See commented USAGE in the source code.
4. Shifted atbash is atbash for Vizhener cyphertext.
This ciphertext have reversive operation for encryption and decryption.
Atbash for shifted atbash - return Vizhener cyphertext. This was been tested.
See commented code in the source code of this script.
There is the table for this cipher mode, in the bottom.
5. Delete and backspace working, if symbol was been incorrect.
After this seed updated again, and all textarea enciphered then.
*/
/*
//Example of using isaacCSPRNG:
var prng1 = isaacCSPRNG(); //new object of generator
prng1.seed('seed'); //then set specified seed
//or
//var prng1 = isaacCSPRNG('seed'); //new object of generator with specified seed.
//skip N rounds by 256 bytes:
console.log(
'258 bytes: ', isaacCSPRNG('seed').bytes(258) //258 bytes from new PRNG object with previous seed.
, '\n'+ 'skip 1 round (256 bytes * 1 round): ', prng1.prng(1) //skip 1 round (1 round * 256 bytes) in old defined CSPRNG object
, '\n'+ 'next 2 bytes: ', prng1.bytes(2) //after skip 1 round, get next 2 bytes is equal of 257,258 bytes from previous prng.
);
//skip N bytes (prng.skip_bytes(N) - added in isaacCSPRNG-1.1.js):
var prng1 = isaacCSPRNG('seed'); //set seed again in defined prng
console.log(
'just 8 bytes: ', isaacCSPRNG('seed').bytes(8) //get 8 bytes from new PRNG object with this seed
, '\n'+ 'skip 5 bytes: ', prng1.skip_bytes(5) //skip 5 bytes in old defined CSPRNG object
, '\n'+ 'next 3 bytes: ', prng1.bytes(3) //after skip 5 bytes, next 3 bytes there is equal of 6,7,8 bytes from previous PRNG.
);
//All this pseudorandom values, beginning from initialization seed,
//can be used as vernam key for encryption/decryption.
//isaacCSPRNG have the smallest period of 4.67 * 10^1240, according data from Wikipedia.
//> In practice this means that the attack needs 4.67 * 10^1240 instead of 10^2466.
*/
var prng = isaacCSPRNG(); //Define generator object with isaacCSPRNG
var use_XOR_char = '+';
var XOR_char_element = document.getElementById('XOR_char');
XOR_char_element.value = use_XOR_char;
var shuffled_alphabet_pre = document.getElementById('shuffled_alphabet');
//HTML elements, as JavaScript variables:
//mode
var Shifted_atbash_mode = document.getElementById('shifted_atbash_mode');
var Vizhener_mode = document.getElementById('Vizhener_mode');
var Beaufort_mode = document.getElementById('Beaufort_mode');
var Atbash_mode = document.getElementById('Atbash_mode');
var Gronsfeld_mode = document.getElementById('Gronsfeld_mode');
var Vernam_mode = document.getElementById('Vernam_mode');
var Tritemius_mode = document.getElementById('Tritemius_mode');
//encrypt/decrypt buttons
var encrypt_decrypt_button = document.getElementById('encryptdecrypt');
var Shifted_atbash_button = document.getElementById('Shifted_atbash');
var Beaufort_button = document.getElementById('Beaufort');
var Atbash_button = document.getElementById('Atbash');
var Vizhener_encrypt_button = document.getElementById('Vizhener_encrypt');
var Vizhener_decrypt_button = document.getElementById('Vizhener_decrypt');
var Gronsfeld_encrypt_button = document.getElementById('Gronsfeld_encrypt');
var Gronsfeld_decrypt_button = document.getElementById('Gronsfeld_decrypt');
var tritemius_encrypt_button = document.getElementById('tritemius_encrypt');
var tritemius_decrypt_button = document.getElementById('tritemius_decrypt');
//show-hide buttons and div, onchange cipher mode
function set_cypher(){
encrypt_decrypt_button.style.display = 'none';
Shifted_atbash_button.style.display = 'none';
Beaufort_button.style.display = 'none';
Atbash_button.style.display = 'none';
Vizhener_encrypt_button.style.display = 'none';
Vizhener_decrypt_button.style.display = 'none';
Gronsfeld_encrypt_button.style.display = 'none';
Gronsfeld_decrypt_button.style.display = 'none';
tritemius_encrypt_button.style.display = 'none';
tritemius_decrypt_button.style.display = 'none';
XOR_char_element.style.display = 'none';
//Then enable selected
if(Shifted_atbash_mode.checked){
// console.log('shifted_atbash');
Shifted_atbash_button.style.display = 'inline-block';
progress_text(undefined, undefined, undefined, undefined, 'shifted_atbash', undefined, undefined);
}
else if(Vernam_mode.checked){
// console.log('Vernam_mode');
encrypt_decrypt_button.style.display = 'inline-block';
encrypt_decrypt_button.style.display = 'inline-block';
XOR_char_element.style.display = 'inline-block';
progress_text(undefined, undefined, undefined, undefined, 'vernam', undefined, XOR_char_element.value);
}
else if(Vizhener_mode.checked){
// console.log('Vizhener');
Vizhener_encrypt_button.style.display = 'inline-block';
Vizhener_decrypt_button.style.display = 'inline-block';
progress_text(undefined, undefined, undefined, undefined, 'vizhener', undefined, undefined);
}
else if(Beaufort_mode.checked){
// console.log('Beaufort');
Beaufort_button.style.display = 'inline-block';
progress_text(undefined, undefined, undefined, undefined, 'beaufort', undefined, undefined);
}
else if(Atbash_mode.checked){
// console.log('Atbash_mode');
Atbash_button.style.display = 'inline-block';
progress_text(undefined, undefined, undefined, undefined, 'atbash', undefined, undefined);
}
else if(Gronsfeld_mode.checked){
// console.log('Gronsfeld');
Gronsfeld_encrypt_button.style.display = 'inline-block';
Gronsfeld_decrypt_button.style.display = 'inline-block';
progress_text(undefined, undefined, undefined, undefined, 'gronsfeld', undefined, undefined);
}
else if(Tritemius_mode.checked){
// console.log('Tritemius_mode');
tritemius_encrypt_button.style.display = 'inline-block';
tritemius_decrypt_button.style.display = 'inline-block';
progress_text(undefined, undefined, undefined, undefined, 'tritemius', undefined, undefined);
}
//recalculate text in textareas:
setTimeout(
function(){
progress_text( prng.param_get('algo'), true );
},
500
);
}
set_cypher(); //run onload page.
//function to change XOR_char from input - to custom symbol
function change_XOR_char(){
if(alphabet.indexOf(XOR_char_element.value)===-1){
progress_text(undefined, undefined, undefined, undefined, undefined, undefined, XOR_char_element.value);
show_hide_border(XOR_char_element, "green", border_size);
prng.param_set('XOR_char', XOR_char_element.value);
update_textareas_vernam_cipher(XOR_char_element.value);
}else{
progress_text(undefined, undefined, undefined, undefined, undefined, undefined, '�'); //set default XOR_char
show_hide_border(XOR_char_element, "red", border_size);
if(XOR_char_element.value !== ''){
setTimeout(
function(){
XOR_char_element.value = '�';
prng.param_set('XOR_char', XOR_char_element.value);
update_textareas_vernam_cipher(XOR_char_element.value);
}, 500
)
}
}
//console.log("prng.param_get('XOR_char')", prng.param_get('XOR_char'));
}
//function to shuffle alphabet, after set seed...
/**
* Shuffles array in place.
* @param {Array} a items An array containing the items.
*/
function shuffle(a) {
if(seed_element.value===''){return a;}
var j, x, i;
for (i = a.length - 1; i > 0; i--) {
j = prng.range(0, a.length-1); //deterministic shuffle, after seeding.
x = a[i];
a[i] = a[j];
a[j] = x;
}
return a;
}
//Usage:
//var myArray = ['1','2','3','4','5','6','7','8','9'];
//console.log(myArray);
//shuffle(myArray);
//console.log(myArray);
//function to show-hide boder for textareas.
var border_size = 2;
function show_hide_border(element, color, px){
element.style.border = px+"px solid "+color;
setTimeout(
function(){
element.style.border = "";
}
, 500
);
}
var not_shuffled_alphabet;
var shuffled_alphabet;
function change_alphabet_to_not_shuffled(){
shuffled_alphabet = alphabet;
progress_text(undefined, undefined, undefined, undefined, undefined, not_shuffled_alphabet, undefined);
}
var shuffle_alphabet = document.getElementById('shuffle');
var not_shuffle = document.getElementById('not_shuffle');
//function to set specified alphabet
function setAlphabet(element){
var symbols = element.value; //get alphabet string
not_shuffled_alphabet = symbols; //save not shuffled alphabet
if(shuffle_alphabet.checked){
symbols = symbols.split(''); //to array
shuffle(symbols); //shuffle it
symbols = symbols.join(''); //convert to string
}
if(Vernam_mode.checked){
prng.param_set('XOR_char', XOR_char_element.value); //update XOR-symbol, for vernam cipher, if need.
}
progress_text('', '', '', '', '', symbols);
shuffled_alphabet_pre.innerHTML = prng.param_set('alphabet', symbols);
show_hide_border(element, "green", border_size);
}
//textareas elements, as variables:
var method_single = document.getElementById('single');
var method_block = document.getElementById('block');
var startbtn = document.getElementById('startbtn');
var txt_in = document.getElementById('txt_in');
var init_txtinput = document.getElementById('textin');
var msg_in = document.getElementById('msg_in');
var msg_out = document.getElementById('msg_out');
var seed_element = document.getElementById('seed');
function generate_seed(){
//fill seed value, depending current unix timestamp
seed_element.value = (new Date())+(new Date().getTime()); //current date and time + unix timestamp - as seed string.
}
generate_seed();
//function to set seed in generator
function change_seed(seed_source_element){
var sha512_hash = sha512(seed_source_element.value);
// console.log('seed: '+sha512_hash);
prng.seed(sha512_hash); //set seed
//console.log("New seed updated.");
setAlphabet(document.getElementById('alphabet')); //set alphabet and shuffle this.
show_hide_border(seed_source_element, "green", border_size);
}
//clear filled textareas
function clear_textareas(){
msg_in.value = '';
msg_out.value = '';
generated_key_value.value = '';
txt_in.value = '';
input_key_value.value = '';
change_seed(seed_element);
}
//select method to input text
//symbol-by-symbol
method_single.onclick = function() {
document.getElementById('blockinput').style.display = "none";
show_hide_border(init_txtinput, "blue", border_size);
}
//or as text block
method_block.onclick = function() {
document.getElementById('blockinput').style.display = "";
show_hide_border(txt_in, "blue", border_size);
}
//textareas elements, as variables:
var show_key = document.getElementById('show_key');
var hide_key = document.getElementById('hide_key');
var generated_key_label = document.getElementById('generated_key_label');
var generated_key_value = document.getElementById('generated_key_value');
//show generated key
show_key.onclick = function() {
document.getElementById('generated_key_label').style.display = "block";
show_hide_border(generated_key_value, "blue", border_size);
}
//or hide this
hide_key.onclick = function() {
document.getElementById('generated_key_label').style.display = "none";
}
show_key.click(); //show, by default
//textareas elements, as variables:
var generate_key = document.getElementById('generate_key');
var input_key = document.getElementById('input_key');
var input_key_label = document.getElementById('input_key_label');
var input_key_value = document.getElementById('input_key_value');
//generate key, using CSPRNG
generate_key.onclick = function() {
document.getElementById('input_key_label').style.display = "none";
}
//or input this in the text block
input_key.onclick = function() {
document.getElementById('input_key_label').style.display = "block";
show_hide_border(input_key_value, "blue", border_size);
}
input_key.click(); //input by default. If value is empty - generate then.
var alphabet = prng.param_get('alphabet'); //get alphabet from variable, inside isaacCSPRNG.js
//progress one symbol
init_txtinput.onkeyup = function() {
if( alphabet.indexOf(this.value) === -1 ){
if(
Vernam_mode.checked
&& this.value === special_symbol_for_XOR_element.value
){
msg_in.value += this.value;
this.value = "";
this.focus();
return;
}
else{
this.value = "";
var elem = this;
show_hide_border(elem, "red", border_size);
}
}
for (i = 0; i < this.value.length; i++){
msg_in.value += this.value;
var result = prng.symbol(this.value);
if(msg_in.value[msg_in.value.length-2] === XOR_char_element.value){
result[0] = result[0].slice(1); //remove xor char from result
}
msg_out.value += result[0]; //cipher
generated_key_value.value += result[1]; //key
}
this.value = "";
this.focus();
};
//remove symbol on press Backspace or Delete buttons
init_txtinput.onkeydown = function(event) {
var key = event.keyCode || event.charCode;
if( key == 8 || key == 46 ) { //if backspace or delete key down
//remove one last char in three textareas: msg_in, msg_out, generated_key_value
msg_in.value = msg_in
.value
//.substring(0, msg_in.value.length-1)
.slice(0, msg_in.value.length-1)
;
//remove XOR-char
if( msg_in.value[msg_in.value.length-1] === XOR_char_element.value ){
msg_in.value = msg_in.value.slice(0, -1);
}
msg_out.value = msg_out
.value
.substring(0, msg_out.value.length-1)
//.slice(0, msg_out.value.length-1)
;
//remove XOR-char
if( msg_out.value[msg_out.value.length-1] === XOR_char_element.value ){
msg_out.value = msg_out.value.slice(0, -1);
}
generated_key_value.value = generated_key_value
.value
.substring(0, generated_key_value.value.length-1)
//.slice(0, msg_out.value.length-1)
;
change_seed(seed_element); //set previous seed again
prng.skip_bytes(msg_in.value.length); //skip generated bytes - up to new message length
}
};
//fill textarea by this value;
document.getElementById('alphabet').value = alphabet;
//update Vernam_XOR_symbol in specified textarea
function update_textarea_vernam_cipher(Vernam_XOR_symbol, textarea){
var str = '';
for(var i = 0; i<textarea.value.length; i++){
if(
(alphabet.indexOf(textarea.value[i]) === -1)
&& textarea.value[i] !== Vernam_XOR_symbol
){
str += Vernam_XOR_symbol;
}
else{
str += textarea.value[i];
}
}
textarea.value = str;
}
//update Vernam_XOR_symbol in all textareas, where vernam-cipher can contains.
function update_textareas_vernam_cipher(Vernam_XOR_symbol){
update_textarea_vernam_cipher(Vernam_XOR_symbol, msg_in);
update_textarea_vernam_cipher(Vernam_XOR_symbol, txt_in);
update_textarea_vernam_cipher(Vernam_XOR_symbol, msg_out);
}
//progress text run strings encryption/decryption
function progress_text(param, recalculate, text_cypher, key, change_algo, change_alphabet, Vernam_XOR_symbol){
if(typeof change_algo !== 'undefined' || typeof change_alphabet !== 'undefined' || typeof Vernam_XOR_symbol !== 'undefined'){
return prng.symbol(undefined, undefined, undefined, change_algo, change_alphabet, Vernam_XOR_symbol);
}
if(typeof key === 'undefined'){
key = document.getElementById('input_key_value').value;
key = (typeof key === 'undefined') ? undefined : key;
}
if(recalculate === true){
change_seed(seed_element);
}
var src = (typeof text_cypher === 'undefined')
? ( ( recalculate === true ) ? msg_in.value : txt_in.value )
: text_cypher
;
for(i=0; i<src.length; i++){
if(
(alphabet.indexOf(src[i]) === -1)
&& !(
(prng.param_get('algo') === 'vernam')
&& (src[i] === prng.param_get('XOR_char'))
)
){
show_hide_border(txt_in, "red", border_size);
return;
}
}
//var result = progress_string(param, src, key, change_algo, change_alphabet, Vernam_XOR_symbol);
var result = prng.progress_string(param, src, key, change_algo, change_alphabet, Vernam_XOR_symbol);
msg_in.value = src;
msg_out.value = result[0];
generated_key_value.value = (prng.param_get('algo')==='atbash') //if just atbash
?
(
(shuffle_alphabet.checked) //and if alphabet shuffled
? 'Alphabet shuffled, this is not key' //return this
: 'Atbash have not key' //or empty key if not shuffled.
)
: result[1] //else - key
;
return result[0];
}
/*
setTimeout(
function(){
//update_alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
prng.param_set('alphabet', "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
//algo = 'shifted_atbash';
//console.log('cipher text: ', progress_text('Encipher', false, "ATTACKATDAWN", "LEMONLEMONLE", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
//console.log('cipher text: ', progress_text('shifted_atbash', false, "ATTACKATDAWN", "LEMONLEMONLE"));
//console.log('cipher text: ', progress_text('vernam', false, "ATTACKATDAWN", "LEMONLEMONLE"));
//algo = 'vernam';
//console.log('cipher text: ', progress_text(algo, false, "ATTACKATDAWN", "LEMONLEMONLE"));
prng.progress_string(undefined, undefined, undefined, 'shifted_atbash');
console.log('cipher text: ', progress_text(prng.param_get('algo'), false, "ATTACKATDAWN", "LEMONLEMONLE"));
//change_seed(seed_element);
}
,1000
);
*/
/*
//TESTS
//just uncomment test(); in isaacCSPRNG.js and see console.log( F12 button );
*/
/*
//Generate the table for reversive shifted_atbash cipher algorithm:
prng.param_set('alphabet', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'); //set alphabet
prng.param_set('algo', 'shifted_atbash'); //set cipher mode to shifted_atbash
var alphabet = prng.param_get('alphabet');
var html = '<h1>Shifted_atbash table:</h1><table>';
for(i = 0; i<=alphabet.length; i++){
html += '<tr>';
if(i===0){html += '<td>\\</td>';}
for (j = 0; j<=alphabet.length; j++){
if(j===0){
if(i===0){continue;}
else{
html += '<td style="border: 2px solid red;">'+
(alphabet[(i-1)])
+'</td>';
}
}
if ( i !== 0 ){ if( j === alphabet.length ){ continue; } }
var cyf = prng.symbol(alphabet[i-1], undefined, j)[0];
html += '<td'+
(
(i==0)
?' style= "border: 2px solid green;">'+
(alphabet[((i+j-1)%alphabet.length)])
:'>'+
cyf
);
html += '</td>';
}
html += '</tr>';
}
html += '</table><br>';
shifted_atbash = false;
document.write(html); //show table for shifted atbash.
// Atbash encrypt/decrypt table
// | 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|
// | 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|
//
// Shifted Atbash encrypt/decrypt table
// | \| 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| 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|
// | B| 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| Z|
// | C| X| W| V| U| T| S| R| Q| P| O| N| M| L| K| J| I| H| G| F| E| D| C| B| A| Z| Y|
// | D| W| V| U| T| S| R| Q| P| O| N| M| L| K| J| I| H| G| F| E| D| C| B| A| Z| Y| X|
// | E| V| U| T| S| R| Q| P| O| N| M| L| K| J| I| H| G| F| E| D| C| B| A| Z| Y| X| W|
// | F| U| T| S| R| Q| P| O| N| M| L| K| J| I| H| G| F| E| D| C| B| A| Z| Y| X| W| V|
// | G| T| S| R| Q| P| O| N| M| L| K| J| I| H| G| F| E| D| C| B| A| Z| Y| X| W| V| U|
// | H| S| R| Q| P| O| N| M| L| K| J| I| H| G| F| E| D| C| B| A| Z| Y| X| W| V| U| T|
// | I| R| Q| P| O| N| M| L| K| J| I| H| G| F| E| D| C| B| A| Z| Y| X| W| V| U| T| S|
// | J| Q| P| O| N| M| L| K| J| I| H| G| F| E| D| C| B| A| Z| Y| X| W| V| U| T| S| R|
// | K| P| O| N| M| L| K| J| I| H| G| F| E| D| C| B| A| Z| Y| X| W| V| U| T| S| R| Q|
// | L| O| N| M| L| K| J| I| H| G| F| E| D| C| B| A| Z| Y| X| W| V| U| T| S| R| Q| P|
// | M| N| M| L| K| J| I| H| G| F| E| D| C| B| A| 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| Z| Y| X| W| V| U| T| S| R| Q| P| O| N|
// | O| L| K| J| I| H| G| F| E| D| C| B| A| Z| Y| X| W| V| U| T| S| R| Q| P| O| N| M|
// | P| K| J| I| H| G| F| E| D| C| B| A| Z| Y| X| W| V| U| T| S| R| Q| P| O| N| M| L|
// | Q| J| I| H| G| F| E| D| C| B| A| Z| Y| X| W| V| U| T| S| R| Q| P| O| N| M| L| K|
// | R| I| H| G| F| E| D| C| B| A| Z| Y| X| W| V| U| T| S| R| Q| P| O| N| M| L| K| J|
// | S| H| G| F| E| D| C| B| A| Z| Y| X| W| V| U| T| S| R| Q| P| O| N| M| L| K| J| I|
// | T| G| F| E| D| C| B| A| Z| Y| X| W| V| U| T| S| R| Q| P| O| N| M| L| K| J| I| H|
// | U| F| E| D| C| B| A| Z| Y| X| W| V| U| T| S| R| Q| P| O| N| M| L| K| J| I| H| G|
// | V| E| D| C| B| A| Z| Y| X| W| V| U| T| S| R| Q| P| O| N| M| L| K| J| I| H| G| F|
// | W| D| C| B| A| Z| Y| X| W| V| U| T| S| R| Q| P| O| N| M| L| K| J| I| H| G| F| E|
// | X| C| B| A| Z| Y| X| W| V| U| T| S| R| Q| P| O| N| M| L| K| J| I| H| G| F| E| D|
// | Y| B| A| Z| Y| X| W| V| U| T| S| R| Q| P| O| N| M| L| K| J| I| H| G| F| E| D| C|
// | Z| A| 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|
var a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //set custom alphabet
//fast "shifted atbash" function
//reversive encrypt/decrypt one symbol of message/cyphertext - m, using symbol of key - k.
function shifted_atbash_(m, k){
return a[ ( ( ( a.length * 2 ) - ( ( a.indexOf( m ) + a.indexOf( k ) ) + 1 ) ) % a.length ) ];
}
var m = "F";
var k = "U";
//Shifted atbash
var cypher = shifted_atbash_(m, k);
var restore_m = shifted_atbash_(cypher, k); //decryption - is again encryption, like for Vernam cipher
console.log('\ncypher', cypher, '\nrestore_m', restore_m, '\nEquals? (restore_m === m): ', (restore_m === m) );
//cypher = "A" = F (line/colunm) + U (column/line) = A (letter at intersection)
// = А (line/colunm) + U (letter inside) = A (column/line)
// As you can see, encryption and decryption is reversive and... Commutative!
// F + U = A;
// U + F = A;
// No any difference where is the key of message symbol, no any difference where is cyphertext symbol and where is key symbol.
// This cipher is commutative, like Vernam cipher with XOR operation, but... This is usable for CUSTOM ALPHABETS with any length.
// Atbash for cyphertext of this cipher return not reversive Vizhener cipher:
//Test
//ATTACKATDAWN
//LEMONLEMONLE
//Encrypt
console.log('\n\n\nShifted_atbash - Encrypt');
console.log('cypher', shifted_atbash_("A", "L"));
console.log('cypher', shifted_atbash_("T", "E"));
console.log('cypher', shifted_atbash_("T", "M"));
console.log('cypher', shifted_atbash_("A", "O"));
console.log('cypher', shifted_atbash_("C", "N"));
console.log('cypher', shifted_atbash_("K", "L"));
console.log('cypher', shifted_atbash_("A", "E"));
console.log('cypher', shifted_atbash_("T", "M"));
console.log('cypher', shifted_atbash_("D", "O"));
console.log('cypher', shifted_atbash_("A", "N"));
console.log('cypher', shifted_atbash_("W", "L"));
console.log('cypher', shifted_atbash_("N", "E"));
//Decrypt
console.log('\n\n\nShifted_atbash - Decrypt');
console.log('decrypted', shifted_atbash_("O", "L"));
console.log('decrypted', shifted_atbash_("C", "E"));
console.log('decrypted', shifted_atbash_("U", "M"));
console.log('decrypted', shifted_atbash_("L", "O"));
console.log('decrypted', shifted_atbash_("K", "N"));
console.log('decrypted', shifted_atbash_("E", "L"));
console.log('decrypted', shifted_atbash_("V", "E"));
console.log('decrypted', shifted_atbash_("U", "M"));
console.log('decrypted', shifted_atbash_("I", "O"));
console.log('decrypted', shifted_atbash_("M", "N"));
console.log('decrypted', shifted_atbash_("S", "L"));
console.log('decrypted', shifted_atbash_("I", "E"));
//atbash for cyphertext - return vizhener cipher:
//shifted_atbash cipher - to vizhener cipher
console.log('\n\n\nShifted_atbash - to Vizhener Cipher (Just atbash for shifted_atbash ciphertext)');
console.log('vizhener', a[a.length-1-a.indexOf( shifted_atbash_("A", "L") )] );
console.log('vizhener', a[a.length-1-a.indexOf( shifted_atbash_("T", "E") )] );
console.log('vizhener', a[a.length-1-a.indexOf( shifted_atbash_("T", "M") )] );
console.log('vizhener', a[a.length-1-a.indexOf( shifted_atbash_("A", "O") )] );
console.log('vizhener', a[a.length-1-a.indexOf( shifted_atbash_("C", "N") )] );
console.log('vizhener', a[a.length-1-a.indexOf( shifted_atbash_("K", "L") )] );
console.log('vizhener', a[a.length-1-a.indexOf( shifted_atbash_("A", "E") )] );
console.log('vizhener', a[a.length-1-a.indexOf( shifted_atbash_("T", "M") )] );
console.log('vizhener', a[a.length-1-a.indexOf( shifted_atbash_("D", "O") )] );
console.log('vizhener', a[a.length-1-a.indexOf( shifted_atbash_("A", "N") )] );
console.log('vizhener', a[a.length-1-a.indexOf( shifted_atbash_("W", "L") )] );
console.log('vizhener', a[a.length-1-a.indexOf( shifted_atbash_("N", "E") )] );
//atbash for vizhener cyphertext - return again shifted_atbash cipher:
//vizhener cipher - to shifted_atbash cipher
console.log('\n\n\nVizhener cipher - to Shifted_atbash (Just atbash for vizhener ciphertext)');
console.log('cypher', a[a.length-1-a.indexOf( a[a.length-1-a.indexOf( shifted_atbash_("A", "L") )] )] );
console.log('cypher', a[a.length-1-a.indexOf( a[a.length-1-a.indexOf( shifted_atbash_("T", "E") )] )] );
console.log('cypher', a[a.length-1-a.indexOf( a[a.length-1-a.indexOf( shifted_atbash_("T", "M") )] )] );
console.log('cypher', a[a.length-1-a.indexOf( a[a.length-1-a.indexOf( shifted_atbash_("A", "O") )] )] );
console.log('cypher', a[a.length-1-a.indexOf( a[a.length-1-a.indexOf( shifted_atbash_("C", "N") )] )] );
console.log('cypher', a[a.length-1-a.indexOf( a[a.length-1-a.indexOf( shifted_atbash_("K", "L") )] )] );
console.log('cypher', a[a.length-1-a.indexOf( a[a.length-1-a.indexOf( shifted_atbash_("A", "E") )] )] );
console.log('cypher', a[a.length-1-a.indexOf( a[a.length-1-a.indexOf( shifted_atbash_("T", "M") )] )] );
console.log('cypher', a[a.length-1-a.indexOf( a[a.length-1-a.indexOf( shifted_atbash_("D", "O") )] )] );
console.log('cypher', a[a.length-1-a.indexOf( a[a.length-1-a.indexOf( shifted_atbash_("A", "N") )] )] );
console.log('cypher', a[a.length-1-a.indexOf( a[a.length-1-a.indexOf( shifted_atbash_("W", "L") )] )] );
console.log('cypher', a[a.length-1-a.indexOf( a[a.length-1-a.indexOf( shifted_atbash_("N", "E") )] )] );
*/
</script>