This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
153 lines (128 loc) · 3.8 KB
/
app.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
// Elements
const encryptMode = document.querySelector(".encrypt");
const decryptMode = document.querySelector(".decrypt");
const plainText = document.querySelector(".plain_text");
const cipherText = document.querySelector(".cipher_text");
const inputKey = document.querySelector(".key");
const refreshButton = document.querySelector(".refresh-btn");
const decryptButton = document.querySelector(".decrypt-btn");
const copyButton = document.querySelector(".copy-btn");
const copyrightYear = document.querySelector(".year");
const ascii =
"abcdefghijklmnopqrstuvwxyz0123456789.,;:'`\"!@#$%^&*()_--=+{}[]|/~<>|\\\t\n ";
let text, key;
// add year to copyright
const year = new Date().getFullYear();
copyrightYear.innerText = year;
// disable fields
function disablefields() {
inputKey.disabled = true;
plainText.disabled = true;
refreshButton.disabled = true;
copyButton.disabled = true;
}
disablefields();
// generateKey
const genKey = function (string) {
key = string
.split("")
.filter((e) => e !== " ")
.join("")
.toLocaleLowerCase()
.repeat(text.length)
.slice(0, text.length);
return key;
};
// capitalise word
function capitaliseCase(word) {
return word[0].toUpperCase() + word.slice(1, text.length);
}
// transform text to lowerCase
function reduceCase(text) {
return text.toLowerCase();
}
// clear fields
function clearfields() {
plainText.value = cipherText.value = inputKey.value = "";
}
// enablefields
function enablefields() {
inputKey.disabled = false;
plainText.disabled = false;
refreshButton.disabled = false;
copyButton.disabled = false;
inputKey.classList.add("keyEnabled");
}
// validate key
function validateKey() {
if (key === "" && plainText.value.length === 1) {
disablefields();
alert("enter key");
refreshButton.disabled = false;
}
}
// Event listeners
// encrypt text
encryptMode.addEventListener("click", () => {
clearfields();
enablefields();
encryptMode.classList.toggle("encryptMode");
decryptMode.classList.remove("decryptMode");
plainText.addEventListener("input", (e) => {
text = e.target.value;
key = inputKey.value;
validateKey();
const encrypt = function (text, key) {
const letterPosition = [];
text = reduceCase(text);
key = reduceCase(key);
[...text].forEach((_, i) => {
const letterIndex = ascii.indexOf(text[i]) + ascii.indexOf(key[i]);
letterPosition.push(letterIndex % ascii.length);
});
return letterPosition
.map((charPostion) => ascii.charAt(charPostion))
.join("")
.toLocaleUpperCase();
};
cipherText.value = encrypt(text, genKey(key));
});
});
// decrypt key
decryptMode.addEventListener("click", () => {
clearfields();
enablefields();
decryptMode.classList.toggle("decryptMode");
encryptMode.classList.remove("encryptMode");
plainText.addEventListener("input", () => {
if (plainText.value === "") return;
text = plainText.value;
key = inputKey.value;
validateKey();
const decrypt = function (text, key) {
const letterPosition = [];
text = reduceCase(text);
key = reduceCase(key);
// get text letter positions in alphabet
[...text].forEach((_, i) => {
const letterIndex = ascii.indexOf(text[i]) - ascii.indexOf(key[i]);
letterPosition.push((letterIndex + ascii.length) % ascii.length);
});
const ouput = letterPosition
.map((charPostion) => ascii.charAt(charPostion))
.join("");
return capitaliseCase(ouput);
};
cipherText.value = decrypt(text, genKey(key));
});
});
// copy cipherText
copyButton.addEventListener("click", () => {
cipherText.select();
navigator.clipboard.writeText(cipherText.value);
});
// refresh cipher
refreshButton.addEventListener("click", () => {
clearfields();
location.reload();
});