-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
165 lines (148 loc) · 4.94 KB
/
index.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
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
154
155
156
157
158
159
160
161
162
163
164
165
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Alfred Zimo's Caesar Encipher</title>
<style>
/* Body styles */
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f0f0f0;
color: #333;
}
/* Header styles */
header {
background-color: #333;
padding: 20px 0;
text-align: center;
color: #fff;
}
h1 {
font-size: 36px;
margin: 0;
}
/* Main content styles */
main {
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
/* Form styles */
form {
background-color: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
}
label, select, textarea, input {
display: block;
margin-bottom: 15px;
}
textarea {
width: 100%;
height: 100px;
resize: vertical;
}
button {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
}
button:hover {
background-color: #45a049;
}
#result {
text-align: center;
margin-top: 20px;
}
/* Footer styles */
footer {
background-color: #333;
color: #fff;
text-align: center;
padding: 20px 0;
}
</style>
</head>
<body>
<header>
<h1>Alfred Zimo's Caesar Encipher</h1>
</header>
<main>
<form id="encryptionForm">
<label for="operation">Select Operation:</label>
<select id="operation" onchange="toggleForm()">
<option value="encrypt">Encrypt</option>
<option value="decrypt">Decrypt</option>
</select>
<div id="inputFields">
<label for="text">Plaintext/Ciphertext:</label>
<textarea id="text" rows="4" cols="50" required></textarea>
<label for="key">Key (Positive Integer):</label>
<input type="number" id="key" min="1" required>
</div>
<button type="submit">Submit</button>
</form>
<div id="result"></div>
</main>
<footer>
Made with ❤️ by Alfred Zimo
</footer>
<script>
function toggleForm() {
var operation = document.getElementById("operation").value;
var keyInput = document.getElementById("key");
var labelText = operation === "encrypt" ? "Key :" : "Positive integer";
keyInput.setAttribute("placeholder", labelText);
}
document.getElementById("encryptionForm").addEventListener("submit", function(event) {
event.preventDefault();
var operation = document.getElementById("operation").value;
var text = document.getElementById("text").value;
var key = parseInt(document.getElementById("key").value);
var result = "";
if (operation === "encrypt") {
result = encrypt(text, key);
} else {
result = decrypt(text, key);
}
document.getElementById("result").innerText = "Result: " + result;
});
function encrypt(plaintext, key) {
var ciphertext = "";
for (var i = 0; i < plaintext.length; i++) {
var charCode = plaintext.charCodeAt(i);
if (charCode >= 65 && charCode <= 90) { // Uppercase letters
ciphertext += String.fromCharCode((charCode - 65 + key) % 26 + 65);
} else if (charCode >= 97 && charCode <= 122) { // Lowercase letters
ciphertext += String.fromCharCode((charCode - 97 + key) % 26 + 97);
} else {
ciphertext += plaintext[i]; // Non-alphabetic characters remain unchanged
}
}
return ciphertext;
}
function decrypt(ciphertext, key) {
var plaintext = "";
for (var i = 0; i < ciphertext.length; i++) {
var charCode = ciphertext.charCodeAt(i);
if (charCode >= 65 && charCode <= 90) { // Uppercase letters
plaintext += String.fromCharCode((charCode - 65 - key + 26) % 26 + 65);
} else if (charCode >= 97 && charCode <= 122) { // Lowercase letters
plaintext += String.fromCharCode((charCode - 97 - key + 26) % 26 + 97);
} else {
plaintext += ciphertext[i]; // Non-alphabetic characters remain unchanged
}
}
return plaintext;
}
</script>
</body>
</html>