forked from nikhil-RGB/Shuffle-Cipher-Web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encryption_script.js
331 lines (293 loc) · 9.86 KB
/
encryption_script.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
// JavaScript source code
var SUDO = false;
const data_c = "ṽẄẁṿṿṷṾṵẄḗḕ";
const isMobile = (/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ||
(/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.platform)));
if (isMobile) {
/*
const txtB1 = document.getElementById("23");
txtB1.style.height = "40%";
const txtB2 = document.getElementById("24");
txtB2.style.height = "40%";
const encb = document.getElementById("CIPHER");
encb.style.height = "4%";
encb.style.width = "13%";
encb.style.fontSize = "28px";
const swi = document.getElementById("enc");
swi.style.minHeight = "10%";
swi.style.fontSize = "28px";
const lab1 = document.getElementsByClassName("labs");
for (const labe of lab1) {
labe.style.fontSize = "28px";
}
*/
//code to detect swipes
document.addEventListener('touchstart', handleTouchStart, false);
document.addEventListener('touchmove', handleTouchMove, false);
var xDown = null;
var yDown = null;
function getTouches(evt) {
return evt.touches ||
evt.originalEvent.touches;
}
function handleTouchStart(evt) {
const firstTouch = getTouches(evt)[0];
xDown = firstTouch.clientX;
yDown = firstTouch.clientY;
};
function handleTouchMove(evt) {
if (!xDown || !yDown) {
return;
}
var xUp = evt.touches[0].clientX;
var yUp = evt.touches[0].clientY;
var xDiff = xDown - xUp;
var yDiff = yDown - yUp;
if (Math.abs(xDiff) > Math.abs(yDiff)) {
if (xDiff > 0) {
console.log("Left side");
/* left swipe */
if (SUDO) { return; }
let pwd = prompt("Enter password to escalate to SUDO mode:");
if (!(pwd == Encrypter.doDecryption(data_c))) {
alert("Invalid Entry, permission to escalate denied.");
return;
}
SUDO = true;
sudoModeColorChange(true);
clearOut();
const element = document.getElementById("main-header");
element.innerText = "SUDO MODE ACTIVE";
element.style.color = "cyan";
alert("Escalation to SUDO mode approved!");
} else {
/* right swipe */
console.log("Right side");
if (!SUDO) { return; }
SUDO = false;
sudoModeColorChange(false);
clearOut();
const element = document.getElementById("main-header");
element.innerText = "SHUFFLE CIPHER";
element.style.color = "white";
alert("SUDO mode will now deactivate");
}
}
/* reset values */
xDown = null;
yDown = null;
};
//end of swipe gesture code
} else {
document.addEventListener("keydown", (event) => {
if (SUDO) {
let cond = event.ctrlKey && event.key == 'b';
if (cond) {
const element = document.getElementById("main-header");
element.innerText = "SHUFFLE CIPHER";
element.style.color = "white";
SUDO = false;
sudoModeColorChange(false);
clearOut();
alert("SUDO mode exiting!");
}
return;
}
let cond1 = event.key == 'k';
let cond2 = event.key == 'c'
let cond = event.ctrlKey && event.altKey && (cond1 || cond2);
if (cond) {
let name = (cond1) ? "Nikhil" : "Chandu<3";
let psswd = prompt("Escalate to SUDO mode for super-user " + name + "\nEnter password");
if (psswd == Encrypter.doDecryption(data_c)) {
const element = document.getElementById("main-header");
element.innerText = "SUDO MODE ACTIVE";
element.style.color = "cyan";
SUDO = true;
sudoModeColorChange(true);
clearOut();
alert("Escalating to SuperUser-Mode!");
} else {
alert("Incorrect password");
}
//code to skip password checks
}
});
}
class Encrypter {
static performPadding(s) {
while (s.length < 4) {
s += " ";
}
return s;
}
static doEncryption(s) {
s = Encrypter.performPadding(s);
let re = Encrypter.shuffle(s);
let ret_val = Encrypter.encrypt(re);
return ret_val;
}
static doDecryption(s) {
let re = Encrypter.decrypt(s);
re = Encrypter.unshuffle(re);
return re;
}
static unshuffle(s) {
let kindex = s.charCodeAt(s.length - 1);
s = s.substring(0, s.length - 1);
let sp = s.substring(0, kindex);
let pp = s.substring(kindex);
sp = sp.split('').reverse().join('');
s = sp + pp;
let re = "";
for (let k = 0; k < s.length; ++k) {
let ch = s.charAt(k) + "";
if (k % 2 == 0) {
ch = String.fromCharCode(ch.charCodeAt(0) - 1);
} else {
ch = String.fromCharCode(ch.charCodeAt(0) + 1);
}
re += ch;
}
return re;
}
static shuffle(s) {
let re = "";
for (let k = 0; k < s.length; k++) {
let ch = s.charAt(k) + "";
if (k % 2 === 0) {
ch = String.fromCharCode(ch.charCodeAt(0) + 1);
} else {
ch = String.fromCharCode(ch.charCodeAt(0) - 1);
}
re += ch;
}
let pos = 0;
while (pos === 0) {
pos = Math.floor(Math.random() * (re.length - 1));
}
let sp = re.substring(0, pos);
let pp = re.substring(pos);
let arr = sp.split('');
arr.reverse();
sp = arr.join('');
re = sp + pp;
return re + String.fromCharCode(pos);
}
static encrypt(req) {
let key = Math.floor((Math.random() * 10000) + 200);
// int key=r.nextInt(10000)+200;
let bb = "";
for (let a = 0; a < req.length; ++a) {
bb += String.fromCharCode(req.charCodeAt(a) + key);
}
bb += String.fromCharCode(key);
return bb;
}
static decrypt(s) {
let key = s.charCodeAt(s.length - 1);
let bb = "";
for (let a = 0; a < s.length - 1; ++a) {
bb += String.fromCharCode(s.charCodeAt(a) - key);
}
return bb;
}
}
function boom() {
let inp = window.document.getElementById("23");
let bEnc = inp.value.split("\n");
let psswd = "";
let string = "";
if (!SUDO) {
let flag;
do {
psswd = prompt("Would you like to password-lock this encryption?\n Click cancel if you'd like to encrypt without a password.");
if (psswd === null) {
break;
}
flag = (psswd.length == 0) || (psswd.indexOf(" ") != -1);
if (flag) {
alert("Invalid Password, password cannot be empty or have spaces.");
}
}
while (flag);
if (psswd === null) {
string = "false";
} else {
string = "true" + " " + psswd;
}
string = Encrypter.encrypt(string) + "\n";
}
bEnc = bEnc.map((elem) => {
return Encrypter.doEncryption(elem);
});
let tex = string + bEnc.join("\n");
if (SUDO) {
tex = Encrypter.encrypt("SUDO") + "\n" + tex;
}
let out = window.document.getElementById("24");
out.value = tex;
}
//Decryption routine final
function antiBoom() {
let inp = window.document.getElementById("23");
let bdec = inp.value;
let sens = bdec.split("\n");
let pass = Encrypter.decrypt(sens.shift()).split(" ");
if (pass[0] === "true") {
if (SUDO) { alert('SuperUser Password Protection Bypass Applied!\nMessage Password: ' + pass[1]); } else {
userp = prompt("This encryption is password protected, please enter the password this message was locked");
if (userp === null) {
return;
} else if (!(userp === pass[1])) {
alert("Incorrect Password entered, try again.");
return;
}
}
} else if (pass[0] !== "false" && (!SUDO)) {
alert("This message was either tampered with or encrypted with an older version of the application and can no longer be decrypted.");
return;
}
sens = sens.map((elem) => {
return Encrypter.doDecryption(elem);
});
let tex = sens.join("\n");
let nline = tex.indexOf("\n")
if (SUDO && (tex.substring(0, nline) == "SUDO")) {
tex = tex.substring(nline + 1);
}
let out = window.document.getElementById("24");
out.value = tex;
}
let tf = window.document.getElementById("23");
let ta = window.document.getElementById("24");
let button = document.getElementById("CIPHER");
function logger(e) {
//let text = tf.value;
let op = window.document.getElementById("enc");
if (op.value === "Encrypt") {
boom();
} else {
antiBoom();
}
}
button.addEventListener("click", logger);
let op1 = window.document.getElementById("enc");
op1.addEventListener("change", clearOut);
function clearOut() {
button.textContent = op1.value;
tf.value = "";
ta.value = "";
}
function sudoModeColorChange(flag) {
//if true, switch to SUDO color scheme(cyan), else default white
const elem1 = document.getElementById("23");
const elem2 = document.getElementById("24");
if (flag) {
elem1.style.color = "cyan";
elem2.style.color = "cyan";
} else {
elem1.style.color = "white";
elem2.style.color = "white";
}
}