11/*
2- code assumes that plainText contains ONLY LOWER CASE ALPHABETS
3- */
2+ code assumes that plainText contains ONLY LOWER CASE ALPHABETS
3+ */
44
55Number . prototype . mod = function ( n ) {
6- return ( ( this % n ) + n ) % n ;
6+ return ( ( this % n ) + n ) % n ;
77} ;
88
99var keys = { a : 5 , b : 7 } ,
10- N = 26 ;
10+ N = 26 ;
1111
12- function encrypt ( plainText ) {
13- var cypherText = '' ;
14- function cryptAlpha ( alpha ) {
15- var index = alpha . charCodeAt ( 0 ) - 'a' . charCodeAt ( 0 ) ;
16- var result = ( ( keys . a * index ) + keys . b ) . mod ( N ) ;
12+ function encrypt ( plainText ) {
13+ var cypherText = '' ;
1714
18- logger . _print ( 'Index of ' + alpha + ' = ' + index ) ;
15+ function cryptAlpha ( alpha ) {
16+ var index = alpha . charCodeAt ( 0 ) - 'a' . charCodeAt ( 0 ) ;
17+ var result = ( ( keys . a * index ) + keys . b ) . mod ( N ) ;
1918
20- result += 'a' . charCodeAt ( 0 ) ;
21- return String . fromCharCode ( result ) ;
22- }
19+ logger . _print ( 'Index of ' + alpha + ' = ' + index ) ;
2320
24- logger . _print ( 'Beginning Affine Encryption' ) ;
25- logger . _print ( 'Encryption formula: <b>((keys.a * index_of_alphabet) + keys.b) % N</b>' ) ;
26- logger . _print ( 'keys.a=' + keys . a + ', keys.b=' + keys . b + ', N=' + N ) ;
21+ result += 'a' . charCodeAt ( 0 ) ;
22+ return String . fromCharCode ( result ) ;
23+ }
2724
28- for ( var i in plainText ) {
29- ptTracer . _select ( i ) . _wait ( ) ;
30- ptTracer . _deselect ( i ) . _wait ( ) ;
25+ logger . _print ( 'Beginning Affine Encryption' ) ;
26+ logger . _print ( 'Encryption formula: <b>((keys.a * index_of_alphabet) + keys.b) % N</b>' ) ;
27+ logger . _print ( 'keys.a=' + keys . a + ', keys.b=' + keys . b + ', N=' + N ) ;
3128
32- cypherText += cryptAlpha ( plainText [ i ] ) ;
29+ for ( var i in plainText ) {
30+ ptTracer . _select ( i ) . _wait ( ) ;
31+ ptTracer . _deselect ( i ) ;
3332
34- ptTracer . _notify ( i , cypherText . slice ( - 1 ) ) . _wait ( ) ;
35- ptTracer . _denotify ( i ) . _wait ( ) ;
36- }
33+ cypherText += cryptAlpha ( plainText [ i ] ) ;
3734
38- return cypherText ;
35+ ptTracer . _notify ( i , cypherText . slice ( - 1 ) ) . _wait ( ) ;
36+ ptTracer . _denotify ( i ) ;
37+ }
38+
39+ return cypherText ;
3940}
4041
41- function decrypt ( cypherText ) {
42- var plainText = '' ;
43- var aInverse = ( function ( ) {
44- for ( var i = 1 ; i < N ; i ++ ) {
45- if ( ( ( keys . a * i ) . mod ( N ) ) === 1 ) {
46- return i ;
47- }
48- }
49- } ) ( ) ;
42+ function decrypt ( cypherText ) {
43+ var plainText = '' ;
44+ var aInverse = ( function ( ) {
45+ for ( var i = 1 ; i < N ; i ++ ) {
46+ if ( ( ( keys . a * i ) . mod ( N ) ) === 1 ) {
47+ return i ;
48+ }
49+ }
50+ } ) ( ) ;
5051
51- logger . _print ( 'a<sup>-1</sup> = ' + aInverse ) ;
52+ logger . _print ( 'a<sup>-1</sup> = ' + aInverse ) ;
5253
53- function decryptAlpha ( alpha ) {
54- var index = alpha . charCodeAt ( 0 ) - 'a' . charCodeAt ( 0 ) ;
55- var result = ( aInverse * ( index - keys . b ) ) . mod ( N ) ;
54+ function decryptAlpha ( alpha ) {
55+ var index = alpha . charCodeAt ( 0 ) - 'a' . charCodeAt ( 0 ) ;
56+ var result = ( aInverse * ( index - keys . b ) ) . mod ( N ) ;
5657
57- logger . _print ( 'Index of ' + alpha + ' = ' + index ) ;
58+ logger . _print ( 'Index of ' + alpha + ' = ' + index ) ;
5859
59- result += 'a' . charCodeAt ( 0 ) ;
60- return String . fromCharCode ( result ) ;
61- }
60+ result += 'a' . charCodeAt ( 0 ) ;
61+ return String . fromCharCode ( result ) ;
62+ }
6263
63- logger . _print ( 'Beginning Affine Decryption' ) ;
64- logger . _print ( 'Decryption formula: <b>(a<sup>-1</sup> * (index - keys.b)) % N</b>' ) ;
65- logger . _print ( 'keys.b=' + keys . b + ', N=' + N ) ;
64+ logger . _print ( 'Beginning Affine Decryption' ) ;
65+ logger . _print ( 'Decryption formula: <b>(a<sup>-1</sup> * (index - keys.b)) % N</b>' ) ;
66+ logger . _print ( 'keys.b=' + keys . b + ', N=' + N ) ;
6667
67- for ( var i in cypherText ) {
68- ctTracer . _select ( i ) . _wait ( ) ;
69- ctTracer . _deselect ( i ) . _wait ( ) ;
68+ for ( var i in cypherText ) {
69+ ctTracer . _select ( i ) . _wait ( ) ;
70+ ctTracer . _deselect ( i ) . _wait ( ) ;
7071
71- plainText += decryptAlpha ( cypherText [ i ] ) ;
72+ plainText += decryptAlpha ( cypherText [ i ] ) ;
7273
73- ctTracer . _notify ( i , plainText . slice ( - 1 ) ) . _wait ( ) ;
74- ctTracer . _denotify ( i ) . _wait ( ) ;
75- }
74+ ctTracer . _notify ( i , plainText . slice ( - 1 ) ) . _wait ( ) ;
75+ ctTracer . _denotify ( i ) . _wait ( ) ;
76+ }
7677
77- return plainText ;
78+ return plainText ;
7879}
7980
80- var cipherText = encrypt ( plainText ) ;
81- ctTracer . _setData ( cipherText ) ;
82- decrypt ( cipherText ) ;
81+ var cipherText = encrypt ( plainText ) ;
82+ ctTracer . _setData ( cipherText ) ;
83+ decrypt ( cipherText ) ;
0 commit comments