-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrsa.html
More file actions
650 lines (548 loc) · 21.4 KB
/
rsa.html
File metadata and controls
650 lines (548 loc) · 21.4 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
649
650
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>RSA Algorithm 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"], input[type="number"] {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
font-size: 16px;
}
.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;
}
.key-generation {
margin-bottom: 30px;
padding: 20px;
background-color: #f8f9fa;
border-radius: 5px;
}
.key-generation h2 {
margin-bottom: 15px;
color: #2c3e50;
}
.key-pair {
display: flex;
flex-wrap: wrap;
gap: 20px;
margin-top: 20px;
}
.key-box {
flex: 1;
min-width: 250px;
padding: 15px;
border: 1px solid #ddd;
border-radius: 5px;
background-color: white;
}
.key-title {
font-weight: bold;
margin-bottom: 10px;
color: #3498db;
}
.key-value {
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%;
}
.formula {
font-family: monospace;
background-color: #f0f0f0;
padding: 10px;
border-radius: 5px;
margin: 10px 0;
text-align: center;
}
.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;
}
@media (max-width: 768px) {
.container {
padding: 15px;
}
.key-pair {
flex-direction: column;
}
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<button class="back-btn" onclick="window.location.href='index.html'">Back</button>
<h1>RSA Algorithm 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="p">Prime Number p:</label>
<input type="number" id="p" placeholder="Enter a prime number" value="17">
<div class="hint">Enter a prime number (for demonstration purposes, use small primes)</div>
</div>
<div class="input-group">
<label for="q">Prime Number q:</label>
<input type="number" id="q" placeholder="Enter a prime number" value="11">
<div class="hint">Enter a prime number different from p</div>
</div>
<div class="input-group">
<label for="e">Public Exponent e:</label>
<input type="number" id="e" placeholder="Enter a value for e" value="7">
<div class="hint">Must be coprime to (p-1)(q-1)</div>
</div>
<div class="input-group">
<label for="input-text">
<span id="input-label">Message</span> (Numeric):
</label>
<input type="number" id="input-text" placeholder="Enter a number to encrypt" value="88">
<div class="hint">Enter a number less than n = p*q (187 for default values)</div>
</div>
<button id="process-btn">Process</button>
</div>
<div class="visualization">
<div class="key-generation">
<h2>Key Generation</h2>
<div id="key-generation-steps"></div>
<div class="key-pair">
<div class="key-box">
<div class="key-title">Public Key</div>
<div class="key-value" id="public-key"></div>
</div>
<div class="key-box">
<div class="key-title">Private Key</div>
<div class="key-value" id="private-key"></div>
</div>
</div>
</div>
<div class="animation-container" id="animation-container">
<div class="animation-title">RSA 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:
</div>
<div class="result" id="result"></div>
</div>
</div>
<div class="how-it-works">
<h2>How RSA Works:</h2>
<p>RSA (Rivest–Shamir–Adleman) is a public-key cryptosystem that is widely used for secure data transmission. It is based on the practical difficulty of factoring the product of two large prime numbers.</p>
<ol>
<li><strong>Key Generation:</strong>
<ol>
<li>Choose two distinct prime numbers p and q.</li>
<li>Compute n = p × q. This is the modulus for both the public and private keys.</li>
<li>Compute the totient: φ(n) = (p - 1) × (q - 1).</li>
<li>Choose an integer e such that 1 < e < φ(n) and gcd(e, φ(n)) = 1 (e is coprime to φ(n)).</li>
<li>Compute d such that d × e ≡ 1 (mod φ(n)). This is the modular multiplicative inverse of e modulo φ(n).</li>
</ol>
</li>
<li><strong>Public Key:</strong> (n, e)</li>
<li><strong>Private Key:</strong> (n, d)</li>
<li><strong>Encryption:</strong> c = m<sup>e</sup> mod n, where m is the message and c is the ciphertext.</li>
<li><strong>Decryption:</strong> m = c<sup>d</sup> mod n, where c is the ciphertext and m is the original message.</li>
</ol>
<p>The security of RSA relies on the fact that finding the prime factors of a large composite number is computationally intensive. If an attacker can factor n into p and q, they can compute φ(n) and then d, breaking the encryption.</p>
<p>In practice, RSA keys are typically 1024 to 4096 bits long, making factorization infeasible with current technology. However, for this demonstration, we use small numbers to make the calculations easier to follow.</p>
</div>
</div>
<script>
// DOM Elements
const pInput = document.getElementById('p');
const qInput = document.getElementById('q');
const eInput = document.getElementById('e');
const inputText = document.getElementById('input-text');
const processBtn = document.getElementById('process-btn');
const publicKeyEl = document.getElementById('public-key');
const privateKeyEl = document.getElementById('private-key');
const keyGenerationStepsEl = document.getElementById('key-generation-steps');
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', () => {
processRSA();
});
// 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 = 'Message';
resultLabel.textContent = 'Encrypted';
} else {
inputLabel.textContent = 'Ciphertext';
resultLabel.textContent = 'Decrypted';
}
processRSA();
});
});
processBtn.addEventListener('click', processRSA);
prevStepBtn.addEventListener('click', () => {
if (currentAnimationStep > 0) {
showAnimationStep(--currentAnimationStep);
}
});
nextStepBtn.addEventListener('click', () => {
if (currentAnimationStep < totalAnimationSteps - 1) {
showAnimationStep(++currentAnimationStep);
}
});
// RSA Functions
function processRSA() {
const p = parseInt(pInput.value);
const q = parseInt(qInput.value);
const e = parseInt(eInput.value);
const message = parseInt(inputText.value);
if (!isPrime(p) || !isPrime(q)) {
alert("Both p and q must be prime numbers");
return;
}
const n = p * q;
const phi = (p - 1) * (q - 1);
if (e <= 1 || e >= phi || gcd(e, phi) !== 1) {
alert(`e must be between 1 and ${phi} and coprime to ${phi}`);
return;
}
if (message >= n) {
alert(`Message must be less than n (${n})`);
return;
}
const d = modInverse(e, phi);
// Display key generation steps
keyGenerationStepsEl.innerHTML = `
<p>1. Choose two prime numbers: p = ${p}, q = ${q}</p>
<p>2. Compute n = p × q = ${p} × ${q} = ${n}</p>
<p>3. Compute φ(n) = (p - 1) × (q - 1) = ${p - 1} × ${q - 1} = ${phi}</p>
<p>4. Choose e = ${e} (coprime to ${phi})</p>
<p>5. Compute d such that d × e ≡ 1 (mod φ(n)): d = ${d}</p>
`;
// Display keys
publicKeyEl.innerHTML = `(n, e) = (${n}, ${e})`;
privateKeyEl.innerHTML = `(n, d) = (${n}, ${d})`;
// Create animation steps
const steps = [];
if (mode === 'encrypt') {
// Encryption: c = m^e mod n
const ciphertext = modPow(message, e, n);
steps.push({
type: 'encrypt-formula',
description: 'Encryption Formula',
content: `<p>To encrypt a message (m) using the public key (n, e), we use the formula:</p>
<div class="formula">c = m<sup>e</sup> mod n</div>`
});
steps.push({
type: 'encrypt-calculation',
description: 'Encryption Calculation',
content: `<p>Substituting our values:</p>
<div class="formula">c = ${message}<sup>${e}</sup> mod ${n}</div>
<p>This equals:</p>
<div class="formula">c = ${ciphertext}</div>`
});
displayResult(ciphertext);
} else {
// Decryption: m = c^d mod n
const plaintext = modPow(message, d, n);
steps.push({
type: 'decrypt-formula',
description: 'Decryption Formula',
content: `<p>To decrypt a ciphertext (c) using the private key (n, d), we use the formula:</p>
<div class="formula">m = c<sup>d</sup> mod n</div>`
});
steps.push({
type: 'decrypt-calculation',
description: 'Decryption Calculation',
content: `<p>Substituting our values:</p>
<div class="formula">m = ${message}<sup>${d}</sup> mod ${n}</div>
<p>This equals:</p>
<div class="formula">m = ${plaintext}</div>`
});
displayResult(plaintext);
}
// Add final step
steps.push({
type: 'complete',
description: `${mode === 'encrypt' ? 'Encryption' : 'Decryption'} Complete`,
content: `<p>The ${mode === 'encrypt' ? 'encrypted message (ciphertext)' : 'decrypted message (plaintext)'} is:</p>
<div class="formula">${resultEl.textContent}</div>`
});
createAnimation(steps);
// Reset animation
currentAnimationStep = 0;
totalAnimationSteps = steps.length;
showAnimationStep(currentAnimationStep);
}
// Helper Functions
function isPrime(num) {
if (num <= 1) return false;
if (num <= 3) return true;
if (num % 2 === 0 || num % 3 === 0) return false;
for (let i = 5; i * i <= num; i += 6) {
if (num % i === 0 || num % (i + 2) === 0) return false;
}
return true;
}
function gcd(a, b) {
while (b !== 0) {
const temp = b;
b = a % b;
a = temp;
}
return a;
}
function modInverse(a, m) {
// Find modular multiplicative inverse of a under modulo m
a = ((a % m) + m) % m; // Ensure positive value
for (let x = 1; x < m; x++) {
if ((a * x) % m === 1) {
return x;
}
}
return 1; // Default, should not reach here if inputs are valid
}
function modPow(base, exponent, modulus) {
// Compute (base^exponent) % modulus efficiently
if (modulus === 1) return 0;
let result = 1;
base = base % modulus;
while (exponent > 0) {
if (exponent % 2 === 1) {
result = (result * base) % modulus;
}
exponent = Math.floor(exponent / 2);
base = (base * base) % modulus;
}
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;
const content = document.createElement('div');
content.innerHTML = step.content;
stepEl.appendChild(title);
stepEl.appendChild(content);
animationStepsEl.appendChild(stepEl);
});
}
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>