-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
509 lines (434 loc) · 16.4 KB
/
Copy pathscript.js
File metadata and controls
509 lines (434 loc) · 16.4 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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
console.log('Tejas Codes :)')
let lastAction = null;
document.addEventListener('DOMContentLoaded', () => {
const symRadio = document.getElementById('symRadio');
const asymRadio = document.getElementById('asymRadio');
const keyInput = document.getElementById('key');
const infoText = document.querySelector('.info');
function updateUI() {
if (asymRadio.checked) {
keyInput.placeholder = "Key (3+ digits to encrypt, roots to decrypt)";
infoText.innerText = "* Asymmetric mode requires a key (3+ digits to encrypt, or comma-separated roots to decrypt).";
} else {
keyInput.placeholder = "Key (optional)";
infoText.innerText = "* Remember to use the same key for decryption if provided.";
}
}
symRadio?.addEventListener('change', updateUI);
asymRadio?.addEventListener('change', updateUI);
const params = new URLSearchParams(window.location.search);
const payload = params.get('pl') || params.get('payload');
const type = params.get('t') || params.get('type');
const key = params.get('k') || params.get('key');
const action = params.get('a') || params.get('action');
const version = params.get('v');
if (payload) {
document.getElementById('inputText').value = payload;
}
if (key) {
keyInput.value = key;
}
if (type === 'a' || type === 'asymmetric') {
if (asymRadio) asymRadio.checked = true;
} else if (type === 's' || type === 'symmetric') {
if (symRadio) symRadio.checked = true;
}
if (version === '1') {
const v1Mode = document.getElementById('v1Mode');
if (v1Mode) v1Mode.checked = true;
}
updateUI();
if (action === 'e' || action === 'encrypt') {
setTimeout(encryptText, 50);
} else if (action === 'd' || action === 'decrypt') {
setTimeout(decryptText, 50);
}
});
function encryptText() {
const isAsymmetric = document.getElementById("asymRadio")?.checked;
if (isAsymmetric) {
return encryptAsymmetric();
}
lastAction = 'encrypt';
const isLegacy = document.getElementById("v1Mode")?.checked;
const text = document.getElementById("inputText").value;
if (!text) {
alert("Please enter some text to encrypt.");
return;
}
const keyInput = document.getElementById("key").value.trim();
let cycles = 2;
if (keyInput) {
if (!isNaN(keyInput) && Number.isInteger(Number(keyInput))) {
cycles = parseInt(keyInput);
} else {
const asciiSum = [...keyInput].reduce((sum, char) => sum + char.charCodeAt(0), 0);
cycles = Math.floor(asciiSum / keyInput.length);
}
}
// Cap cycles between 1 and 3 for fast output
cycles = Math.max(1, Math.min(cycles, 3));
let keyShift = 0;
if (keyInput) {
let hash = 0;
for (let i = 0; i < keyInput.length; i++) {
hash = (hash << 5) - hash + keyInput.charCodeAt(i);
hash |= 0;
}
keyShift = Math.abs(hash) % 256;
}
showLoader('encryptBtn', 'Encrypting...', () => {
let encrypted = text;
if (isLegacy) {
const timeKey = Date.now();
const shift = timeKey % 256;
for (let i = 0; i < cycles; i++) {
encrypted = encryptSym(encrypted, shift);
}
encrypted = encrypted + "|" + timeKey.toString(36);
document.getElementById("inputText").value = encrypted;
} else {
// Use current date and time instead of random number
const timeShift = Date.now() % 256;
const totalShift = (timeShift + keyShift) % 256;
for (let i = 0; i < cycles; i++) {
encrypted = encryptSym(encrypted, totalShift);
}
// Append ONLY the timeShift as a single byte to retrieve it during decryption
encrypted = encrypted + String.fromCharCode(timeShift);
// Convert to Base62 to ensure only alphanumeric characters while minimizing length
document.getElementById("inputText").value = toBase62(encrypted);
}
});
}
function decryptText() {
const isAsymmetric = document.getElementById("asymRadio")?.checked;
if (isAsymmetric) {
return decryptAsymmetric();
}
lastAction = 'decrypt';
const isLegacy = document.getElementById("v1Mode")?.checked;
let codedB62 = document.getElementById("inputText").value;
if (!codedB62) {
alert("Please enter some text to decrypt.");
return;
}
let coded;
if (isLegacy) {
coded = codedB62;
} else {
if (!/^[0-9A-Za-z]+$/.test(codedB62)) {
alert("Invalid encrypted text format. It should only contain english letters and numbers (Base62).");
return;
}
coded = fromBase62(codedB62);
}
const keyInput = document.getElementById("key").value.trim();
let cycles = 2;
if (keyInput) {
if (!isNaN(keyInput) && Number.isInteger(Number(keyInput))) {
cycles = parseInt(keyInput);
} else {
const asciiSum = [...keyInput].reduce((sum, char) => sum + char.charCodeAt(0), 0);
cycles = Math.floor(asciiSum / keyInput.length);
}
}
// Cap cycles between 1 and 3 for fast output
cycles = Math.max(1, Math.min(cycles, 3));
let keyShift = 0;
if (keyInput) {
let hash = 0;
for (let i = 0; i < keyInput.length; i++) {
hash = (hash << 5) - hash + keyInput.charCodeAt(i);
hash |= 0;
}
keyShift = Math.abs(hash) % 256;
}
showLoader('decryptBtn', 'Decrypting...', () => {
if (isLegacy) {
const lastPipe = coded.lastIndexOf("|");
if (lastPipe !== -1) {
const timeKeyStr = coded.substring(lastPipe + 1);
const timeKey = parseInt(timeKeyStr, 36);
if (!isNaN(timeKey)) {
const shift = timeKey % 256;
coded = coded.substring(0, lastPipe);
for (let i = 0; i < cycles; i++) {
coded = decryptSym(coded, shift);
}
document.getElementById("inputText").value = coded;
return;
}
}
// Fallback for even older format without |
for (let i = 0; i < cycles; i++) {
coded = decryptSym(coded, keyShift);
}
document.getElementById("inputText").value = coded;
return;
}
if (coded.length > 0) {
// The last byte is the timeShift
const timeShift = coded.charCodeAt(coded.length - 1);
coded = coded.substring(0, coded.length - 1);
const totalShift = (timeShift + keyShift) % 256;
for (let i = 0; i < cycles; i++) {
coded = decryptSym(coded, totalShift);
}
document.getElementById("inputText").value = coded;
return;
}
alert("Invalid symmetric encrypted text format.");
});
}
function encryptSym(text, shift) {
const halfLength = Math.floor(text.length / 2);
const part1 = text.substring(0, halfLength).split("").reverse().join("");
const part2 = text.substring(halfLength).split("").reverse().join("");
const encode = str => [...str].map(ch => String.fromCharCode((ch.charCodeAt(0) + shift) % 256)).join("");
return encode(part2) + encode(part1);
}
function decryptSym(coded, shift) {
const part2Len = Math.ceil(coded.length / 2);
const part2Enc = coded.substring(0, part2Len);
const part1Enc = coded.substring(part2Len);
const decode = str => [...str].map(ch => String.fromCharCode((ch.charCodeAt(0) - shift + 256) % 256)).join("");
const part2 = decode(part2Enc).split("").reverse().join("");
const part1 = decode(part1Enc).split("").reverse().join("");
return part1 + part2;
}
function encryptAsymmetric() {
lastAction = 'encrypt';
const isLegacy = document.getElementById("v1Mode")?.checked;
const text = document.getElementById("inputText").value;
if (!text) {
alert("Please enter some text to encrypt.");
return;
}
const keyInput = document.getElementById("key").value.trim();
if (keyInput.length < 3 || isNaN(keyInput)) {
alert("Please enter a 3 or more digit number as the key for asymmetric encryption.");
return;
}
const len = keyInput.length;
const partLen = Math.floor(len / 3);
const a = parseInt(keyInput.substring(0, partLen)) || 1;
const bStr = keyInput.substring(partLen, partLen * 2);
const c = parseInt(keyInput.substring(partLen * 2)) || 1;
const b_fraction = parseFloat("0." + (bStr || "0"));
// Construct positive real roots directly from the key parts
const r1 = parseFloat((a + b_fraction + 1).toFixed(4)).toString();
const r2 = parseFloat((c + b_fraction + 2).toFixed(4)).toString();
const r1Float = parseFloat(r1);
const r2Float = parseFloat(r2);
const b_over_a = -(r1Float + r2Float);
const c_over_a = r1Float * r2Float;
showLoader('encryptBtn', 'Encrypting...', () => {
let encrypted = "";
for (let i = 0; i < text.length; i++) {
let shift;
if (isLegacy) {
shift = Math.round(i * i + b_over_a * i + c_over_a) % 256;
} else {
const rawShift = Math.round((i * i + b_over_a * i + c_over_a) * 1000000);
shift = ((rawShift % 256) + 256) % 256;
}
encrypted += String.fromCharCode((text.charCodeAt(i) + shift) % 256);
}
if (isLegacy) {
document.getElementById("inputText").value = encrypted;
} else {
// Convert to Base62 to ensure only alphanumeric characters while minimizing length
document.getElementById("inputText").value = toBase62(encrypted);
}
// Output decryption keys to the key textarea so the user can easily copy them
document.getElementById("key").value = `${r1}, ${r2}`;
document.querySelector('.info').innerText = "Keys generated! Please copy and save them to decrypt this text later.";
});
}
function decryptAsymmetric() {
lastAction = 'decrypt';
const isLegacy = document.getElementById("v1Mode")?.checked;
const codedB62 = document.getElementById("inputText").value;
if (!codedB62) {
alert("Please enter some text to decrypt.");
return;
}
let text;
if (isLegacy) {
text = codedB62;
} else {
if (!/^[0-9A-Za-z]+$/.test(codedB62)) {
alert("Invalid encrypted text format. It should only contain english letters and numbers (Base62).");
return;
}
text = fromBase62(codedB62);
}
const keyInput = document.getElementById("key").value.trim();
if (!keyInput.includes(",")) {
alert("Please enter the two roots separated by a comma. e.g. '-1.2345, 0.6789'");
return;
}
const parts = keyInput.split(",");
const r1Str = parts[0].trim();
const r2Str = parts[1].trim();
const r1 = parseFloat(r1Str);
const r2 = parseFloat(r2Str);
if (isNaN(r1) || isNaN(r2)) {
alert("Invalid root format. Please enter two valid numbers separated by a comma.");
return;
}
const b_over_a = -(r1 + r2);
const c_over_a = r1 * r2;
showLoader('decryptBtn', 'Decrypting...', () => {
let decrypted = "";
for (let i = 0; i < text.length; i++) {
let shift;
if (isLegacy) {
shift = Math.round(i * i + b_over_a * i + c_over_a) % 256;
} else {
const rawShift = Math.round((i * i + b_over_a * i + c_over_a) * 1000000);
shift = ((rawShift % 256) + 256) % 256;
}
decrypted += String.fromCharCode((text.charCodeAt(i) - shift + 256) % 256);
}
document.getElementById("inputText").value = decrypted;
});
}
function copyText() {
const text = document.getElementById("inputText");
text.select();
text.setSelectionRange(0, 99999);
navigator.clipboard.writeText(text.value);
const btn = document.querySelector('button[onclick="copyText()"]');
if (btn) {
btn.innerText = "Copied!";
setTimeout(() => {
btn.innerText = "Copy";
}, 2000);
}
}
function clearText() {
document.getElementById("inputText").value = "";
document.getElementById("key").value = "";
const isAsymmetric = document.getElementById("asymRadio")?.checked;
const infoText = document.querySelector('.info');
if (infoText) {
if (isAsymmetric) {
infoText.innerText = "* Asymmetric mode requires a key (3+ digits to encrypt, or comma-separated roots to decrypt).";
} else {
infoText.innerText = "* Remember to use the same key for decryption if provided.";
}
}
}
const BASE62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
// Helpers for ensuring output uses only alphanumeric characters while remaining compact
function toBase62(str) {
if (!str) return "";
let hex = "";
let leadingZeroes = 0;
let countingZeroes = true;
for (let i = 0; i < str.length; i++) {
let code = str.charCodeAt(i);
if (code === 0 && countingZeroes) {
leadingZeroes++;
} else {
countingZeroes = false;
}
hex += code.toString(16).padStart(2, '0');
}
let res = "";
if (hex.length > 0 && hex !== "00".repeat(str.length)) {
let dec = BigInt("0x" + hex);
while (dec > 0n) {
res = BASE62[Number(dec % 62n)] + res;
dec = dec / 62n;
}
}
return BASE62[0].repeat(leadingZeroes) + res;
}
function fromBase62(b62) {
if (!b62) return "";
let leadingZeroes = 0;
for (let i = 0; i < b62.length; i++) {
if (b62[i] === BASE62[0]) leadingZeroes++;
else break;
}
let dec = 0n;
for (let i = leadingZeroes; i < b62.length; i++) {
let val = BigInt(BASE62.indexOf(b62[i]));
if (val === -1n) return null;
dec = dec * 62n + val;
}
let hex = "";
if (dec > 0n) {
hex = dec.toString(16);
if (hex.length % 2 !== 0) {
hex = '0' + hex;
}
}
hex = "00".repeat(leadingZeroes) + hex;
let str = "";
for (let i = 0; i < hex.length; i += 2) {
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
}
return str;
}
// UI Helpers
function openManual() {
document.getElementById('manualOverlay').classList.add('open');
}
function closeManual() {
document.getElementById('manualOverlay').classList.remove('open');
}
function showLoader(btnId, loadingText, callback) {
const btn = document.getElementById(btnId);
let originalText = "";
if (btn) {
originalText = btn.innerText;
btn.innerText = loadingText;
}
// Yield thread to allow browser to render the text change
setTimeout(() => {
try {
callback();
} finally {
if (btn) {
btn.innerText = originalText;
}
}
}, 20);
}
function copyShareLink() {
const text = document.getElementById("inputText").value;
const isAsymmetric = document.getElementById("asymRadio")?.checked;
const key = document.getElementById("key").value.trim();
const isV1 = document.getElementById("v1Mode")?.checked;
const url = new URL(window.location.origin + window.location.pathname);
if (text) url.searchParams.set('pl', text);
if (isAsymmetric) {
url.searchParams.set('t', 'a');
} else {
url.searchParams.set('t', 's');
}
if (key) url.searchParams.set('k', key);
if (isV1) {
url.searchParams.set('v', '1');
} else {
url.searchParams.set('v', '2');
}
if (lastAction === 'encrypt') {
url.searchParams.set('a', 'd');
} else if (lastAction === 'decrypt') {
url.searchParams.set('a', 'e');
}
navigator.clipboard.writeText(url.toString());
const btn = document.getElementById('copyLinkBtn');
if (btn) {
const originalText = btn.innerText;
btn.innerText = "Copied!";
setTimeout(() => {
btn.innerText = originalText;
}, 2000);
}
}