-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
651 lines (598 loc) · 27 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
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
649
650
651
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>DES Encryption Detailed Visualization</title>
<!-- Include Mermaid.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/mermaid/9.3.0/mermaid.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
max-width: 90%;
margin: 20px auto;
line-height: 1.6;
color: #333;
font-size: 16px;
}
h1 {
text-align: center;
color: #2c3e50;
}
#controls {
margin-top: 20px;
display: flex;
flex-direction: column;
align-items: center;
}
#controls input {
padding: 8px;
margin: 5px 0;
width: 300px;
font-size: 16px;
}
#controls button {
padding: 10px 20px;
margin: 10px 5px;
font-size: 16px;
cursor: pointer;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
}
#controls button:disabled {
background-color: #cccccc;
cursor: not-allowed;
}
#explanationBox {
width: 100%;
padding: 15px;
background-color: #f8f9fa;
border: 1px solid #007bff;
border-radius: 5px;
margin-top: 20px;
box-sizing: border-box;
overflow-x: auto;
}
.binary, .hexadecimal {
font-family: monospace;
background-color: #e0e0e0;
padding: 2px 4px;
border-radius: 3px;
display: inline-block;
word-break: break-all;
}
.mermaid {
text-align: center;
margin-bottom: 20px;
font-size: 16px;
max-width: none;
}
.highlight {
fill: #ffeb3b !important;
}
.diagram-container {
display: flex;
flex-direction: column;
align-items: center;
width: 100%;
overflow-x: auto;
}
.diagram-container .mermaid {
width: 100%;
}
table {
border-collapse: collapse;
margin-top: 10px;
}
table, th, td {
border: 1px solid #333;
padding: 5px;
text-align: center;
}
.table-container {
overflow-x: auto;
margin-top: 10px;
}
.step-title {
color: #2c3e50;
}
#navigationButtons {
margin-top: 20px;
display: flex;
justify-content: center;
}
</style>
</head>
<body>
<h1>DES Encryption Detailed Visualization</h1>
<div id="controls">
<label for="plaintextInput"><strong>Plaintext (Hexadecimal):</strong></label>
<input type="text" id="plaintextInput" placeholder="e.g., 0123456789ABCDEF" maxlength="16">
<label for="keyInput"><strong>Key (Hexadecimal):</strong></label>
<input type="text" id="keyInput" placeholder="e.g., 133457799BBCDFF1" maxlength="16">
<button id="startButton">Start Visualization</button>
</div>
<div class="diagram-container">
<div class="mermaid" id="diagram">
<!-- The diagram will be dynamically updated -->
</div>
</div>
<div id="explanationBox" style="display:none;"></div>
<div id="navigationButtons" style="display:none;">
<button id="prevButton">Previous</button>
<button id="nextButton">Next</button>
</div>
<script>
// Initialize Mermaid with custom configuration
mermaid.initialize({
startOnLoad: false,
theme: 'neutral',
themeVariables: {
fontFamily: 'Arial, sans-serif',
fontSize: '16px',
nodeTextSize: '16px',
noteTextSize: '16px',
primaryColor: '#f4f9f9',
primaryTextColor: '#000',
primaryBorderColor: '#333',
lineColor: '#333',
}
});
// Default plaintext and key (hexadecimal)
const defaultPlaintextHex = "0123456789ABCDEF";
const defaultKeyHex = "133457799BBCDFF1";
// Elements
const startButton = document.getElementById('startButton');
const plaintextInput = document.getElementById('plaintextInput');
const keyInput = document.getElementById('keyInput');
const prevButton = document.getElementById('prevButton');
const nextButton = document.getElementById('nextButton');
const navigationButtons = document.getElementById('navigationButtons');
const explanationBox = document.getElementById('explanationBox');
// Variables to store user inputs or defaults
let plaintextHex = defaultPlaintextHex;
let keyHex = defaultKeyHex;
// Steps data
let steps = [];
let currentStep = 0;
// Placeholder variables to store intermediate results
let permutedPlaintext, L0, R0, permutedKey, C0, D0, roundKeys = [], finalCiphertext;
let L = [], R = [];
// Function to validate hexadecimal input
function isValidHex(hex) {
return /^[0-9A-Fa-f]+$/.test(hex);
}
// Function to convert hex to binary array
function hexToBinaryArray(hex) {
return hex.split('').map(char => {
return parseInt(char, 16).toString(2).padStart(4, '0');
}).join('').split('').map(bit => parseInt(bit));
}
// Function to convert binary array to hexadecimal string
function binaryToHex(binaryArray) {
let hex = '';
for (let i = 0; i < binaryArray.length; i += 4) {
const nibble = binaryArray.slice(i, i + 4).join('');
hex += parseInt(nibble, 2).toString(16).toUpperCase();
}
return hex;
}
// Permutation and shift tables
const INITIAL_PERMUTATION = [
58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6,
64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7
];
const INVERSE_INITIAL_PERMUTATION = [
40, 8, 48, 16, 56, 24, 64, 32,
39, 7, 47, 15, 55, 23, 63, 31,
38, 6, 46, 14, 54, 22, 62, 30,
37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28,
35, 3, 43, 11, 51, 19, 59, 27,
34, 2, 42, 10, 50, 18, 58, 26,
33, 1, 41, 9, 49, 17, 57, 25
];
const PC1 = [
57, 49, 41, 33, 25, 17, 9,
1, 58, 50, 42, 34, 26, 18,
10, 2, 59, 51, 43, 35, 27,
19, 11, 3, 60, 52, 44, 36,
63, 55, 47, 39, 31, 23, 15,
7, 62, 54, 46, 38, 30, 22,
14, 6, 61, 53, 45, 37, 29,
21, 13, 5, 28, 20, 12, 4
];
const PC2 = [
14, 17, 11, 24, 1, 5,
3, 28, 15, 6, 21, 10,
23, 19, 12, 4, 26, 8,
16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55,
30, 40, 51, 45, 33, 48,
44, 49, 39, 56, 34, 53,
46, 42, 50, 36, 29, 32
];
const SHIFT_SCHEDULE = [1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1];
// Feistel function components
const EXPANSION_TABLE = [
32, 1, 2, 3, 4, 5,
4, 5, 6, 7, 8, 9,
8, 9, 10, 11, 12, 13,
12, 13, 14, 15, 16, 17,
16, 17, 18, 19, 20, 21,
20, 21, 22, 23, 24, 25,
24, 25, 26, 27, 28, 29,
28, 29, 30, 31, 32, 1
];
const S_BOXES = [
// S1
[
[14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7],
[0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8],
[4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0],
[15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13]
],
// S2
[
[15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10],
[3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5],
[0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15],
[13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9]
],
// S3
[
[10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8],
[13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1],
[13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7],
[1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12]
],
// S4
[
[7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15],
[13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9],
[10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4],
[3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14]
],
// S5
[
[2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9],
[14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6],
[4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14],
[11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3]
],
// S6
[
[12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11],
[10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8],
[9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6],
[4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13]
],
// S7
[
[4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1],
[13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6],
[1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2],
[6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12]
],
// S8
[
[13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7],
[1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2],
[7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8],
[2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11]
]
];
const P_BOX = [
16, 7, 20, 21, 29, 12, 28, 17,
1, 15, 23, 26, 5, 18, 31, 10,
2, 8, 24, 14, 32, 27, 3, 9,
19, 13, 30, 6, 22, 11, 4, 25
];
// Function to perform permutations
function permute(inputBits, table) {
return table.map(index => inputBits[index - 1]);
}
// Left circular shift
function leftShift(bits, shifts) {
return bits.slice(shifts).concat(bits.slice(0, shifts));
}
// XOR two bit arrays
function xor(a, b) {
return a.map((bit, index) => bit ^ b[index]);
}
// Function to generate round keys
function generateRoundKeys(C0, D0) {
const roundKeys = [];
let C = C0.slice();
let D = D0.slice();
for (let i = 0; i < 16; i++) {
C = leftShift(C, SHIFT_SCHEDULE[i]);
D = leftShift(D, SHIFT_SCHEDULE[i]);
const combined = C.concat(D);
const roundKey = permute(combined, PC2);
roundKeys.push({ round: i + 1, key: roundKey });
}
return roundKeys;
}
// Function to perform the Feistel function
function feistelFunction(R, K) {
// Expansion
const expandedR = permute(R, EXPANSION_TABLE);
// Key mixing
const xorResult = xor(expandedR, K);
// S-box substitution
const sboxResult = [];
for (let i = 0; i < 8; i++) {
const block = xorResult.slice(i * 6, (i + 1) * 6);
const row = parseInt(`${block[0]}${block[5]}`, 2);
const col = parseInt(block.slice(1, 5).join(''), 2);
const sboxValue = S_BOXES[i][row][col];
const sboxBits = parseInt(sboxValue).toString(2).padStart(4, '0').split('').map(bit => parseInt(bit));
sboxResult.push(...sboxBits);
}
// Permutation
const fResult = permute(sboxResult, P_BOX);
return fResult;
}
// Function to generate the diagram with highlighted step
function generateDiagram() {
let diagramContent = `graph TD
subgraph "DES Encryption"
A[Original Plaintext] --> B[Initial Permutation]
B --> C[Split into L0 and R0]
D[Original Key] --> E["Parity Drop (PC-1)"]
E --> F[Split into C0 and D0]
F --> G[Key Schedule Generation]
C --> H[Feistel Rounds]
G --> H
H --> I[Swap Halves]
I --> J[Inverse Initial Permutation]
J --> K[Final Ciphertext]
end
`;
// Highlight current step
steps.forEach((step, index) => {
if (index === currentStep) {
diagramContent += `\nstyle ${step.id} fill:#ffeb3b,stroke:#333,stroke-width:2px;`;
} else {
diagramContent += `\nstyle ${step.id} fill:#f4f9f9,stroke:#333,stroke-width:2px;`;
}
});
// Update the diagram using mermaid.render()
mermaid.render('mermaidGraph', diagramContent, function(svgCode) {
document.getElementById('diagram').innerHTML = svgCode;
});
// Show explanation
showExplanation(steps[currentStep].id);
// Update navigation buttons' state
updateNavigationButtons();
}
// Function to show explanation
function showExplanation(id) {
const step = steps.find(s => s.id === id);
explanationBox.innerHTML = `<h2 class="step-title">${step.title}</h2><p>${step.content}</p>`;
// Add additional data for certain steps
switch (id) {
case 'A':
explanationBox.innerHTML += `<p>Plaintext Binary: <span class="binary">${hexToBinaryArray(plaintextHex).join('')}</span></p>`;
break;
case 'B':
permutedPlaintext = permute(plaintextBinary, INITIAL_PERMUTATION);
explanationBox.innerHTML += `<p>After Initial Permutation:<br>Binary: <span class="binary">${permutedPlaintext.join('')}</span></p>`;
explanationBox.innerHTML += generatePermutationTable(INITIAL_PERMUTATION, 'Initial Permutation Table');
break;
case 'C':
L0 = permutedPlaintext.slice(0, 32);
R0 = permutedPlaintext.slice(32);
explanationBox.innerHTML += `<p>L0:<br><span class="binary">${L0.join('')}</span></p><p>R0:<br><span class="binary">${R0.join('')}</span></p>`;
break;
case 'D':
explanationBox.innerHTML += `<p>Key Binary: <span class="binary">${keyBinary.join('')}</span></p>`;
break;
case 'E':
permutedKey = permute(keyBinary, PC1);
explanationBox.innerHTML += `<p>After PC-1 Permutation (56 bits):<br>Binary: <span class="binary">${permutedKey.join('')}</span></p>`;
explanationBox.innerHTML += generatePermutationTable(PC1, 'PC-1 Table');
break;
case 'F':
C0 = permutedKey.slice(0, 28);
D0 = permutedKey.slice(28);
explanationBox.innerHTML += `<p>C0 (28 bits):<br><span class="binary">${C0.join('')}</span></p><p>D0 (28 bits):<br><span class="binary">${D0.join('')}</span></p>`;
break;
case 'G':
roundKeys = generateRoundKeys(C0, D0);
explanationBox.innerHTML += `<p>Generated 16 Round Keys (48 bits each):</p>`;
roundKeys.forEach(rk => {
explanationBox.innerHTML += `<p>K${rk.round}: <span class="binary">${rk.key.join('')}</span></p>`;
});
explanationBox.innerHTML += generatePermutationTable(PC2, 'PC-2 Table');
break;
case 'H':
explanationBox.innerHTML += `<p>The Feistel rounds involve expansion, key mixing, substitution via S-boxes, and permutation. Use the "Previous" and "Next" buttons to navigate through each round.</p>`;
// Initialize L and R arrays
L = [L0];
R = [R0];
break;
default:
if (id.startsWith('Round')) {
const roundNumber = parseInt(id.replace('Round', ''));
performFeistelRound(roundNumber);
} else if (id === 'I') {
explanationBox.innerHTML += `<p>Before Swap:<br>L16: <span class="binary">${L[16].join('')}</span><br>R16: <span class="binary">${R[16].join('')}</span></p>`;
explanationBox.innerHTML += `<p>After Swap:<br>Left: <span class="binary">${R[16].join('')}</span><br>Right: <span class="binary">${L[16].join('')}</span></p>`;
} else if (id === 'J') {
const preOutput = R[16].concat(L[16]);
finalCiphertext = permute(preOutput, INVERSE_INITIAL_PERMUTATION);
explanationBox.innerHTML += `<p>After Inverse Initial Permutation:<br>Binary: <span class="binary">${finalCiphertext.join('')}</span></p>`;
explanationBox.innerHTML += generatePermutationTable(INVERSE_INITIAL_PERMUTATION, 'Inverse Initial Permutation Table');
} else if (id === 'K') {
const ciphertextBinaryStr = finalCiphertext.join('');
const ciphertextHex = binaryToHex(finalCiphertext);
explanationBox.innerHTML += `<p>Final Ciphertext:</p><p>Binary: <span class="binary" id="ciphertextBinary">${ciphertextBinaryStr}</span><br>Hexadecimal: <span class="hexadecimal" id="ciphertextHex">${ciphertextHex}</span></p>`;
}
break;
}
explanationBox.style.display = 'block';
}
// Function to perform Feistel round
function performFeistelRound(roundNumber) {
const rk = roundKeys[roundNumber - 1];
const prevL = L[roundNumber - 1];
const prevR = R[roundNumber - 1];
const newL = prevR;
const feistelResult = feistelFunction(prevR, rk.key);
const newR = xor(prevL, feistelResult);
L.push(newL);
R.push(newR);
explanationBox.innerHTML += `<p><strong>Round ${roundNumber}:</strong></p>`;
explanationBox.innerHTML += `<p>Round Key K${roundNumber}: <span class="binary">${rk.key.join('')}</span></p>`;
explanationBox.innerHTML += `<p>L${roundNumber}: <span class="binary">${newL.join('')}</span></p>`;
explanationBox.innerHTML += `<p>R${roundNumber}: <span class="binary">${newR.join('')}</span></p>`;
// Show Feistel function details
explanationBox.innerHTML += `<h3>Feistel Function f(R${roundNumber - 1}, K${roundNumber}):</h3>`;
// Expansion
const expandedR = permute(prevR, EXPANSION_TABLE);
explanationBox.innerHTML += `<p>Expansion E(R${roundNumber - 1}): <span class="binary">${expandedR.join('')}</span></p>`;
explanationBox.innerHTML += generatePermutationTable(EXPANSION_TABLE, 'Expansion Table');
// Key mixing
const xorResult = xor(expandedR, rk.key);
explanationBox.innerHTML += `<p>XOR with K${roundNumber}: <span class="binary">${xorResult.join('')}</span></p>`;
// S-box substitution
let sboxResultStr = '';
const sboxResult = [];
for (let i = 0; i < 8; i++) {
const block = xorResult.slice(i * 6, (i + 1) * 6);
const row = parseInt(`${block[0]}${block[5]}`, 2);
const col = parseInt(block.slice(1, 5).join(''), 2);
const sboxValue = S_BOXES[i][row][col];
const sboxBits = parseInt(sboxValue).toString(2).padStart(4, '0').split('').map(bit => parseInt(bit));
sboxResult.push(...sboxBits);
sboxResultStr += `<p>S-box ${i + 1}: Input: <span class="binary">${block.join('')}</span>, Row: ${row}, Column: ${col}, Output: <span class="binary">${sboxBits.join('')}</span></p>`;
}
explanationBox.innerHTML += `<h4>S-box Substitution Results:</h4>${sboxResultStr}`;
// Permutation
const permutedSBoxResult = permute(sboxResult, P_BOX);
explanationBox.innerHTML += `<p>After Permutation P: <span class="binary">${permutedSBoxResult.join('')}</span></p>`;
explanationBox.innerHTML += generatePermutationTable(P_BOX, 'Permutation P Table');
// Final XOR
explanationBox.innerHTML += `<p>New R${roundNumber}: L${roundNumber - 1} XOR f(R${roundNumber - 1}, K${roundNumber}): <span class="binary">${newR.join('')}</span></p>`;
}
// Generate permutation table for display
function generatePermutationTable(table, title) {
let html = `<div class="table-container"><h4>${title}</h4><table><tr>`;
for (let i = 0; i < table.length; i++) {
html += `<td>${table[i]}</td>`;
if ((i + 1) % 8 === 0 && i !== table.length - 1) {
html += `</tr><tr>`;
}
}
html += `</tr></table></div>`;
return html;
}
// Function to initialize steps
function initializeSteps() {
steps = [
{ id: 'A', title: 'Original Plaintext', content: `The plaintext message to be encrypted.<br><br>Hexadecimal: <span class="hexadecimal">${plaintextHex}</span><br>Binary: <span class="binary">${plaintextBinary.join('')}</span>` },
{ id: 'B', title: 'Initial Permutation (IP)', content: 'The plaintext bits are rearranged according to the initial permutation table to begin the encryption process.' },
{ id: 'C', title: 'Split into L0 and R0', content: 'The permuted block is split into two 32-bit halves, L0 and R0, to initiate the Feistel rounds.' },
{ id: 'D', title: 'Original Key', content: `The key used for encryption.<br><br>Hexadecimal: <span class="hexadecimal">${keyHex}</span><br>Binary: <span class="binary">${keyBinary.join('')}</span>` },
{ id: 'E', title: 'Parity Drop (PC-1)', content: 'The key is permuted using PC-1, reducing it from 64 to 56 bits by discarding parity bits.' },
{ id: 'F', title: 'Split into C0 and D0', content: 'The 56-bit key is split into two 28-bit halves for key schedule generation.' },
{ id: 'G', title: 'Key Schedule Generation', content: 'Generating 16 round keys through left circular shifts and permuted choices (PC-2).' },
{ id: 'H', title: 'Feistel Rounds Overview', content: 'An overview of the 16 Feistel rounds in the DES encryption process.' },
// Steps for each Feistel round
...Array.from({ length: 16 }, (_, i) => ({
id: `Round${i + 1}`,
title: `Feistel Round ${i + 1}`,
content: `Details of Feistel Round ${i + 1}, including expansion, key mixing, substitution, and permutation.`
})),
{ id: 'I', title: 'Swap Halves', content: 'After 16 rounds, the left and right halves are swapped before the final permutation.' },
{ id: 'J', title: 'Inverse Initial Permutation (IP⁻¹)', content: 'The final permutation is applied to obtain the ciphertext.' },
{ id: 'K', title: 'Final Ciphertext', content: `The encrypted message.<br><br>Binary: <span class="binary" id="ciphertextBinary"></span><br>Hexadecimal: <span class="hexadecimal" id="ciphertextHex"></span>` },
];
}
// Function to update navigation buttons' state
function updateNavigationButtons() {
prevButton.disabled = currentStep === 0;
nextButton.disabled = currentStep === steps.length - 1;
}
// Function to reset visualization
function resetVisualization() {
currentStep = 0;
permutedPlaintext = null;
L0 = null;
R0 = null;
permutedKey = null;
C0 = null;
D0 = null;
roundKeys = [];
finalCiphertext = null;
L = [];
R = [];
explanationBox.innerHTML = '';
explanationBox.style.display = 'none';
navigationButtons.style.display = 'none';
}
// Event listener for Start Visualization button
startButton.addEventListener('click', function() {
// Get user inputs
const userPlaintext = plaintextInput.value.trim();
const userKey = keyInput.value.trim();
// Validate inputs
if (userPlaintext && !isValidHex(userPlaintext)) {
alert('Invalid Plaintext. Please enter a valid hexadecimal string.');
return;
}
if (userKey && !isValidHex(userKey)) {
alert('Invalid Key. Please enter a valid hexadecimal string.');
return;
}
// Set plaintext and key
plaintextHex = userPlaintext || defaultPlaintextHex;
keyHex = userKey || defaultKeyHex;
// Ensure plaintext and key are 16 hexadecimal characters (64 bits)
if (plaintextHex.length !== 16) {
alert('Plaintext must be exactly 16 hexadecimal characters (64 bits).');
return;
}
if (keyHex.length !== 16) {
alert('Key must be exactly 16 hexadecimal characters (64 bits).');
return;
}
// Convert to binary arrays
plaintextBinary = hexToBinaryArray(plaintextHex);
keyBinary = hexToBinaryArray(keyHex);
// Initialize steps
initializeSteps();
// Generate diagram
generateDiagram();
// Show navigation buttons
navigationButtons.style.display = 'flex';
});
// Event listeners for Previous and Next buttons
prevButton.addEventListener('click', function() {
if (currentStep > 0) {
currentStep--;
generateDiagram();
}
});
nextButton.addEventListener('click', function() {
if (currentStep < steps.length - 1) {
currentStep++;
generateDiagram();
}
});
// Initialize visualization with default values on page load
window.onload = function() {
resetVisualization();
};
</script>
</body>
</html>