-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
89 lines (72 loc) · 2.58 KB
/
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
console.log('Tejas Codes :)')
let cycle = 9;
function encryptText() {
const text = document.getElementById("inputText").value;
const cycles = parseInt(document.getElementById("cycles").value) || 9; // Use default value of 1 if cycles is not a valid number
if (!text || isNaN(cycles) || cycles <= 0) {
alert("Please enter some text to encrypt.");
return;
}
let encrypted = text;
var key = Math.floor(Math.random() * 9) + 1;
for (let i = 0; i < cycles; i++) {
encrypted = encrypt(encrypted,key);
}
document.getElementById("inputText").value = encrypted;
}
function decryptText() {
const coded = document.getElementById("inputText").value;
const cycles = parseInt(document.getElementById("cycles").value) || 9; // Use default value of 1 if cycles is not a valid number
if (!coded || isNaN(cycles) || cycles <= 0) {
alert("Please enter some text to decrypt.");
return;
}
let decrypted = coded;
for (let i = 0; i < cycles; i++) {
decrypted = decrypt(decrypted);
}
document.getElementById("inputText").value = decrypted;
}
function encrypt(text,key) {
let halfLength = Math.floor(text.length / 2);
let parts = [];
parts.push(text.substring(0, halfLength));
parts.push(text.substring(halfLength).split("").reverse().join(""));
let temp1 = [];
parts.forEach(i => {
let temp2 = "";
for (let j of i) {
j = j.charCodeAt(0) + key;
temp2 += String.fromCharCode(j);
}
temp1.push(temp2);
});
let encrypted = temp1[1] + String(key) + temp1[0];
return encrypted;
}
function decrypt(coded) {
const keyIndex = Math.floor(coded.length / 2);
const key = parseInt(coded.charAt(keyIndex));
let encryptedPart1 = coded.substring(0, keyIndex);
let encryptedPart2 = coded.substring(keyIndex + 1);
let parts = [encryptedPart1, encryptedPart2];
let temp1 = [];
parts.forEach(i => {
let temp2 = "";
for (let j of i) {
let value = j.charCodeAt(0) - key;
temp2 += String.fromCharCode(value);
}
temp1.push(temp2);
});
let decrypted = temp1[1] + temp1[0].split("").reverse().join("");
return decrypted;
}
function copyText() {
const copied = document.getElementById("inputText").value;
navigator.clipboard.writeText(copied);
}
function clearText(){
document.getElementById("inputText").value = "";
document.getElementById("cycles").value = "";
}