forked from macmcmeans/isaacCSPRNG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
isaacCSPRNG-1.1.js
1379 lines (1082 loc) · 49.3 KB
/
isaacCSPRNG-1.1.js
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
/*///////////////////////////////////////////////////////////////////////////////////////////////////
isaacCSPRNG 1.1 (+ 7 cyphers with symbol_by_symbol encrypt/decrypt and using the custom keys).
/////////////////////////////////////////////////////////////////////////////////////////////////////
https://github.com/macmcmeans/isaacCSPRNG/blob/master/isaacCSPRNG-1.1.js
/////////////////////////////////////////////////////////////////////////////////////////////////////
This is a derivative work copyright (c) 2018, William P. "Mac" McMeans, under BSD license.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of isaacCSPRNG nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Original work copyright (c) 2012 Yves-Marie K. Rinquin, under MIT license.
https://github.com/rubycon/isaac.js
///////////////////////////////////////////////////////////////////////////////////////////////////*/
isaacCSPRNG = function( specifiedSeed ){
return (function( userSeed ) {
"use strict";
/* private: internal states */
var m = new Array( 256 ) // internal memory
, acc = 0 // accumulator
, brs = 0 // last result
, cnt = 0 // counter
, r = new Array( 256 ) // result array
, gnt = 0 // generation counter
;
var _version = '1.1';
////////////////////////////////////////////////////
/* initial random seed */
// seed( Math.random() * 0xffffffff ); // 4294967295
var internalSeed
, uinta = new Uint32Array( 2 )
, defaultInternalSeed = new Array()
;
window.crypto.getRandomValues( uinta );
defaultInternalSeed = uinta[ 0 ] + uinta[ 1 ];
internalSeed = userSeed || defaultInternalSeed;
seed( internalSeed );
////////////////////////////////////////////////////
/* private: 32-bit integer safe adder */
function _add( x, y ) {
var lsb = ( x & 0xffff ) + ( y & 0xffff )
, msb = ( x >>> 16 ) + ( y >>> 16 ) + ( lsb >>> 16 )
;
return ( msb << 16 ) | ( lsb & 0xffff );
};
/* private: return data converted from hex string */
function _hexDecode( data ){
var j
, hexes = data.match(/.{1,4}/g) || []
, back = ""
;
for( j = 0; j < hexes.length; j++ ) {
back += String.fromCharCode( parseInt( hexes[ j ], 16 ) );
}
return back;
};
/* private: return data converted to hex string */
function _hexEncode( data ){
var hex
, i
;
var result = "";
for( i = 0; i < data.length; i++ ) {
hex = data.charCodeAt( i ).toString( 16 );
result += ( "000" + hex ).slice( -4 );
}
return result;
};
/* private: return the CSPRNG _internals in an object (for get/set) */
function _internals() {
return {
a : acc
, b : brs
, c : cnt
, m : m
, r : r
, g : gnt
};
};
/* private: check if number is integer */
function _isInteger( n ) {
return parseInt( n ) === n;
};
/* private: convert string to integer array */
/* js string (ucs-2/utf16) to a 32-bit integer (utf-8 chars, little-endian) array */
function _toIntArray( string ) {
var w1
, w2
, u
, r4 = []
, r = []
, i = 0
, s = string + '\0\0\0' // pad string to avoid discarding last chars
, l = s.length - 1
;
while( i < l ) {
w1 = s.charCodeAt( i++ );
w2 = s.charCodeAt( i + 1 );
// 0x0000 - 0x007f code point: basic ascii
if( w1 < 0x0080 ) {
r4.push( w1 );
} else
// 0x0080 - 0x07ff code point
if( w1 < 0x0800 ) {
r4.push( ( ( w1 >>> 6 ) & 0x1f ) | 0xc0 );
r4.push( ( ( w1 >>> 0 ) & 0x3f ) | 0x80 );
} else
// 0x0800 - 0xd7ff / 0xe000 - 0xffff code point
if( ( w1 & 0xf800 ) != 0xd800 ) {
r4.push( ( ( w1 >>> 12 ) & 0x0f ) | 0xe0 );
r4.push( ( ( w1 >>> 6 ) & 0x3f ) | 0x80 );
r4.push( ( ( w1 >>> 0 ) & 0x3f ) | 0x80 );
} else
// 0xd800 - 0xdfff surrogate / 0x10ffff - 0x10000 code point
if( ( ( w1 & 0xfc00 ) == 0xd800 ) && ( ( w2 & 0xfc00 ) == 0xdc00 ) ) {
u = ( ( w2 & 0x3f ) | ( ( w1 & 0x3f ) << 10 ) ) + 0x10000;
r4.push( ( ( u >>> 18 ) & 0x07 ) | 0xf0 );
r4.push( ( ( u >>> 12 ) & 0x3f ) | 0x80 );
r4.push( ( ( u >>> 6 ) & 0x3f ) | 0x80 );
r4.push( ( ( u >>> 0 ) & 0x3f ) | 0x80 );
i++;
} else {
// invalid char
}
/* _add integer (four utf-8 value) to array */
if( r4.length > 3 ) {
// little endian
r.push(
( r4.shift() << 0 ) | ( r4.shift() << 8 ) | ( r4.shift() << 16 ) | ( r4.shift() << 24 )
);
}
}
return r;
};
/* private: return a Vernam (XOR) transform of msg */
function _vernam( msg ) {
var out = "";
for( var i = 0; i < msg.length; i++) {
var ra = range( 33, 126 );
out += String.fromCharCode( ra ^ msg.charCodeAt( i ) );
}
return out;
};
/* public: return an array of amount elements consisting of unsigned random integers in the range [0, 255] */
function bytes( amount ) {
var out = new Array( amount );
for( var i = 0; i < amount; i++ ) {
out[ i ] = range( 255 );
}
return out;
};
/* public: return a string of length (safe) characters consisting of random 7-bit ASCII graphemes */
function chars( length ) {
//var str = " ~`'\"_-+={}[]<>/\\,.:;?|!@#$%^&*()0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
var str = " ~`_-+={}[]<>/,.:;?|!@#$%^&*()0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
, out = ''
;
for( var i = 0; i < length; i++ ) {
out += str[ range( 0, str.length - 1 ) ];
}
return out;
};
/* public: return vernam transform on ciphertext string data/hex string data */
function decipher( key, msg, flag ) {
seed( key );
if( Number( flag ) === 1 ) {
return _vernam( _hexDecode( msg ) );
} else {
return _vernam( msg );
}
};
/* public: return a 53-bit fraction in the range [0, 1] */
function double() {
return random() + ( random() * 0x200000 | 0 ) * 1.1102230246251565e-16; // 2^-53
};
/* public: return vernam transform on plaintext string data/hex string data */
function encipher( key, msg, flag ) {
seed( key );
if( Number( flag ) === 1 ) {
return _hexEncode( _vernam( msg ) );
} else {
return _vernam( msg );
}
};
/* public: export object describing CSPRNG internal state */
function get() {
return JSON.stringify( _internals() );
};
/* public: return an unsigned random integer in the range [0, 2^32] */
function int32() {
var _r = rand();
return _r < 0 ? -_r : _r;
};
/* public: expose internals */
function internals() {
return {
a: acc
, b: brs
, c: cnt
, m: m
, r: r
};
};
/* public: isaac generator, n = number of runs */
function prng( n ){ //each run rotate PRNG by 256 bytes.
var i
, x
, y
;
n = ( n && typeof n === 'number' ? Math.abs( Math.floor( n ) ) : 1 );
while( n-- ) {
cnt = _add( cnt, 1 );
brs = _add( brs, cnt );
for( i = 0; i < 256; i++ ) {
switch( i & 3 ) {
case 0: acc ^= acc << 13; break;
case 1: acc ^= acc >>> 6; break;
case 2: acc ^= acc << 2; break;
case 3: acc ^= acc >>> 16; break;
}
acc = _add( m[ ( i + 128 ) & 0xff ], acc ); x = m[ i ];
m[i] = y = _add( m[ ( x >>> 2 ) & 0xff ], _add( acc, brs ) );
r[i] = brs = _add( m[ ( y >>> 10 ) & 0xff ], x );
}
}
};
//skip k bytes.
function skip_bytes(k){
var rounds = 0;
if(k>256){
rounds = Math.floor(k/256);
k = k % 256;
}
if(rounds!==0){
prng.bytes(rounds);
}
bytes(k);
}
/* public: return a signed random integer in the range [-2^31, 2^31] */
function rand() {
if( !gnt-- ) {
prng();
gnt = 255;
}
return r[ gnt ];
};
/* public: return a 32-bit fraction in the range [0, 1] */
function random() {
return 0.5 + rand() * 2.3283064365386963e-10; // 2^-32
};
/* public: return inclusive range */
function range() {
var loBound
, hiBound
;
if( arguments.length === 1 ) {
loBound = 0;
hiBound = arguments[ 0 ];
} else {
loBound = arguments[ 0 ];
hiBound = arguments[ 1 ];
}
if( arguments[ 0 ] > arguments[ 1 ] ) {
loBound = arguments[ 1 ];
hiBound = arguments[ 0 ];
}
// return integer
if( _isInteger( loBound ) && _isInteger( hiBound ) ) {
return Math.floor( random() * ( hiBound - loBound + 1 ) ) + loBound;
// return float
} else {
return random() * ( hiBound - loBound ) + loBound;
}
};
/* public: zeroize the CSPRNG */
function reset() {
acc = brs = cnt = 0;
for( var i = 0; i < 256; ++i ) {
m[ i ] = r[ i ] = 0;
}
gnt = 0;
};
/* public: seeding function */
function seed( seed ) {
var a
, b
, c
, d
, e
, f
, g
, h
, i
;
/* seeding the seeds of love */
a = b = c = d = e = f = g = h =
/* the golden ratio ( 2654435769 ),
see https://stackoverflow.com/questions/4948780/magic-number-in-boosthash-combine
*/
0x9e3779b9
;
if( seed && typeof seed === 'string' ) { seed = _toIntArray( seed ); }
if( seed && typeof seed === 'number' ) { seed = [ seed ]; }
if( seed instanceof Array ) {
reset();
for( i = 0; i < seed.length; i++ ) {
r[ i & 0xff ] += ( typeof seed[ i ] === 'number' ? seed[ i ] : 0 );
}
}
/* private: seed mixer */
function _seed_mix() {
a ^= b << 11; d = _add( d, a ); b = _add( b, c );
b ^= c >>> 2; e = _add( e, b ); c = _add( c, d );
c ^= d << 8; f = _add( f, c ); d = _add( d, e );
d ^= e >>> 16; g = _add( g, d ); e = _add( e, f );
e ^= f << 10; h = _add( h, e ); f = _add( f, g );
f ^= g >>> 4; a = _add( a, f ); g = _add( g, h );
g ^= h << 8; b = _add( b, g ); h = _add( h, a );
h ^= a >>> 9; c = _add( c, h ); a = _add( a, b );
}
/* scramble it */
for( i = 0; i < 4; i++ ) { _seed_mix(); }
for( i = 0; i < 256; i += 8 ) {
/* use all the information in the seed */
if( seed ) {
a = _add( a, r[ i + 0 ] ); b = _add( b, r[ i + 1 ] );
c = _add( c, r[ i + 2 ] ); d = _add( d, r[ i + 3 ] );
e = _add( e, r[ i + 4 ] ); f = _add( f, r[ i + 5 ] );
g = _add( g, r[ i + 6 ] ); h = _add( h, r[ i + 7 ] );
}
_seed_mix();
/* fill in m[] with messy stuff */
m[ i + 0 ] = a; m[ i + 1 ] = b; m[ i + 2 ] = c; m[ i + 3 ] = d;
m[ i + 4 ] = e; m[ i + 5 ] = f; m[ i + 6 ] = g; m[ i + 7 ] = h;
}
/* do a second pass to make all of the seed affect all of m[] */
if( seed ) {
for( i = 0; i < 256; i += 8 ) {
a = _add( a, m[ i + 0 ] ); b = _add( b, m[ i + 1 ] );
c = _add( c, m[ i + 2 ] ); d = _add( d, m[ i + 3 ] );
e = _add( e, m[ i + 4 ] ); f = _add( f, m[ i + 5 ] );
g = _add( g, m[ i + 6 ] ); h = _add( h, m[ i + 7 ] );
_seed_mix();
/* fill in m[] with messy stuff (again) */
m[ i + 0 ] = a; m[ i + 1 ] = b; m[ i + 2 ] = c; m[ i + 3 ] = d;
m[ i + 4 ] = e; m[ i + 5 ] = f; m[ i + 6 ] = g; m[ i + 7 ] = h;
}
}
/* fill in the first set of results */
prng();
/* prepare to use the first set of results */;
gnt = 256;
};
/* public: import object and use it to set CSPRNG internal state */
function set( incoming ) {
var imported = JSON.parse( incoming );
acc = imported.a;
brs = imported.b;
cnt = imported.c;
m = imported.m;
r = imported.r;
gnt = imported.g;
};
/* public: show version */
function version() {
return _version;
};
//Begin source code for 7 cyphers:
//BEGIN CIPHERS BLOCK
//all modes
var modes = [
//Reversive
'vernam',
'shifted_atbash',
'beaufort',
'atbash',
//Not reversive
'tritemius',
'gronsfeld',
'vizhener'
];
var algo = 'vernam'; //define current mode
/*
//This 7 cyphers working with castom keys and can be used without isaacCSPRNG.
//isaacCSPRNG key = seed + message.length
//________________________________________________________________________________________________________________________________________
//USAGE:
var prng = isaacCSPRNG(); //Define generator object with isaacCSPRNG
//1. Update alphabet to custom alphabet.
// directly:
console.log( prng.param_set('alphabet', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ') );
// or on try to encrypt-decrypt strings with new alphabet:
console.log( prng.progress_string('', '', '', '', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', '') );
// or on try to encrypt-decrypt symbol with new alphabet:
console.log( prng.symbol('', '', '', '', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', '') );
//2. Get current alphabet:
console.log( prng.param_get('alphabet') );
//3. Show all cipher algorithms:
console.log(prng.param_get('algos')); //["vernam", "shifted_atbash", "beaufort", "atbash", "tritemius", "gronsfeld", "vizhener"]
//4. Set and update curent cipher algorithm.
// directly:
//reversive
console.log( prng.param_set('algo', 'vernam') );
console.log( prng.param_set('algo', 'shifted_atbash') );
console.log( prng.param_set('algo', 'beaufort') );
console.log( prng.param_set('algo', 'atbash') );
//not reversive
console.log( prng.param_set('algo', 'tritemius') );
console.log( prng.param_set('algo', 'gronsfeld') );
console.log( prng.param_set('algo', 'vizhener') );
// or on try to encrypt-decrypt strings with new algo:
//reversive
console.log( prng.progress_string('', '', '', 'vernam') );
console.log( prng.progress_string('', '', '', 'shifted_atbash') );
console.log( prng.progress_string('', '', '', 'beaufort') );
console.log( prng.progress_string('', '', '', 'atbash') );
//not reversive
console.log( prng.progress_string('', '', '', 'tritemius') );
console.log( prng.progress_string('', '', '', 'gronsfeld') );
console.log( prng.progress_string('', '', '', 'vizhener') );
// or on try to encrypt-decrypt symbol with new algo:
//reversive
console.log( prng.symbol('', '', '', 'vernam') );
console.log( prng.symbol('', '', '', 'shifted_atbash') );
console.log( prng.symbol('', '', '', 'beaufort') );
console.log( prng.symbol('', '', '', 'atbash') );
//not reversive
console.log( prng.symbol('', '', '', 'tritemius') );
console.log( prng.symbol('', '', '', 'gronsfeld') );
console.log( prng.symbol('', '', '', 'vizhener') );
//5. Show current algo:
console.log( prng.param_get('algo') );
//6. Set XOR_char for vernam encrypt-decrypt:
console.log( prng.param_set('XOR_char', '+') ); //directly
console.log( prng.progress_string(undefined, '', undefined, '', undefined, '♥') ); // or on try to encrypt-decrypt strings with new algo
console.log( prng.symbol('', undefined, '', undefined, '', '▲') ); // or on try to encrypt-decrypt symbol with new algo
//7. Get current XOR_char:
console.log( prng.param_get('XOR_char') );
//8. Encrypt/decrypt symbol-by-symbol, with reversive and not reversive cipher algorithms, using CSPRNG or specified keys:
console.log( prng.param_set('algo', 'vernam') );
console.log( prng.param_set('alphabet', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ') );
console.log( prng.param_set('XOR_char', '♥') );
//Encrypt:
console.log( prng.seed('My_super_secret_start_seed') ); // set start seed
console.log( prng.symbol('B') ); // return array [cypher_symbol, key_symbol] //["T", "S"] //CSPRNG values for this seed
console.log( prng.symbol('B') ); // return new array [cypher_symbol, key_symbol] // ["N", "M"] //CSPRNG values for this seed
//Key is generated, using CSPRNG, according start seed.
//Decrypt
//set values
console.log( prng.param_set('algo', 'vernam') );
console.log( prng.param_set('alphabet', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ') );
console.log( prng.param_set('XOR_char', '♥') );
//decrypt with specified key
console.log(prng.symbol('T', '', 'S')); // ["B", "S"] //text + specified key
console.log(prng.symbol('N', undefined, 'M')); // ["B", "M"] //text + specified key
//or decrypt, using CSPRNG
console.log(prng.seed('My_super_secret_start_seed'));
console.log(prng.symbol('T')); // ["B", "S"] //text + CSPRNG key
console.log(prng.symbol('N')); // ["B", "M"] //text + CSPRNG key
//Same for another reversive cipher algos...
console.log( prng.param_set('algo', 'vernam') );
console.log( prng.param_set('algo', 'shifted_atbash') );
console.log( prng.param_set('algo', 'beaufort') );
console.log( prng.param_set('algo', 'atbash') );
// + previous code ...
//For not reversive algorithms Need specify second parameter "[En]crypt", "[En]cipher" or "[De]crypt", "[De]cipher":
//console.log( prng.symbol('', '', '', 'tritemius') );
//console.log( prng.symbol('', '', '', 'gronsfeld') );
//console.log( prng.symbol('', '', '', 'vizhener') );
//Example not reversive - encrypt:
console.log( prng.symbol('', '', '', 'vizhener') );
console.log( prng.param_set('alphabet', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ') );
console.log(prng.seed('My_super_secret_start_seed'));
console.log( prng.symbol('B', 'Encipher') ); //["C", "B"] //cyphertext + CSPRNG key
console.log( prng.symbol('B', 'Encrypt') ); //["S", "R"] //cyphertext + CSPRNG key
console.log( prng.symbol('B', 'Encalculate') ); //["Q", "P"] //cyphertext + CSPRNG key
//Example not reversive - decrypt with CSPRNG:
console.log( prng.symbol('', '', '', 'vizhener') );
console.log( prng.param_set('alphabet', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ') );
console.log(prng.seed('My_super_secret_start_seed'));
console.log( prng.symbol('C', 'Decipher') ); //text + CSPRNG key
console.log( prng.symbol('S', 'Decrypt') ); //text + CSPRNG key
console.log( prng.symbol('Q', 'Decalculate') ); //text + CSPRNG key
//Example not reversive - decrypt with specified key:
console.log( prng.symbol('C', 'Decipher', 'B') ); //text + specified key
console.log( prng.symbol('S', 'Decrypt', 'R') ); //text + specified key
console.log( prng.symbol('Q', 'Decalculate', 'P') ); //text + specified key
//8. Encrypt/decrypt strings, with reversive cipher algorithms, using CSPRNG or using specified keys:
console.log( prng.param_set('algo', 'vernam') );
console.log( prng.param_set('alphabet', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ') );
console.log( prng.param_set('XOR_char', '▲') );
//Encrypt:
console.log( prng.seed('My_super_secret_start_seed') ); // set start seed
console.log( prng.progress_string('Encrypt', 'THISISMYTEXT') );
// return array [cypher_string, key_string] //["BLRRNHUMVWEC", "SMZDFVYUGSTR"] //CSPRNG values for this seed
console.log( prng.progress_string('Encrypt', 'THISISMYTEXT') );
// return new array [cypher_symbol, key_symbol] //["▲KOJU▲OHHZ▲ODRQ", "JJBGWVLBNHGD"] //CSPRNG values for this seed
//Decrypt
//set values
console.log( prng.param_set('algo', 'vernam') );
console.log( prng.param_set('alphabet', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ') );
console.log( prng.param_set('XOR_char', '▲') );
//decrypt with specified key
console.log(prng.progress_string('Encrypt', "BLRRNHUMVWEC", "SMZDFVYUGSTR"));
//["THISISMYTEXT", "SMZDFVYUGSTR"] //text + specified key
console.log(prng.progress_string('Encrypt', "▲KOJU▲OHHZ▲ODRQ", "JJBGWVLBNHGD"));
//["THISISMYTEXT", "JJBGWVLBNHGDHGD"] //text + specified key
//or decrypt, using CSPRNG
console.log(prng.seed('My_super_secret_start_seed')); //set start seed
//decrypt cyphertext without specify key (key generated, using CSPRNG):
console.log(prng.progress_string('Encrypt', "BLRRNHUMVWEC")); // ["THISISMYTEXT", "SMZDFVYUGSTR"] //text + CSPRNG key
console.log(prng.progress_string('', "▲KOJU▲OHHZ▲ODRQ")); // ["THISISMYTEXT", "JJBGWVLBNHGD"] //text + CSPRNG key
// Same for another cipher algorithms:
console.log( prng.param_set('algo', 'vernam') );
console.log( prng.param_set('algo', 'shifted_atbash') );
console.log( prng.param_set('algo', 'beaufort') );
console.log( prng.param_set('algo', 'atbash') ); //no need any key, just alphabet, and this can be shuffled.
//9. Encrypt/decrypt strings, with CSPRNG key or with specified key for not reversive cipher algorithms:
//console.log( prng.symbol('', '', '', 'tritemius') );
//console.log( prng.symbol('', '', '', 'gronsfeld') );
//console.log( prng.symbol('', '', '', 'vizhener') );
// Just specify encrypt or decrypt option in first parameter.
// Example:
console.log( prng.param_set('algo', 'gronsfeld') ); //ONLY DIGITS FOR GRONSFELD KEY
console.log( prng.param_set('alphabet', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ') );
//encrypt with CSPRNG generated key
console.log( prng.seed('My_super_secret_start_seed') ); // set start seed
console.log( prng.progress_string('Encrypt', 'THISISMYTEXT') ); //["WIRTIUTGBGFY", "319102788285"] //ciphertext + key
console.log( prng.progress_string('Encipher', 'THISISMYTEXT') ); //["XMMXNTNCXHEB", "454551144378"] //ciphertext + key
console.log( prng.progress_string('Encalculate', 'THISISMYTEXT') ); //["YJJTOTOZCHYX", "521161219314"] //ciphertext + key
//decrypt with CSPRNG generated key
console.log( prng.seed('My_super_secret_start_seed') ); // set start seed
console.log( prng.progress_string('Decalculate', 'WIRTIUTGBGFY') ); //["THISISMYTEXT", "319102788285"] //text + CSPRNG key
console.log( prng.progress_string('Decipher', 'XMMXNTNCXHEB') ); //["THISISMYTEXT", "454551144378"] //text + CSPRNG key
console.log( prng.progress_string('Decrypt', 'YJJTOTOZCHYX') ); //["THISISMYTEXT", "521161219314"] //text + CSPRNG key
//decrypt with specified previous gronsfeld key
console.log( prng.progress_string('Decalculate', 'WIRTIUTGBGFY', '319102788285' ) ); //["THISISMYTEXT", "319102788285"]
console.log( prng.progress_string('Decipher', 'XMMXNTNCXHEB', '454551144378' ) ); //["THISISMYTEXT", "454551144378"]
console.log( prng.progress_string('Decrypt', 'YJJTOTOZCHYX', '521161219314' ) ); //["THISISMYTEXT", "521161219314"]
//Encrypt with specified short gronsfeld key
console.log( prng.progress_string('Encalculate', 'THISISMYTEXT', '12345') ); //["UJLWNTOBXJYV", "123451234512"] //cyphertext + key. Repeat short key up to message length.
console.log( prng.progress_string('Encipher', 'THISISMYTEXT', '12345') ); //["UJLWNTOBXJYV", "123451234512"]
console.log( prng.progress_string('Encrypt', 'THISISMYTEXT', '54321') ); //["YLLUJXQBVFCX", "543215432154"]
//decrypt with specified previous short gronsfeld key
console.log( prng.progress_string('Decrypt', 'UJLWNTOBXJYV', '12345') ); //["THISISMYTEXT", "123451234512"] //short key will be repeated automatically.
console.log( prng.progress_string('Decipher', 'UJLWNTOBXJYV', '123451234512') ); //["THISISMYTEXT", "123451234512"] //full key used for decryption with the same result
console.log( prng.progress_string('Decalculate', 'YLLUJXQBVFCX', '54321') ); //["THISISMYTEXT", "543215432154"] //second short key return the same text.
//________________________________________________________________________________________________________________________________________
//That's all.
*/
//start alphabet
var alphabet =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЬЪЫЭЮЯабвгдеёжзийклмнопрстуфхцчшщьъыэюя .,!?-[]()/"+
(
//symbols esaped with backslash
"\\"+"\n"
);
//var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //or like in enigma - 26 letters
//XOR_char for vernam key
/*
Vernam cipher using XOR.
This working good when alphabet length is equal of 2^N (2^N-1, including 0);
So, when alphabet length is 2^N, for example 32 symbols (2^5), XOR can working good.
But for alphabets with length not equal 2^N need XOR_char.
Why?
For example, alphabet is:
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
and alphabeg.length = 26;
Encrypted symbol code = 25 (decimal) = 11001 (binary);
key = 00100 (binary) = 4 (decimal)
encrypted symbol: 11001 XOR 00100 = 11101 (binary) = 29 (decimal) - after alphabet.
How to resolve this?
1. Don't XOR to values, up to max value before alphabet length, and equals 2^N.
As you can see, vernam key with value 00100 have 5 bits,
but must to have 4 bits. (2^4) = 16 - 1 = 15 (including 0)
That means key can have value from 0 to 1111 maximum.
2. When symbol code 11001 or result 11101 > 1111
Just XOR this to 10000 and add XOR_char which not contains in alphabet.
Then add result.
Example:
11001 XOR 0100 = (11001>1111) XOR 0100 =
XOR_char + (11001 XOR 10000) XOR 0100 =
XOR_char + 01001 XOR 0100 =
XOR_char + 01001 XOR 0100 =
XOR_char + 1101.
3. When XOR_char found, XOR at 10000 again:
XOR the result:
XOR_char + 1101 XOR 0100 =
(1101 XOR 10000) XOR 0100 =
11101 XOR 10000 XOR 0100 = 11001
Or XOR the symbol:
XOR_char + 1101 XOR 0100 =
1101 XOR 0100 =
1001 XOR (10000) = 11001
In this case Vernam cypher can be used for custom alphabets with any alphabet.length
*/
//for vernam mode need to define this variables:
var alphabet_length; //current lenght of alphabet
var as_binary_string; //length, as binary string
var binary_digits; //number of binary digits
var minus_left_one; //nulled left one.
var minus_left_one_to_bin_string; //As binary string.
var minus_left_one_to_bin_string_length; //max bitlength.
var ones; //max value for XOR
var ones_bin_string; //bitlength ones binary string
var N_pow_2; //ones + 1 symbol (100...0, 2^N) - for additional XOR
var wait_next_symbol; //true if need to wait next symbol, after XOR_char
var XOR_char; //XOR_char value
var default_XOR_char = '�'; //if previous undefined - use this value. It's any char which not contains in alphabet '�', '▲', '+', '*', '♥', etc...
var insert_symbol; //true when XOR_char was been inserted.
//function to update variables for vernam successfully XOR
function set_VERNAM_variables_for_XOR(){
//for vernam mode need to calculate the following variables:
alphabet_length = alphabet.length-1; //-1 symbol (first symbol, as XOR_char)
as_binary_string = alphabet_length.toString(2); //length, as binary string
binary_digits = as_binary_string.length; //number of binary digits
minus_left_one = (alphabet_length>>1); //nulled left one.
minus_left_one_to_bin_string = (alphabet_length>>1).toString(2); //As binary string.
minus_left_one_to_bin_string_length = minus_left_one_to_bin_string.length; //bitlength.
ones = (Math.pow(2, minus_left_one_to_bin_string_length)-1) //bitlength ones
ones_bin_string = ones.toString(2); //bitlength ones binary string
N_pow_2 = ones+1; //ones + 1 symbol (1000000, 2^N)
//show this all
// console.log(
// '\nalphabet_length', alphabet_length
// , '\ntoString(2)', as_binary_string
// , '\ntoString(2).length', binary_digits
// , '\nalphabet_length>>1', minus_left_one
// , '\n(alphabet_length>>1).toString(2)', minus_left_one_to_bin_string
// , '\n(alphabet_length>>1).toString(2).length', minus_left_one_to_bin_string_length
// , '\nMath.pow(2, (alphabet_length>>1).toString(2).length)', ones
// , '\nMath.pow(2, (alphabet_length>>1).toString(2).length)', ones_bin_string
// );
wait_next_symbol = false;
XOR_char = XOR_char || default_XOR_char;
insert_symbol = false;
}
function param_get(param){
if(typeof param !== 'undefined'){
if(param === 'algo') return algo;
else if(param === 'algos') return modes;
else if(param === 'alphabet') return alphabet;
else if(param === 'XOR_char') return XOR_char;
}
}
function param_set(param, value){
if(typeof param !== 'undefined'){
if(param === 'algo'){
algo = value;
if(algo==='vernam'){set_VERNAM_variables_for_XOR();}
console.log('algo updated: ', algo, ( (algo === 'vernam') ? ', XOR_char: '+XOR_char : '' ) );
return algo;
}
if(param === 'alphabet'){
alphabet = value.split('').filter(function(item, i, ar){ return ar.indexOf(item) === i; }).join(''); //only unique symbols.
if(algo==='vernam'){set_VERNAM_variables_for_XOR();}
//console.log('alphabet updated: ', alphabet);
return alphabet;
}
if(param === 'XOR_char'){
XOR_char = value;
console.log('XOR_char', XOR_char);
return XOR_char;
}
}
}
//encrypt-decrypt
function symbol(m, param, key, change_algo, change_alphabet, Vernam_XOR_symbol){ //m - message symbol, param - encrypt/decrypt, key - custom symbol from key;
if(typeof change_algo !== 'undefined' && modes.indexOf(change_algo)!==-1 ){
param_set('algo', change_algo)
return change_algo;
}
else if(typeof change_alphabet !== 'undefined' && alphabet.length !==0 ){
param_set('alphabet', change_alphabet);
return change_alphabet;
}
else if(
typeof Vernam_XOR_symbol !== 'undefined'
){
var notify = 'Special symbol for XOR (vernam cipher) was been changed to symbol: ';
if(Vernam_XOR_symbol.length === 1 && alphabet.indexOf(Vernam_XOR_symbol)===-1){
param_set('XOR_char', Vernam_XOR_symbol);
console.log(notify, XOR_char);
return XOR_char;
}else if(Vernam_XOR_symbol.length===0 || Vernam_XOR_symbol.length > 1 || alphabet.indexOf(Vernam_XOR_symbol)!==-1){
if(Vernam_XOR_symbol.length > 1){
console.log('maximum one character for XOR_char. Current length = ', Vernam_XOR_symbol.length);
}
param_set('XOR_char', default_XOR_char); //set default XOR_char if string was been empty.
console.log(notify, XOR_char);
return XOR_char;
}else{
console.log('Another case for XOR_char; return false;');
return false;
}
}
if(typeof key !== 'undefined'){ //if key specified
if(typeof key === 'string'){ //and if this symbol is string
var index =
(
( algo === 'gronsfeld' ) //if gronsfeld key
?
parseInt(key) //get digit from numeric gronsfeld key, if this was been numeric string
//alphabet.indexOf(key) //or get index of this symbol from alphabet, if this was been encoded as letters.
:
alphabet.indexOf(key) //or get index of this symbol from alphabet
)
;
if(index===-1){ //if not found, maybe this is number as string
key = parseInt(key); //parseInt of this number
}else{
key = index; //else key = index of specified symbol
}
}
}
if( algo === 'atbash' ){ //atbash for one symbol
//just atbash
return [ ( alphabet[ alphabet.length-1 - alphabet.indexOf(m) ] ), '' ];
}
else if( algo === 'vernam' ){
//Vernam cipher - with spesial_symbol:
var index = alphabet.indexOf(m); //index of input symbol
var k; //define k
if(index === -1){ //if symbol not found in alphabet
if(m === XOR_char){
wait_next_symbol = true;
return ['', '']; //return empty string as cyphertext and key
} //If this was been XOR_char - return empty string and wait next symbol
else{ //else
var error = 'symbol '+m+' not found in alphabet: '+alphabet;
console.log(error); //show error
return error; //return error
}
}
k = (typeof key === 'undefined') ? range(0, alphabet.length-1) : key; //key up to alphabet.length-1
var xorred = index ^ k; //XOR indexes.
if( (xorred > alphabet.length-1) && (wait_next_symbol === false) ) { //If index greater than alphabet.length
xorred = xorred ^ Math.pow(2, minus_left_one_to_bin_string_length); //discard greatest one bit in the number
insert_symbol = true; //set this as true
}
xorred =
(wait_next_symbol === true) //If need to wait next symbol
? (Math.pow(2, minus_left_one_to_bin_string_length) ^ xorred) //add greatest one bit to the number
: xorred; //or return result
if(wait_next_symbol === true){ wait_next_symbol = false; } //if need to wait next symbol - no need to wait now.
var result_symbol =
(insert_symbol===true) //if need to insert symbol
? XOR_char + alphabet[xorred] //add this to cyphertext
: alphabet[xorred]; //or just add result symbol
if(insert_symbol===true){ insert_symbol=false; } //If need to insert symbol - no need to insert now.
return [ result_symbol, alphabet[k] ]; //Return result symbol (or two, including XOR_char)
}
else if( algo === 'tritemius' ){
//Tritemius cipher
var index = alphabet.indexOf(m); //index
var k = (typeof key === 'undefined') ? range(0, alphabet.length-1) : key; //generate key up to alphabet.length
if(typeof param === 'undefined' || param.indexOf('En')!==-1){ //[En]cipher, [En]crypt
k = k; //add for encipher
}else if(typeof param !== 'undefined' && param.indexOf('De')!==-1){ //[De]cipher, [De]crypt
k = -k; //subtract for decipher
}
var L = ((index + k)+alphabet.length) % alphabet.length; //result code.
return [ alphabet[L], alphabet[k] ]; //return result symbol
}
else if( algo === 'vizhener' || algo === 'beaufort' || algo === 'gronsfeld' || algo === 'shifted_atbash'){
//shifted_atbash, Vizhener, Beaufort, Gronsfeld
var a = alphabet;
var mode = param;
var mi = a.indexOf( m ); //index of symbol
var ki_s = (typeof key === 'undefined') ? bytes(1)[0] : key ;
var ki = (algo === 'gronsfeld') ? (ki_s % 10) : (ki_s % alphabet.length);
if(algo === 'beaufort'){ //for Beaufort cipher
mode = 'Decipher'; //usning Vigener cipher and decrypt only mode - for each encrypt and decrypt operation.
var x = ki; //exchange
ki = mi; //key index
mi = x; //and message index
}