-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathcaesar-cipher.html
More file actions
355 lines (321 loc) · 12.3 KB
/
Copy pathcaesar-cipher.html
File metadata and controls
355 lines (321 loc) · 12.3 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Caesar Cipher — DevDunia</title>
<meta name="description" content="Encode and decode text using Caesar cipher with custom shift values. Free online Caesar cipher encoder/decoder for developers.">
<meta property="og:type" content="website">
<meta property="og:url" content="https://devdunia.com/caesar-cipher.html">
<meta property="og:title" content="Caesar Cipher — DevDunia">
<meta property="og:description" content="Encode and decode text using Caesar cipher with custom shift values. Free online Caesar cipher encoder/decoder.">
<meta property="og:image" content="https://devdunia.com/images/logo.png">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="stylesheet" href="css/comic.css">
<link href="https://fonts.googleapis.com/css2?family=Bangers&family=Comic+Neue:wght@400;700&family=Space+Mono:wght@400;700&display=swap" rel="stylesheet">
<style>
/* ---- TODO migrate: page-specific overrides, not yet centralized in css/comic.css ---- */
.hero-text h1 {
font-family: 'Bangers', cursive;
font-size: 2.8rem;
letter-spacing: 2px;
color: var(--red);
-webkit-text-stroke: 1px var(--ink);
text-shadow: 4px 4px 0 var(--ink);
line-height: 1;
text-transform: uppercase;
}
.comic-textarea:focus { border-color: var(--red); box-shadow: inset 3px 3px 0 rgba(230,57,70,.1); }
/* Shift input */
.shift-row {
display: flex;
align-items: center;
gap: 12px;
margin-bottom: 16px;
flex-wrap: wrap;
}
.shift-label {
font-family: 'Bangers', cursive;
font-size: 1.1rem;
letter-spacing: 1px;
text-transform: uppercase;
}
.comic-input {
background: #fff;
border: 3px solid var(--ink);
border-radius: 6px;
padding: 8px 12px;
font-family: 'Space Mono', monospace;
font-size: 1rem;
color: var(--ink);
width: 80px;
box-shadow: 3px 3px 0 var(--ink);
outline: none;
transition: border-color .15s;
}
.comic-input:focus { border-color: var(--red); }
.shift-hint {
font-size: .8rem;
color: #777;
font-style: italic;
}
.btn-primary { background: var(--blue); color: #fff; }
.btn-purple { background: var(--purple); color: #fff; }
.examples-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 12px;
}
.example-card {
background: var(--paper2);
border: 3px solid var(--ink);
border-radius: 6px;
box-shadow: 4px 4px 0 var(--ink);
padding: 10px 12px;
cursor: pointer;
transition: transform .1s, box-shadow .1s;
}
.example-card:hover { transform: translate(-2px,-2px); box-shadow: 6px 6px 0 var(--ink); }
.example-card:active { transform: translate(1px,1px); box-shadow: 2px 2px 0 var(--ink); }
.example-title {
font-family: 'Bangers', cursive;
font-size: 1rem;
letter-spacing: 1px;
margin-bottom: 4px;
}
.example-preview {
font-family: 'Space Mono', monospace;
font-size: .68rem;
color: var(--red);
word-break: break-all;
margin-bottom: 2px;
}
.example-arrow { font-size: .75rem; color: var(--ink); opacity: .55; }
@media (max-width: 600px) {
.examples-grid { grid-template-columns: 1fr; }
.hero { flex-direction: column; gap: 10px; }
.hero-text h1 { font-size: 2rem; }
}
</style>
</head>
<body>
<div class="paper-overlay"></div>
<div data-masthead></div>
<div class="page-wrap">
<div data-tool-hero
data-title="Caesar Cipher"
data-chapter="07"
data-category="Crypto & Security"
data-icon="🔐"
data-desc="The ancient Roman encryption technique! Shift every letter by a secret number and transform your messages into coded text. Encode, decode, or brute-force all 25 shifts at once!"
data-color="purple"
data-badges="ENCODE,DECODE,BRUTE FORCE,ROT13"
data-badge-colors="red,green,blue,yellow">
</div>
<!-- MASTHEAD -->
<!-- HERO -->
<!-- INPUT PANEL -->
<div class="panel">
<div class="panel-header">
⌨ SET SHIFT & ENTER TEXT
<span class="panel-tag">INPUT</span>
</div>
<div class="panel-body">
<div class="shift-row">
<span class="shift-label">🔑 Shift Value (1–25):</span>
<input type="number" id="shift-value" class="comic-input" min="1" max="25" value="3">
<span class="shift-hint">ROT13 = shift 13</span>
</div>
<div class="section-label">Input Text</div>
<textarea id="input-text" class="comic-textarea" rows="6"
placeholder="Enter text to encode or decode with Caesar cipher..."></textarea>
<div class="btn-row">
<button id="encode-btn" class="btn btn-primary">🔒 Encode!</button>
<button id="decode-btn" class="btn btn-success">🔓 Decode!</button>
<button id="brute-force-btn" class="btn btn-purple">💥 Brute Force!</button>
<button id="clear-btn" class="btn btn-danger">🗑 Clear</button>
</div>
<div id="error-bubble" class="error-bubble">
💥 <span id="error-msg">Please enter some text!</span>
</div>
</div>
</div>
<!-- OUTPUT PANEL -->
<div class="panel" id="output-panel" style="display:none;">
<div class="panel-header">
📤 CIPHER OUTPUT
<span class="panel-tag">OUTPUT</span>
</div>
<div class="panel-body">
<div class="section-label">Result</div>
<div class="output-wrap">
<textarea id="output-text" class="comic-textarea" rows="6" readonly></textarea>
<button id="copy-btn" class="copy-btn">📋 COPY</button>
</div>
</div>
</div>
<!-- EXAMPLES PANEL -->
<div class="panel">
<div class="panel-header">
📚 TRY AN EXAMPLE — CLICK TO LOAD!
<span class="panel-tag">EXAMPLES</span>
</div>
<div class="panel-body">
<div class="examples-grid">
<div class="example-card" data-text="Hello World" data-shift="3" data-mode="encode">
<div class="example-title" style="color:var(--blue);">Encode (Shift 3)</div>
<div class="example-preview">"Hello World"</div>
<div class="example-arrow">↓ Khoor Zruog</div>
</div>
<div class="example-card" data-text="Khoor Zruog" data-shift="3" data-mode="decode">
<div class="example-title" style="color:var(--green);">Decode (Shift 3)</div>
<div class="example-preview">"Khoor Zruog"</div>
<div class="example-arrow">↓ Hello World</div>
</div>
<div class="example-card" data-text="Hello World" data-shift="13" data-mode="encode">
<div class="example-title" style="color:var(--purple);">ROT13 (Shift 13)</div>
<div class="example-preview">"Hello World"</div>
<div class="example-arrow">↓ Uryyb Jbeyq</div>
</div>
<div class="example-card" data-text="DevDunia Rocks" data-shift="7" data-mode="encode">
<div class="example-title" style="color:var(--red);">Secret Message</div>
<div class="example-preview">"DevDunia Rocks"</div>
<div class="example-arrow">↓ Encode with shift 7</div>
</div>
</div>
</div>
</div>
<!-- HOW IT WORKS -->
<div class="panel">
<div class="panel-header">
🧠 HOW CAESAR CIPHER WORKS
<span class="panel-tag">EXPLAINER</span>
</div>
<div class="panel-body">
<div class="how-steps">
<div class="how-step">
<div class="step-dot" style="background:var(--red);">1</div>
<p>Named after Julius Caesar who used it to send secret military messages. Each letter in the alphabet is <strong>shifted</strong> by a fixed number of positions.</p>
</div>
<div class="how-step">
<div class="step-dot" style="background:var(--blue);">2</div>
<p>With shift 3: <code>A → D</code>, <code>B → E</code>, <code>Z → C</code>. The alphabet wraps around — it's modular arithmetic mod 26!</p>
</div>
<div class="how-step">
<div class="step-dot" style="background:var(--green);">3</div>
<p>Numbers, symbols, and spaces are <strong>not changed</strong>. Only A–Z and a–z are shifted. Case is preserved throughout.</p>
</div>
<div class="how-step">
<div class="step-dot" style="background:var(--purple);">4</div>
<p><strong>Brute Force</strong> tries all 25 possible shifts at once — since there are only 25 options, the correct one is always visible!</p>
</div>
<div class="how-step">
<div class="step-dot" style="background:var(--orange);">5</div>
<p><code>ROT13</code> (shift 13) is special: encoding and decoding use the same operation since 13 + 13 = 26 (full alphabet).</p>
</div>
</div>
</div>
</div>
</div><!-- /page-wrap -->
<script>
(function() {
var inputEl = document.getElementById('input-text');
var outputEl = document.getElementById('output-text');
var shiftEl = document.getElementById('shift-value');
var encodeBtn = document.getElementById('encode-btn');
var decodeBtn = document.getElementById('decode-btn');
var bruteForceBtn = document.getElementById('brute-force-btn');
var clearBtn = document.getElementById('clear-btn');
var copyBtn = document.getElementById('copy-btn');
var outPanel = document.getElementById('output-panel');
var errBubble = document.getElementById('error-bubble');
var errMsg = document.getElementById('error-msg');
function showError(msg) {
errMsg.textContent = msg;
errBubble.classList.add('show');
outPanel.style.display = 'none';
}
function hideError() { errBubble.classList.remove('show'); }
function showOutput(text) {
outputEl.value = text;
outPanel.style.display = 'block';
hideError();
}
function getShift() {
var s = parseInt(shiftEl.value, 10);
if (isNaN(s) || s < 1 || s > 25) {
showError('Shift value must be between 1 and 25!');
return null;
}
return s;
}
function caesarCipher(str, shift) {
return str.replace(/[a-zA-Z]/g, function(ch) {
var code = ch.charCodeAt(0);
var offset = code <= 90 ? 65 : 97;
return String.fromCharCode(((code - offset + shift) % 26 + 26) % 26 + offset);
});
}
encodeBtn.addEventListener('click', function() {
var input = inputEl.value.trim();
if (!input) { showError('Please enter some text to encode!'); return; }
var shift = getShift();
if (shift === null) return;
showOutput(caesarCipher(input, shift));
});
decodeBtn.addEventListener('click', function() {
var input = inputEl.value.trim();
if (!input) { showError('Please enter some text to decode!'); return; }
var shift = getShift();
if (shift === null) return;
showOutput(caesarCipher(input, -shift));
});
bruteForceBtn.addEventListener('click', function() {
var input = inputEl.value.trim();
if (!input) { showError('Please enter some text to brute force!'); return; }
var result = 'Brute Force Results (All 25 possible shifts):\n\n';
for (var s = 1; s <= 25; s++) {
result += 'Shift ' + (s < 10 ? ' ' : '') + s + ': ' + caesarCipher(input, -s) + '\n';
}
showOutput(result);
});
clearBtn.addEventListener('click', function() {
inputEl.value = '';
outputEl.value = '';
outPanel.style.display = 'none';
hideError();
});
copyBtn.addEventListener('click', function() {
outputEl.select();
try { document.execCommand('copy'); } catch(e) {}
if (navigator.clipboard) navigator.clipboard.writeText(outputEl.value).catch(function(){});
var orig = copyBtn.textContent;
copyBtn.textContent = '✔ COPIED!';
copyBtn.style.background = 'var(--green)';
setTimeout(function() { copyBtn.textContent = orig; copyBtn.style.background = 'var(--ink)'; }, 1800);
});
// Example cards
var cards = document.querySelectorAll('.example-card');
for (var i = 0; i < cards.length; i++) {
cards[i].addEventListener('click', function() {
inputEl.value = this.dataset.text;
shiftEl.value = this.dataset.shift;
var s = parseInt(this.dataset.shift, 10);
var mode = this.dataset.mode;
hideError();
if (mode === 'encode') {
showOutput(caesarCipher(this.dataset.text, s));
} else {
showOutput(caesarCipher(this.dataset.text, -s));
}
});
}
// Preload
inputEl.value = 'Hello World! This is a Caesar cipher example.';
})();
</script>
<script src="masthead.js"></script>
<script src="hero-banner.js"></script>
<script src="author-card.js"></script>
</body>
</html>