-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
359 lines (290 loc) · 7.9 KB
/
main.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
function getDeterminent(matrix) {
let x = matrix[0][0] * ((matrix[1][1] * matrix[2][2]) - (matrix[2][1] * matrix[1][2]));
let y = matrix[0][1] * ((matrix[1][0] * matrix[2][2]) - (matrix[2][0] * matrix[1][2]));
let z = matrix[0][2] * ((matrix[1][0] * matrix[2][1]) - (matrix[2][0] * matrix[1][1]));
return (x - y + z);
}
function modularInverse(m, n) {
let x = m;
let y = n;
let divs = [];
let adds = [];
let result;
if (y > x) {
let i = 1;
while (x != 0) {
divs[i] = Math.floor(y / x);
let temp = x;
x = y % x;
y = temp;
i++;
}
let len = divs.length;
adds[len - 1] = 0;
adds[len - 2] = 1;
for (let index = len - 2; index > 0; index--) {
adds[index - 1] = (divs[index] * adds[index]) + adds[index + 1];
}
if ((adds[0] * m) > (adds[1] * n)) {
result = adds[0];
} else {
result = n - adds[0];
}
}
return result;
}
function inverseMatrix(matrix) {
let minorMatrix = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
];
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[i].length; j++) {
minorMatrix[i][j] = (matrix[(i + 1) % 3][(j + 1) % 3] * matrix[(i + 2) % 3][(j + 2) % 3]) - (matrix[(i + 1) % 3][(j + 2) % 3] * matrix[(i + 2) % 3][(j + 1) % 3]);
}
}
let adjointMatrix = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
];
for (let i = 0; i < minorMatrix.length; i++) {
for (let j = 0; j < minorMatrix[i].length; j++) {
adjointMatrix[j][i] = minorMatrix[i][j];
}
}
return adjointMatrix;
}
function multiplyMatrix(a, b) {
let result = [];
for (let i = 0; i < a.length; i++) {
result[i] = 0;
for (let j = 0; j < a[i].length; j++) {
result[i] += b[j] * a[i][j];
}
}
return result;
}
function gcd(x, y) {
x = Math.abs(x);
y = Math.abs(y);
while (y) {
var t = y;
y = x % y;
x = t;
}
return x;
}
function getRidOfNeg(x, n) {
while (x < 0) {
x += n;
}
return x;
}
function removeRepetition(n) {
let temp = "";
for (let i = 0; i < n.length; i++) {
if (temp.indexOf(n[i]) === -1) {
temp += n[i];
}
}
return temp;
}
function checkPlain(n, plain) {
for (let i = 0; i < plain.length; i++) {
if (n.indexOf(plain[i]) === -1) {
return false;
}
}
return true;
}
let alphaTxt = document.getElementById("alphaTxt");
let charBtns = document.getElementsByClassName("btnsDiv")[0].childNodes;
let numCharTxt = document.getElementById("numCharTxt");
let okayBtn = document.getElementById("okayBtn");
let containers = document.getElementsByClassName("container");
let matrixDiv = document.getElementsByClassName("matrixDiv")[0].childNodes;
let goBtn = document.getElementById("goBtn");
let tabsDiv = document.getElementsByClassName("tabsDiv")[0].childNodes;
let title1 = document.getElementById("title1");
let txt1 = document.getElementById("txt1");
let title2 = document.getElementById("title2");
let txt2 = document.getElementById("txt2");
let actionBtn = document.getElementById("actionBtn");
let n;
let alphbetics;
let key = [
[],
[],
[]
];
let det;
charBtns[1].addEventListener("click", () => {
alphaTxt.value += "abcdefghijklmnopqrstuvwxyz";
numCharTxt.innerText = alphaTxt.value.length;
});
charBtns[3].addEventListener("click", () => {
alphaTxt.value += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
numCharTxt.innerText = alphaTxt.value.length;
});
charBtns[5].addEventListener("click", () => {
alphaTxt.value += "0123456789";
numCharTxt.innerText = alphaTxt.value.length;
});
alphaTxt.addEventListener("keyup", () => {
numCharTxt.innerText = alphaTxt.value.length;
});
okayBtn.addEventListener("click", (e) => {
e.preventDefault();
if (alphaTxt.value != "") {
containers[0].classList.add("hide");
containers[1].classList.remove("hide");
n = parseInt(numCharTxt.innerText);
alphbetics = alphaTxt.value;
}
});
goBtn.addEventListener("click", () => {
let index = 1;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
key[i][j] = parseInt(matrixDiv[index].value);
index += 2;
}
}
console.log(key);
if (checkRelativelyPrime()) {
alertFunc(1, "relatively prime");
console.log("relatively prime");
containers[1].classList.add("hide");
containers[2].classList.remove("hide");
} else {
alertFunc(0, "not relatively prime");
console.log("not relatively prime");
}
});
tabsDiv[1].addEventListener("click", () => {
title1.innerText = "Enter Plain Text";
title2.innerText = "Cipher Text";
actionBtn.innerHTML = "encrypt";
txt1.value="";
txt2.value="";
});
tabsDiv[3].addEventListener("click", () => {
title1.innerText = "Enter Cipher Text";
title2.innerText = "Plain Text";
actionBtn.innerHTML = "decrypt";
txt1.value="";
txt2.value="";
});
actionBtn.addEventListener("click", () => {
if (actionBtn.innerHTML === "encrypt") {
txt2.value = encrypt(txt1.value);
} else {
console.log(txt1.value);
txt2.value = decrypt(txt1.value);
}
});
function checkRelativelyPrime() {
det = parseInt(getDeterminent(key));
console.log("det = " + det);
let g = gcd(det, n);
console.log("gcd = " + g);
if (g == 1) {
return true;
} else {
return false;
}
}
function encrypt(plain) {
let cipher = "";
if (alphbetics.indexOf(" ") == -1) {
plain = plain.split(" ").join("");
}
for (let index = 0; index < plain.length; index += 3) {
let x = alphbetics.indexOf(plain[index]);
let y, z;
if (index + 1 == plain.length) {
y = 0;
z = 1;
} else {
y = alphbetics.indexOf(plain[index + 1]);
if (index + 2 == plain.length) {
z = 0;
} else {
z = alphbetics.indexOf(plain[index + 2]);
}
}
let res = multiplyMatrix(key, [x, y, z]);
for (let i = 0; i < res.length; i++) {
if (res[i] < 0) {
res[i] = getRidOfNeg(res[i], n);
}
let j = res[i] % n;
cipher += alphbetics[j];
}
}
return cipher;
}
function decrypt(cipher) {
let plain = "";
let m = det;
if (det < 0) {
m = getRidOfNeg(det, n);
}
m = m % n;
console.log("n: " + n);
console.log("m: " + m);
let modularInv = modularInverse(m, n);
let matrixInv = inverseMatrix(key);
console.log(modularInv);
for (let index = 0; index < cipher.length; index += 3) {
let x = alphbetics.indexOf(cipher[index]);
let y = alphbetics.indexOf(cipher[index + 1]);
let z = alphbetics.indexOf(cipher[index + 2]);
console.log("dec: " + [x, y, z]);
let res = multiplyMatrix(matrixInv, [x, y, z]);
console.log("matinv: " + res);
for (let i = 0; i < res.length; i++) {
res[i] *= modularInv;
console.log("res[" + i + "]: " + res[i]);
if (res[i] < 0) {
res[i] = getRidOfNeg(res[i], n);
}
let j = res[i] % n;
plain += alphbetics[j];
}
}
return plain;
}
// for alert
let alertContainer = document.getElementById("alertContainer");
let btn = document.getElementById("btn");
let alertIcon = document.getElementById("alertIcon");
let alertMessage = document.getElementById("alertMessage");
let alert = document.getElementById('alert');
function alertFunc(type, message) {
if (type === 0) {
alertIcon.classList.add('fa-times');
alertIcon.classList.add('text-danger');
alert.style.borderTop = "50px solid #0b9253";
} else if (type === 1) {
alertIcon.classList.add('fa-check');
alertIcon.classList.add('text-success');
alert.style.borderTop = "50px solid #262626";
}
alertMessage.innerHTML = message;
alertContainer.classList.remove('hide');
alert.classList.add("show-animation");
setTimeout(() => {
alertContainer.classList.add('hide-animation');
}, 2000);
setTimeout(() => {
alertContainer.classList.add('hide');
alert.classList.remove("show-animation");
alertContainer.classList.remove('hide-animation');
alertContainer.style.opacity = '1';
alertIcon.className = '';
alertIcon.classList.add('fas');
}, 3000);
}