forked from algorithm-visualizer/algorithm-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
83 lines (61 loc) · 1.99 KB
/
Copy pathcode.js
File metadata and controls
83 lines (61 loc) · 1.99 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
/*
code assumes that plainText contains ONLY LOWER CASE ALPHABETS
*/
Number.prototype.mod = function (n) {
return ((this % n) + n) % n;
};
var keys = {a: 5, b: 7},
N = 26;
function encrypt(plainText) {
var cypherText = '';
function cryptAlpha(alpha) {
var index = alpha.charCodeAt(0) - 'a'.charCodeAt(0);
var result = ((keys.a * index) + keys.b).mod(N);
logger._print('Index of ' + alpha + ' = ' + index);
result += 'a'.charCodeAt(0);
return String.fromCharCode(result);
}
logger._print('Beginning Affine Encryption');
logger._print('Encryption formula: <b>((keys.a * index_of_alphabet) + keys.b) % N</b>');
logger._print('keys.a=' + keys.a + ', keys.b=' + keys.b + ', N=' + N);
for (var i in plainText) {
ptTracer._select(i)._wait();
ptTracer._deselect(i);
cypherText += cryptAlpha(plainText [i]);
ptTracer._notify(i, cypherText.slice(-1))._wait();
ptTracer._denotify(i);
}
return cypherText;
}
function decrypt(cypherText) {
var plainText = '';
var aInverse = (function () {
for (var i = 1; i < N; i++) {
if (((keys.a * i).mod(N)) === 1) {
return i;
}
}
})();
logger._print('a<sup>-1</sup> = ' + aInverse);
function decryptAlpha(alpha) {
var index = alpha.charCodeAt(0) - 'a'.charCodeAt(0);
var result = (aInverse * (index - keys.b)).mod(N);
logger._print('Index of ' + alpha + ' = ' + index);
result += 'a'.charCodeAt(0);
return String.fromCharCode(result);
}
logger._print('Beginning Affine Decryption');
logger._print('Decryption formula: <b>(a<sup>-1</sup> * (index - keys.b)) % N</b>');
logger._print('keys.b=' + keys.b + ', N=' + N);
for (var i in cypherText) {
ctTracer._select(i)._wait();
ctTracer._deselect(i)._wait();
plainText += decryptAlpha(cypherText [i]);
ctTracer._notify(i, plainText.slice(-1))._wait();
ctTracer._denotify(i)._wait();
}
return plainText;
}
var cipherText = encrypt(plainText);
ctTracer._setData(cipherText);
decrypt(cipherText);