1
1
/*
2
- code assumes that plainText contains ONLY LOWER CASE ALPHABETS
3
- */
2
+ code assumes that plainText contains ONLY LOWER CASE ALPHABETS
3
+ */
4
4
5
5
Number . prototype . mod = function ( n ) {
6
- return ( ( this % n ) + n ) % n ;
6
+ return ( ( this % n ) + n ) % n ;
7
7
} ;
8
8
9
9
var keys = { a : 5 , b : 7 } ,
10
- N = 26 ;
10
+ N = 26 ;
11
11
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 = '' ;
17
14
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 ) ;
19
18
20
- result += 'a' . charCodeAt ( 0 ) ;
21
- return String . fromCharCode ( result ) ;
22
- }
19
+ logger . _print ( 'Index of ' + alpha + ' = ' + index ) ;
23
20
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
+ }
27
24
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 ) ;
31
28
32
- cypherText += cryptAlpha ( plainText [ i ] ) ;
29
+ for ( var i in plainText ) {
30
+ ptTracer . _select ( i ) . _wait ( ) ;
31
+ ptTracer . _deselect ( i ) ;
33
32
34
- ptTracer . _notify ( i , cypherText . slice ( - 1 ) ) . _wait ( ) ;
35
- ptTracer . _denotify ( i ) . _wait ( ) ;
36
- }
33
+ cypherText += cryptAlpha ( plainText [ i ] ) ;
37
34
38
- return cypherText ;
35
+ ptTracer . _notify ( i , cypherText . slice ( - 1 ) ) . _wait ( ) ;
36
+ ptTracer . _denotify ( i ) ;
37
+ }
38
+
39
+ return cypherText ;
39
40
}
40
41
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
+ } ) ( ) ;
50
51
51
- logger . _print ( 'a<sup>-1</sup> = ' + aInverse ) ;
52
+ logger . _print ( 'a<sup>-1</sup> = ' + aInverse ) ;
52
53
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 ) ;
56
57
57
- logger . _print ( 'Index of ' + alpha + ' = ' + index ) ;
58
+ logger . _print ( 'Index of ' + alpha + ' = ' + index ) ;
58
59
59
- result += 'a' . charCodeAt ( 0 ) ;
60
- return String . fromCharCode ( result ) ;
61
- }
60
+ result += 'a' . charCodeAt ( 0 ) ;
61
+ return String . fromCharCode ( result ) ;
62
+ }
62
63
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 ) ;
66
67
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 ( ) ;
70
71
71
- plainText += decryptAlpha ( cypherText [ i ] ) ;
72
+ plainText += decryptAlpha ( cypherText [ i ] ) ;
72
73
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
+ }
76
77
77
- return plainText ;
78
+ return plainText ;
78
79
}
79
80
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