-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
367 lines (311 loc) · 8.84 KB
/
javascript.js
File metadata and controls
367 lines (311 loc) · 8.84 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
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
//Main slideshow controller
var slideIndex = 1;
function plusSlides(n) {
showSlides(slideIndex += n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
if (n > slides.length) {slideIndex = slides.length}
if (n < 1) {slideIndex = 1}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slides[slideIndex-1].style.display = "block";
}
document.addEventListener("keydown", function(event) {
if(event.target.tagName == "INPUT")
return;
if (event.keyCode == 37 || event.keyCode == 65) {
plusSlides(-1);
}
if(event.keyCode == 39 || event.keyCode == 68) {
plusSlides(1);
}
})
//Public key encryption animation
var animIndex = 0;
var paused = 1;
function pause() {
paused = 1;
}
function play() {
if(paused == 1) {
paused = 0;
showAnimSlides();
}
}
function reset() {
animIndex = 0;
if(paused == 1) {
paused = 0;
showAnimSlides();
}
}
function showAnimSlides() {
if(paused == 1) {
return;
}
var slide = document.getElementsByClassName("animSlide");
animIndex++;
if (animIndex > 80) {
animIndex = 1;
}
slide[0].src = "images/animation-"+ animIndex +".png";
var waitarray = [
[77, 10],
[78, 10],
[79, 20],
[80, 40]
];
var waittime = 100;
for(var i = 0; i < waitarray.length; i++) {
if(animIndex == waitarray[i][0]) {
waittime *= waitarray[i][1];
break;
}
}
setTimeout(showAnimSlides, waittime);
}
//time animation
var timeIndex = -1;
var paused2 = 1;
function pause2() {
paused2 = 1;
}
function play2() {
if(paused2 == 1) {
paused2 = 0;
showTimeSlides();
}
}
function reset2() {
timeIndex = -1;
if(paused2 == 1) {
paused2 = 0;
showTimeSlides();
}
}
function showTimeSlides() {
if(paused2 == 1) {
return;
}
var slide = document.getElementsByClassName("timeSlide");
timeIndex++;
if (timeIndex > 16) {
timeIndex = 0;
}
slide[0].src = "timeimages/time"+ timeIndex +".png";
var waittime = 1000;
setTimeout(showTimeSlides, waittime);
}
//full RSA animation
var fullRSAIndex = -1;
var paused3 = 1;
function pause3() {
paused3 = 1;
}
function play3() {
if(paused3 == 1) {
paused3 = 0;
showfullRSASlides();
}
}
function reset3() {
fullRSAIndex = -1;
if(paused3 == 1) {
paused3 = 0;
showfullRSASlides();
}
}
function showfullRSASlides() {
if(paused3 == 1) {
return;
}
var slide = document.getElementsByClassName("fullRSASlide");
fullRSAIndex++;
if (fullRSAIndex > 70) {
fullRSAIndex = 0;
}
slide[0].src = "fullrsa/FullRSA("+ fullRSAIndex +").png";
var waittime = 100;
var waitarray = [
[0, 10],
[1, 5],
[2, 5],
[3, 5],
[4, 20],
[5, 20],
[6, 20],
[7, 5],
[8, 5],
[40, 15],
[41, 15],
[42, 20],
[62, 8],
[63, 8],
[64, 8],
[65, 5],
[66, 5],
[67, 5],
[68, 35],
[69, 35],
[70, 35]
];
for(var i = 0; i < waitarray.length; i++) {
if(fullRSAIndex == waitarray[i][0]) {
waittime *= waitarray[i][1];
break;
}
}
setTimeout(showfullRSASlides, waittime);
}
//everything else
var multiply = function() {
var p = bigInt(document.getElementById("p").value);
var q = bigInt(document.getElementById("q").value);
document.getElementById("bothnotprime").style.display = "none";
document.getElementById("qnotprime").style.display = "none";
document.getElementById("pnotprime").style.display = "none";
if(bigInt(p).isPrime() == false && bigInt(q).isPrime() == false) {
document.getElementById("bothnotprime").style.display = "block";
return;
}
else if (bigInt(p).isPrime() == false) {
document.getElementById("pnotprime").style.display = "block";
return;
}
else if (bigInt(q).isPrime() == false) {
document.getElementById("qnotprime").style.display = "block";
return;
}
document.getElementById("modulusn").innerHTML = p.multiply(q);
document.getElementById("phivalue").innerHTML = bigInt(p.minus(1)).multiply(bigInt(q.minus(1)));
};
var randomize = function() {
var p,q;
document.getElementById("bothnotprime").style.display = "none";
document.getElementById("qnotprime").style.display = "none";
document.getElementById("pnotprime").style.display = "none";
while(true) {
var potentialprime = bigInt.randBetween(bigInt(2).pow(31), bigInt(2).pow(32));
if (potentialprime.isPrime() == true) {
document.getElementById("p").value = potentialprime;
break;
}
}
for (var i = 0; i < 1000; i++) {
var potentialprime = bigInt.randBetween(bigInt(2).pow(31), bigInt(2).pow(32));
if (potentialprime.isPrime() == true) {
document.getElementById("q").value = potentialprime;
break;
}
}
multiply();
};
var checke = function () {
var e = bigInt(document.getElementById("e").value);
var phi = bigInt(document.getElementById("phivalue").innerHTML);
if (bigInt(bigInt.gcd(e,phi)).equals(bigInt(1)) == false) {
document.getElementById("eerror").style.display = "block";
}
else {
document.getElementById("eerror").style.display = "none";
document.getElementById("dvalue").innerHTML = bigInt(e).modInv(phi);
}
};
var encrypt = function () {
var message = document.getElementById("message").value;
var numbermessage = "";
for(var i = 0; i < message.length; i++) {
var num = message.charAt(i).charCodeAt(0);
var stringvalue = "";
if(num < 100) {
stringvalue = '0' + num.toString();
}
else {
stringvalue = num.toString();
}
numbermessage += stringvalue + ' ';
}
document.getElementById("numbermessage").innerHTML = numbermessage;
var encryptmessage = "";
var e = bigInt(document.getElementById("e").value);
var n = bigInt(document.getElementById("modulusn").innerHTML);
var splitmessage = numbermessage.split(" ");
console.log(splitmessage);
for(var i = 0; i < splitmessage.length-1; i++) {
var indiv = splitmessage[i];
console.log(indiv);
var encryptednum = bigInt(indiv).modPow(bigInt(e),bigInt(n)).toString();
encryptmessage += encryptednum + ' ';
}
document.getElementById("encrypt").innerHTML = encryptmessage;
};
var decrypt = function () {
var encryptmessage = document.getElementById("encrypt").innerHTML;
var splitmessage = encryptmessage.split(" ");
var decryptmessage = "";
var d = bigInt(document.getElementById("dvalue").innerHTML);
var n = bigInt(document.getElementById("modulusn").innerHTML);
for(var i = 0; i < splitmessage.length-1; i++) {
var indiv = splitmessage[i];
var decryptednum = bigInt(indiv).modPow(bigInt(d),bigInt(n)).toString();
decryptmessage += decryptednum + ' ';
}
var converted = "";
var splitmessage2 = decryptmessage.split(" ");
for(var i = 0; i < splitmessage2.length-1; i++) {
var indiv2 = splitmessage2[i];
converted += String.fromCharCode(indiv2);
}
document.getElementById("decrypt").innerHTML = decryptmessage;
document.getElementById("decryptfull").innerHTML = converted;
};
var phimodal = function() {
var dialog = document.querySelector('#phimodal');
if (! dialog.showModal) {
dialogPolyfill.registerDialog(dialog);
}
dialog.showModal();
};
var phimodalclose = function() {
var dialog = document.querySelector('#phimodal');
if (! dialog.showModal) {
dialogPolyfill.registerDialog(dialog);
}
dialog.close();
}
var modmodal = function() {
var dialog = document.querySelector('#modarith');
if (! dialog.showModal) {
dialogPolyfill.registerDialog(dialog);
}
dialog.showModal();
};
var modmodalclose = function() {
var dialog = document.querySelector('#modarith');
if (! dialog.showModal) {
dialogPolyfill.registerDialog(dialog);
}
dialog.close();
}
// Preload images
var images = new Array()
var images2 = new Array()
var images3 = new Array()
function preload() {
for (var i = 1; i <= 80; i++) {
images[i] = new Image()
images[i].src = "images/animation-"+ i +".png";
}
for(var j = 0; j <= 16; j++) {
images2[j] = new Image()
images2[j].src = "timeimages/time"+ j +".png";
}
for(var k = 0; k <= 70; k++) {
images3[k] = new Image()
images3[k].src = "fullrsa/FullRSA("+ k +").png";
}
}
preload();