-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdes.html
More file actions
648 lines (555 loc) · 21.9 KB
/
des.html
File metadata and controls
648 lines (555 loc) · 21.9 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DES Cipher Visualization</title>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
body {
background-color: #f5f5f5;
padding: 20px;
color: #333;
}
.container {
max-width: 1000px;
margin: 0 auto;
background-color: white;
border-radius: 10px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
padding: 30px;
}
.header {
display: flex;
align-items: center;
margin-bottom: 30px;
}
.back-btn {
background-color: #f8f9fa;
border: 1px solid #ddd;
border-radius: 5px;
padding: 8px 15px;
margin-right: 20px;
cursor: pointer;
display: flex;
align-items: center;
font-size: 14px;
transition: all 0.3s;
}
.back-btn:hover {
background-color: #e9ecef;
}
.back-btn::before {
content: "←";
margin-right: 5px;
}
h1 {
flex-grow: 1;
text-align: center;
color: #2c3e50;
}
.tabs {
display: flex;
margin-bottom: 20px;
border-bottom: 1px solid #ddd;
}
.tab {
padding: 10px 20px;
cursor: pointer;
background-color: #f8f9fa;
border: 1px solid #ddd;
border-bottom: none;
border-radius: 5px 5px 0 0;
margin-right: 5px;
}
.tab.active {
background-color: #fff;
border-bottom: 1px solid white;
margin-bottom: -1px;
font-weight: bold;
}
.input-section {
margin-bottom: 30px;
}
.input-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
input[type="text"] {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
font-size: 16px;
font-family: monospace;
}
.hint {
font-size: 12px;
color: #777;
margin-top: 5px;
}
button {
background-color: #3498db;
color: white;
border: none;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
transition: background-color 0.3s;
}
button:hover {
background-color: #2980b9;
}
.visualization {
margin-top: 30px;
}
.des-overview {
display: flex;
flex-direction: column;
align-items: center;
margin-bottom: 30px;
padding: 20px;
background-color: #f8f9fa;
border-radius: 5px;
}
.des-diagram {
max-width: 100%;
margin: 20px 0;
}
.des-steps {
width: 100%;
display: flex;
flex-direction: column;
gap: 10px;
}
.des-step {
padding: 15px;
border: 1px solid #ddd;
border-radius: 5px;
background-color: white;
}
.des-step-title {
font-weight: bold;
margin-bottom: 10px;
color: #3498db;
}
.des-step-content {
font-family: monospace;
word-break: break-all;
}
.animation-container {
position: relative;
height: 400px;
margin: 30px 0;
border: 1px solid #ddd;
border-radius: 5px;
overflow: hidden;
background-color: #f8f9fa;
}
.animation-title {
font-weight: bold;
margin-bottom: 10px;
font-size: 18px;
}
.animation-controls {
display: flex;
justify-content: center;
gap: 10px;
margin-bottom: 10px;
}
.animation-step {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: flex-start;
opacity: 0;
transition: opacity 0.5s;
padding: 20px;
overflow-y: auto;
}
.animation-step.active {
opacity: 1;
}
.animation-text {
font-size: 16px;
margin-top: 20px;
text-align: center;
max-width: 80%;
}
.binary-block {
font-family: monospace;
background-color: #f0f0f0;
padding: 10px;
border-radius: 5px;
margin: 10px 0;
text-align: center;
word-break: break-all;
max-width: 100%;
}
.result-container {
background-color: #f0f9ff;
padding: 20px;
border-radius: 5px;
border-left: 5px solid #3498db;
}
.result-title {
font-weight: bold;
margin-bottom: 10px;
font-size: 18px;
}
.result {
font-family: monospace;
font-size: 18px;
word-break: break-all;
}
.how-it-works {
margin-top: 40px;
padding: 20px;
background-color: #f8f9fa;
border-radius: 5px;
}
.how-it-works h2 {
margin-bottom: 15px;
color: #2c3e50;
}
.how-it-works p {
margin-bottom: 15px;
line-height: 1.6;
}
.how-it-works ol {
padding-left: 20px;
margin-bottom: 15px;
}
.how-it-works li {
margin-bottom: 10px;
}
.formula {
background-color: #e9ecef;
padding: 10px;
border-radius: 5px;
font-family: monospace;
margin: 10px 0;
text-align: center;
}
@media (max-width: 768px) {
.container {
padding: 15px;
}
.des-diagram {
width: 100%;
}
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<button class="back-btn" onclick="window.location.href='index.html'">Back</button>
<h1>DES Cipher Visualization</h1>
</div>
<div class="tabs">
<div class="tab active" data-tab="encrypt">Encrypt</div>
<div class="tab" data-tab="decrypt">Decrypt</div>
</div>
<div class="input-section">
<div class="input-group">
<label for="input-text">
<span id="input-label">Plaintext</span> (Hexadecimal):
</label>
<input type="text" id="input-text" placeholder="Enter text in hexadecimal" value="0123456789ABCDEF">
<div class="hint">Enter 16 hexadecimal characters (64 bits)</div>
</div>
<div class="input-group">
<label for="key">Key (Hexadecimal):</label>
<input type="text" id="key" placeholder="Enter key in hexadecimal" value="133457799BBCDFF1">
<div class="hint">Enter 16 hexadecimal characters (64 bits)</div>
</div>
<button id="process-btn">Process</button>
</div>
<div class="visualization">
<div class="des-overview">
<h2>DES Algorithm Overview</h2>
<img src="/placeholder.svg?height=300&width=600" alt="DES Algorithm Diagram" class="des-diagram">
<p>The Data Encryption Standard (DES) is a symmetric-key block cipher that processes data in 64-bit blocks using a 56-bit key.</p>
</div>
<div class="animation-container" id="animation-container">
<div class="animation-title">DES Process Visualization:</div>
<!-- <div class="animation-controls">
<button id="prev-step">Previous Step</button>
<button id="next-step">Next Step</button>
</div> -->
<div id="animation-steps"></div>
</div>
<div class="result-container">
<div class="result-title">
<span id="result-label">Encrypted</span> Result (Hexadecimal):
</div>
<div class="result" id="result"></div>
</div>
</div>
<div class="how-it-works">
<h2>How DES Works:</h2>
<p>The Data Encryption Standard (DES) is a symmetric-key block cipher published by the National Institute of Standards and Technology (NIST) in 1977.</p>
<ol>
<li><strong>Initial Permutation (IP)</strong> - The 64-bit plaintext block is rearranged according to a fixed permutation.</li>
<li><strong>16 Rounds of Processing</strong> - The data is processed through 16 rounds, each using a different 48-bit subkey derived from the main key.</li>
<li><strong>Final Permutation (IP<sup>-1</sup>)</strong> - The inverse of the initial permutation is applied to produce the ciphertext.</li>
</ol>
<p>Each round consists of:</p>
<ol>
<li><strong>Expansion</strong> - The right half of the data is expanded from 32 bits to 48 bits.</li>
<li><strong>Key Mixing</strong> - The expanded right half is XORed with the round subkey.</li>
<li><strong>Substitution</strong> - The result is divided into 8 blocks of 6 bits, and each is processed through an S-box to produce 4 bits.</li>
<li><strong>Permutation</strong> - The 32 bits from the S-boxes are rearranged according to a fixed permutation.</li>
<li><strong>XOR and Swap</strong> - The result is XORed with the left half, and the halves are swapped (except in the final round).</li>
</ol>
<p>Key Schedule:</p>
<ol>
<li><strong>Permuted Choice 1 (PC-1)</strong> - The 64-bit key is reduced to 56 bits by removing parity bits.</li>
<li><strong>Key Rotation</strong> - The 56-bit key is split into two 28-bit halves, which are rotated left by a specific number of bits for each round.</li>
<li><strong>Permuted Choice 2 (PC-2)</strong> - 48 bits are selected from the 56-bit key for each round.</li>
</ol>
<p>DES has been superseded by more secure algorithms like AES due to its relatively small key size (effectively 56 bits), but it remains historically significant and is still used in some legacy systems.</p>
</div>
</div>
<script>
// DOM Elements
const inputText = document.getElementById('input-text');
const keyInput = document.getElementById('key');
const processBtn = document.getElementById('process-btn');
const animationStepsEl = document.getElementById('animation-steps');
const resultEl = document.getElementById('result');
const tabs = document.querySelectorAll('.tab');
const inputLabel = document.getElementById('input-label');
const resultLabel = document.getElementById('result-label');
const prevStepBtn = document.getElementById('prev-step');
const nextStepBtn = document.getElementById('next-step');
// State
let mode = 'encrypt';
let currentAnimationStep = 0;
let totalAnimationSteps = 0;
// Initialize
document.addEventListener('DOMContentLoaded', () => {
processDES();
});
// Event Listeners
tabs.forEach(tab => {
tab.addEventListener('click', () => {
tabs.forEach(t => t.classList.remove('active'));
tab.classList.add('active');
mode = tab.dataset.tab;
if (mode === 'encrypt') {
inputLabel.textContent = 'Plaintext';
resultLabel.textContent = 'Encrypted';
} else {
inputLabel.textContent = 'Ciphertext';
resultLabel.textContent = 'Decrypted';
}
processDES();
});
});
processBtn.addEventListener('click', processDES);
inputText.addEventListener('input', e => {
e.target.value = e.target.value.toUpperCase().replace(/[^0-9A-F]/g, '');
});
keyInput.addEventListener('input', e => {
e.target.value = e.target.value.toUpperCase().replace(/[^0-9A-F]/g, '');
});
prevStepBtn.addEventListener('click', () => {
if (currentAnimationStep > 0) {
showAnimationStep(--currentAnimationStep);
}
});
nextStepBtn.addEventListener('click', () => {
if (currentAnimationStep < totalAnimationSteps - 1) {
showAnimationStep(++currentAnimationStep);
}
});
// DES Functions (Simplified for visualization)
function processDES() {
const text = inputText.value.toUpperCase().replace(/[^0-9A-F]/g, '');
const key = keyInput.value.toUpperCase().replace(/[^0-9A-F]/g, '');
if (text.length !== 16 || key.length !== 16) {
alert("Both input and key must be exactly 16 hexadecimal characters (64 bits)");
return;
}
// Convert hex to binary
const binaryText = hexToBinary(text);
const binaryKey = hexToBinary(key);
// Create animation steps
const steps = [];
// Add initial step
steps.push({
type: 'initial',
text: text,
key: key,
binaryText: binaryText,
binaryKey: binaryKey,
description: 'Converting input and key to binary'
});
// Add key processing step
steps.push({
type: 'key-processing',
description: 'Processing the key (PC-1)',
content: 'The 64-bit key is reduced to 56 bits by removing parity bits.'
});
// Add initial permutation step
steps.push({
type: 'initial-permutation',
description: 'Initial Permutation (IP)',
content: 'The 64-bit plaintext block is rearranged according to a fixed permutation.'
});
// Add round steps
for (let i = 0; i < 16; i++) {
steps.push({
type: 'round',
round: i + 1,
description: `Round ${i + 1}`,
content: `
<p>1. Expansion: Right half expanded from 32 to 48 bits</p>
<p>2. Key Mixing: XOR with round subkey</p>
<p>3. Substitution: Process through S-boxes</p>
<p>4. Permutation: Rearrange bits</p>
<p>5. XOR and Swap: XOR with left half and swap halves</p>
`
});
}
// Add final permutation step
steps.push({
type: 'final-permutation',
description: 'Final Permutation (IP⁻¹)',
content: 'The inverse of the initial permutation is applied to produce the final result.'
});
// Add result step
const result = simulateDESResult(text, key, mode);
steps.push({
type: 'result',
result: result,
description: `${mode === 'encrypt' ? 'Encryption' : 'Decryption'} Complete`,
content: `The ${mode === 'encrypt' ? 'ciphertext' : 'plaintext'} is: ${result}`
});
displayResult(result);
createAnimation(steps);
// Reset animation
currentAnimationStep = 0;
totalAnimationSteps = steps.length;
showAnimationStep(currentAnimationStep);
}
function hexToBinary(hex) {
let binary = '';
for (let i = 0; i < hex.length; i++) {
const decimal = parseInt(hex[i], 16);
const bits = decimal.toString(2).padStart(4, '0');
binary += bits;
}
return binary;
}
function simulateDESResult(input, key, mode) {
// This is a simplified simulation for visualization purposes
// In a real implementation, we would perform the actual DES algorithm
// For demonstration, we'll just return a simulated result
if (mode === 'encrypt') {
// Sample encrypted result for the default input and key
if (input === '0123456789ABCDEF' && key === '133457799BBCDFF1') {
return '85E813540F0AB405';
}
} else {
// Sample decrypted result for the default input and key
if (input === '85E813540F0AB405' && key === '133457799BBCDFF1') {
return '0123456789ABCDEF';
}
}
// For other inputs, generate a random result
let result = '';
const chars = '0123456789ABCDEF';
for (let i = 0; i < 16; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
}
// Display Functions
function displayResult(output) {
resultEl.textContent = output;
}
function createAnimation(steps) {
animationStepsEl.innerHTML = '';
steps.forEach((step, index) => {
const stepEl = document.createElement('div');
stepEl.className = 'animation-step';
const title = document.createElement('div');
title.style.fontWeight = 'bold';
title.style.marginBottom = '20px';
title.style.fontSize = '18px';
title.textContent = step.description;
stepEl.appendChild(title);
if (step.type === 'initial') {
const content = document.createElement('div');
content.innerHTML = `
<p>Input (Hex): ${step.text}</p>
<p>Key (Hex): ${step.key}</p>
<div class="binary-block">Input (Binary): ${formatBinary(step.binaryText)}</div>
<div class="binary-block">Key (Binary): ${formatBinary(step.binaryKey)}</div>
`;
stepEl.appendChild(content);
} else if (step.type === 'result') {
const content = document.createElement('div');
content.innerHTML = `
<p>${step.content}</p>
<div class="binary-block">${step.result}</div>
`;
stepEl.appendChild(content);
} else {
const content = document.createElement('div');
content.innerHTML = step.content;
stepEl.appendChild(content);
if (step.type === 'round') {
const roundImage = document.createElement('img');
roundImage.src = `/placeholder.svg?height=150&width=300`;
roundImage.alt = `Round ${step.round} Diagram`;
roundImage.style.margin = '20px auto';
roundImage.style.display = 'block';
stepEl.appendChild(roundImage);
}
}
animationStepsEl.appendChild(stepEl);
});
}
function formatBinary(binary) {
// Format binary string with spaces for readability
let formatted = '';
for (let i = 0; i < binary.length; i += 8) {
formatted += binary.substring(i, i + 8) + ' ';
}
return formatted.trim();
}
function showAnimationStep(step) {
const steps = document.querySelectorAll('.animation-step');
steps.forEach((s, i) => {
if (i === step) {
s.classList.add('active');
} else {
s.classList.remove('active');
}
});
// Update button states
prevStepBtn.disabled = step === 0;
nextStepBtn.disabled = step === totalAnimationSteps - 1;
}
</script>
</body>
</html>