File size: 1,384 Bytes
760f698
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
function caesarCipher(text, shift) {

let result = "";

for(let i = 0; i < text.length; i++){

let char = text[i];

if(char >= 'A' && char <= 'Z'){

result += String.fromCharCode(
((char.charCodeAt(0) - 65 + shift + 26) % 26) + 65
);

}
else if(char >= 'a' && char <= 'z'){

result += String.fromCharCode(
((char.charCodeAt(0) - 97 + shift + 26) % 26) + 97
);

}
else{

result += char;

}

}

return result;
}

function encryptText(){

let text =
document.getElementById("inputText").value;

let shift =
parseInt(document.getElementById("shift").value);

if(text.trim() === ""){

alert("Please enter text!");
return;

}

if(isNaN(shift)){

alert("Please enter shift key!");
return;

}

let encrypted =
caesarCipher(text, shift);

document.getElementById(
"encryptedOutput"
).innerText = encrypted;

}

function decryptText(){

let text =
document.getElementById("encryptedOutput").innerText;

let shift =
parseInt(document.getElementById("shift").value);

if(text === "---"){

alert("Encrypt text first!");
return;

}

let decrypted =
caesarCipher(text, -shift);

document.getElementById(
"decryptedOutput"
).innerText = decrypted;

}

function clearAll(){

document.getElementById("inputText").value = "";
document.getElementById("shift").value = "";

document.getElementById(
"encryptedOutput"
).innerText = "---";

document.getElementById(
"decryptedOutput"
).innerText = "---";

}