-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar.js
More file actions
45 lines (37 loc) · 1.36 KB
/
caesar.js
File metadata and controls
45 lines (37 loc) · 1.36 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
function encrypt() {
const writtenText = document.getElementById("input-text").value;
const key = parseInt(document.getElementById("key").value);
let encryptedText = "";
for(let i = 0; i < writtenText.length; i++){
const charCode = writtenText.charCodeAt(i)
if (charCode >= 65 && charCode <= 90) {
encryptedText += String.fromCharCode(((charCode - 65 + key) % 26) + 65);
}
else if(charCode >= 97 && charCode <= 122) {
encryptedText += String.fromCharCode(((charCode - 97 + key) % 26) + 97);
}
else {
encryptedText += writtenText[i];
}
document.getElementById("output-text").value = encryptedText;
};
}
// decrypted
function decrypt() {
const writtenText = document.getElementById("input-text").value;
const key = parseInt(document.getElementById("key").value);
let decryptedText = "";
for(let i = 0; i < writtenText.length; i++){
const charCode = writtenText.charCodeAt(i)
if (charCode >= 65 && charCode <= 90) {
decryptedText += String.fromCharCode(((charCode - 65 - key + 26) % 26) + 65);
}
else if(charCode >= 97 && charCode <= 122) {
decryptedText += String.fromCharCode(((charCode - 97 - key + 26) % 26) + 97);
}
else {
decryptedText += writtenText[i];
}
document.getElementById("output-text").value = decryptedText;
}
}