-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaeser Cipher.html
More file actions
112 lines (92 loc) · 3.29 KB
/
Copy pathCaeser Cipher.html
File metadata and controls
112 lines (92 loc) · 3.29 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Caesar Cipher</title>
<style>
body {
font-family: 'Arial', sans-serif;
text-align: center;
margin: 50px;
background-color: #f5f5f5;
}
.container {
max-width: 400px;
margin: auto;
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
input, button {
width: 100%;
margin: 10px 0;
padding: 12px;
font-size: 16px;
box-sizing: border-box;
}
button {
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
h2, h3 {
color: #333;
}
#result {
margin-top: 20px;
font-size: 18px;
color: #333;
}
</style>
</head>
<body>
<div class="container">
<h2>Caesar Cipher</h2>
<label for="plainText">Enter Plain Text:</label>
<input type="text" id="plainText" placeholder="Enter text">
<label for="shift">Enter Shift Value:</label>
<input type="number" id="shift" placeholder="Enter shift value">
<button onclick="encrypt()">Encrypt</button>
<button onclick="decrypt()">Decrypt</button>
<h3>Result:</h3>
<p id="result"></p>
</div>
<script>
function encrypt() {
var plainText = document.getElementById("plainText").value.toUpperCase();
var shift = parseInt(document.getElementById("shift").value);
var result = "";
for (var i = 0; i < plainText.length; i++) {
var charCode = plainText.charCodeAt(i);
if (charCode >= 65 && charCode <= 90) { // Uppercase letters only
result += String.fromCharCode(((charCode - 65 + shift) % 26) + 65);
} else {
result += plainText[i]; // Leave non-alphabetic characters unchanged
}
}
document.getElementById("result").innerText = "Encrypted Text: " + result;
}
function decrypt() {
var cipherText = document.getElementById("plainText").value.toUpperCase();
var shift = parseInt(document.getElementById("shift").value);
var result = "";
for (var i = 0; i < cipherText.length; i++) {
var charCode = cipherText.charCodeAt(i);
if (charCode >= 65 && charCode <= 90) { // Uppercase letters only
result += String.fromCharCode(((charCode - 65 - shift + 26) % 26) + 65);
} else {
result += cipherText[i]; // Leave non-alphabetic characters unchanged
}
}
document.getElementById("result").innerText = "Decrypted Text: " + result;
}
</script>
</body>
</html>