-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shifting_Cipher.html
70 lines (55 loc) · 2.01 KB
/
Shifting_Cipher.html
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
<!DOCTYPE html>
<html>
<body>
<center>
<h2>Caesar Ciphering / Deciphering</h2>
<br>
<textarea rows="4" cols="50" id="textbox"></textarea>
<br>
<input type="number" step="1" min="0" id="shift">
<br>
<button id="btn">Cipher</button>
<button id="debtn">Decipher</button>
<br>
<hr>
<h2>Result</h2>
<textarea rows="4" cols="50" id="result"></textarea>
<script>
let alphabetArr = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];
function cipher(){
var textArr = document.getElementById("textbox").value.split("");
var shift = parseInt(document.getElementById("shift").value);
for(let k=0; k<textArr.length; k++){
if ((textArr[k] == ' ') || (textArr[k] == '\t') || (textArr[k] == '\n' || alphabetArr.indexOf(textArr[k].toUpperCase())==-1)){
continue;
}else if(alphabetArr.indexOf(textArr[k].toUpperCase())+shift > 25){
textArr[k]=alphabetArr[alphabetArr.indexOf(textArr[k].toUpperCase())+shift-26];
}else {
textArr[k]=alphabetArr[alphabetArr.indexOf(textArr[k].toUpperCase())+shift];
}
}document.getElementById("result").innerHTML = textArr.join().replace(/,/g, '')
}
function decipher(){
var textArr = document.getElementById("textbox").value.split("");
var shift = parseInt(document.getElementById("shift").value);
for(let k=0; k<textArr.length; k++){
if ((textArr[k] == ' ') || (textArr[k] == '\t') || (textArr[k] == '\n' || alphabetArr.indexOf(textArr[k].toUpperCase())==-1)){
continue;
}else if(alphabetArr.indexOf(textArr[k].toUpperCase())-shift < 0){
textArr[k]=alphabetArr[alphabetArr.indexOf(textArr[k].toUpperCase())+26-shift];
}else {
textArr[k]=alphabetArr[alphabetArr.indexOf(textArr[k].toUpperCase())-shift];
}
}
document.getElementById("result").innerHTML = textArr.join().replace(/,/g, '')
}
document.getElementById("btn").onclick=function(){
cipher();
}
document.getElementById("debtn").onclick=function(){
decipher();
}
</script>
</center>
</body>
</html>