-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrsa.html
More file actions
680 lines (587 loc) · 20.1 KB
/
rsa.html
File metadata and controls
680 lines (587 loc) · 20.1 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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<title>RSA Private Key Parser</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: linear-gradient(135deg, #0f0f23 0%, #1a1a3e 100%);
min-height: 100vh;
padding: 20px;
color: #fff;
}
.container {
max-width: 600px;
margin: 0 auto;
}
h1 {
text-align: center;
margin-bottom: 10px;
font-size: 1.8rem;
background: linear-gradient(90deg, #f59e0b, #ef4444);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
}
.subtitle {
text-align: center;
color: #6b7280;
margin-bottom: 30px;
font-size: 14px;
}
.card {
background: rgba(255, 255, 255, 0.05);
border-radius: 16px;
padding: 24px;
margin-bottom: 20px;
backdrop-filter: blur(10px);
border: 1px solid rgba(255, 255, 255, 0.1);
}
.card-title {
font-size: 1.1rem;
margin-bottom: 15px;
color: #f59e0b;
display: flex;
align-items: center;
gap: 8px;
}
label {
display: block;
margin-bottom: 10px;
font-weight: 500;
color: #a0aec0;
}
textarea {
width: 100%;
min-height: 60px;
padding: 16px;
border: 2px solid rgba(255, 255, 255, 0.1);
border-radius: 12px;
background: rgba(0, 0, 0, 0.4);
color: #10b981;
font-size: 13px;
font-family: 'Consolas', 'Monaco', monospace;
resize: none;
overflow: hidden;
transition: border-color 0.3s;
}
textarea:focus {
outline: none;
border-color: #f59e0b;
}
textarea::placeholder {
color: #4a5568;
}
.button-group {
display: flex;
gap: 12px;
margin: 20px 0;
flex-wrap: wrap;
}
button {
padding: 14px 28px;
border: none;
border-radius: 10px;
font-size: 15px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s;
}
.btn-parse {
background: linear-gradient(135deg, #f59e0b, #d97706);
color: #000;
flex: 1;
min-width: 150px;
}
.btn-sample {
background: rgba(255, 255, 255, 0.1);
color: #fff;
border: 1px solid rgba(255, 255, 255, 0.2);
}
.btn-clear {
background: rgba(239, 68, 68, 0.2);
color: #f87171;
border: 1px solid rgba(239, 68, 68, 0.3);
}
button:hover {
transform: translateY(-2px);
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
}
button:active {
transform: translateY(0);
}
.result-section {
display: none;
}
.result-section.show {
display: block;
}
.info-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 15px;
margin-bottom: 20px;
}
.info-item {
background: rgba(0, 0, 0, 0.3);
padding: 15px;
border-radius: 10px;
border-left: 3px solid #f59e0b;
}
.info-label {
font-size: 12px;
color: #6b7280;
margin-bottom: 5px;
}
.info-value {
font-size: 16px;
font-weight: 600;
color: #fff;
}
.component {
background: rgba(0, 0, 0, 0.3);
border-radius: 12px;
margin-bottom: 15px;
overflow: hidden;
}
.component-header {
padding: 15px 20px;
background: rgba(255, 255, 255, 0.05);
display: flex;
justify-content: space-between;
align-items: center;
cursor: pointer;
transition: background 0.3s;
}
.component-header:hover {
background: rgba(255, 255, 255, 0.1);
}
.component-name {
font-weight: 600;
color: #f59e0b;
}
.component-meta {
font-size: 12px;
color: #6b7280;
}
.component-body {
padding: 15px 20px;
display: none;
}
.component.open .component-body {
display: block;
}
.component-value {
font-family: 'Consolas', 'Monaco', monospace;
font-size: 12px;
color: #10b981;
word-break: break-all;
line-height: 1.6;
background: rgba(0, 0, 0, 0.3);
padding: 12px;
border-radius: 8px;
max-height: 200px;
overflow-y: auto;
}
.copy-btn {
padding: 6px 12px;
font-size: 12px;
background: rgba(255, 255, 255, 0.1);
color: #fff;
border-radius: 6px;
margin-top: 10px;
}
.message {
position: fixed;
top: 20px;
right: 20px;
padding: 12px 16px;
border-radius: 8px;
font-size: 14px;
display: none;
z-index: 1000;
}
.message.success {
display: block;
background: rgba(16, 185, 129, 0.2);
color: #34d399;
border: 1px solid rgba(16, 185, 129, 0.3);
}
.message.error {
display: block;
background: rgba(239, 68, 68, 0.2);
color: #f87171;
border: 1px solid rgba(239, 68, 68, 0.3);
}
.toggle-icon {
transition: transform 0.3s;
}
.component.open .toggle-icon {
transform: rotate(180deg);
}
.warning {
background: rgba(245, 158, 11, 0.1);
border: 1px solid rgba(245, 158, 11, 0.3);
border-radius: 10px;
padding: 15px;
margin-bottom: 20px;
font-size: 13px;
color: #fbbf24;
}
.warning-icon {
margin-right: 8px;
}
.back-link {
display: inline-block;
margin-bottom: 20px;
color: #a0aec0;
text-decoration: none;
font-size: 14px;
transition: color 0.2s;
}
.back-link:hover {
color: #f59e0b;
}
@media (max-width: 480px) {
h1 {
font-size: 1.4rem;
}
.card {
padding: 16px;
}
textarea {
font-size: 11px;
}
button {
padding: 12px 16px;
font-size: 14px;
}
.info-grid {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<div class="container">
<a class="back-link" href="index.html">← Back to Tools</a>
<h1>RSA Private Key Parser</h1>
<p class="subtitle">Parse PEM format RSA private keys and view components</p>
<div class="warning">
<span class="warning-icon">⚠️</span>
<strong>Security Notice:</strong> This tool runs entirely in your browser. No data is uploaded. However, please
protect your private keys and avoid using real keys in untrusted environments.
</div>
<div class="card">
<div class="card-title">Input RSA Private Key (PEM format)</div>
<textarea id="input" placeholder="-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA...
-----END RSA PRIVATE KEY-----
or
-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASC...
-----END PRIVATE KEY-----"></textarea>
<div class="button-group">
<button class="btn-parse" onclick="parseKey()">Parse Key</button>
<button class="btn-sample" onclick="loadSample()">Load Sample</button>
<button class="btn-clear" onclick="clearAll()">Clear</button>
</div>
</div>
<div class="message" id="message"></div>
<div class="result-section" id="result">
<div class="card">
<div class="card-title">Key Information</div>
<div class="info-grid">
<div class="info-item">
<div class="info-label">Key Type</div>
<div class="info-value" id="keyType">-</div>
</div>
<div class="info-item">
<div class="info-label">Key Size</div>
<div class="info-value" id="keySize">-</div>
</div>
<div class="info-item">
<div class="info-label">Format</div>
<div class="info-value" id="keyFormat">-</div>
</div>
<div class="info-item">
<div class="info-label">Version</div>
<div class="info-value" id="keyVersion">-</div>
</div>
</div>
</div>
<div class="card">
<div class="card-title">Key Components</div>
<div id="components"></div>
</div>
</div>
</div>
<script>
const inputEl = document.getElementById('input');
const messageEl = document.getElementById('message');
const resultEl = document.getElementById('result');
// Auto-resize textarea
function autoResize() {
inputEl.style.height = 'auto';
inputEl.style.height = inputEl.scrollHeight + 'px';
}
inputEl.addEventListener('input', autoResize);
// Initial resize
autoResize();
function showMessage(text, type) {
messageEl.textContent = text;
messageEl.className = 'message ' + type;
setTimeout(() => {
messageEl.className = 'message';
}, 5000);
}
// Base64 decode
function base64Decode(str) {
const binary = atob(str);
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i++) {
bytes[i] = binary.charCodeAt(i);
}
return bytes;
}
// ASN.1 DER Parser
class ASN1Parser {
constructor(data) {
this.data = data;
this.pos = 0;
}
readByte() {
return this.data[this.pos++];
}
readLength() {
let length = this.readByte();
if (length & 0x80) {
const numBytes = length & 0x7f;
length = 0;
for (let i = 0; i < numBytes; i++) {
length = (length << 8) | this.readByte();
}
}
return length;
}
readTag() {
return this.readByte();
}
readInteger() {
const tag = this.readTag();
if (tag !== 0x02) {
throw new Error('Expected INTEGER tag');
}
const length = this.readLength();
const value = this.data.slice(this.pos, this.pos + length);
this.pos += length;
return value;
}
readSequence() {
const tag = this.readTag();
if (tag !== 0x30) {
throw new Error('Expected SEQUENCE tag');
}
const length = this.readLength();
return length;
}
readOID() {
const tag = this.readTag();
if (tag !== 0x06) {
throw new Error('Expected OID tag');
}
const length = this.readLength();
const value = this.data.slice(this.pos, this.pos + length);
this.pos += length;
return value;
}
readOctetString() {
const tag = this.readTag();
if (tag !== 0x04) {
throw new Error('Expected OCTET STRING tag');
}
const length = this.readLength();
const value = this.data.slice(this.pos, this.pos + length);
this.pos += length;
return value;
}
skip(length) {
this.pos += length;
}
}
// Convert bytes to hex string
function bytesToHex(bytes) {
return Array.from(bytes).map(b => b.toString(16).padStart(2, '0')).join('');
}
// Calculate bit length of a big integer
function getBitLength(bytes) {
let i = 0;
// Skip leading zero bytes
while (i < bytes.length && bytes[i] === 0) i++;
if (i >= bytes.length) return 0;
const firstByte = bytes[i];
// Safety check for zero byte (should not happen after while loop, but defensive)
if (firstByte === 0) return 0;
const remainingBytes = bytes.length - i - 1;
// Calculate bits in the first non-zero byte
const firstByteBits = 32 - Math.clz32(firstByte);
return remainingBytes * 8 + firstByteBits;
}
// Parse RSA private key
function parseRSAKey(pemData) {
let format = 'PKCS#1';
let base64Data = '';
// Check format
if (pemData.includes('-----BEGIN RSA PRIVATE KEY-----')) {
format = 'PKCS#1';
base64Data = pemData
.replace('-----BEGIN RSA PRIVATE KEY-----', '')
.replace('-----END RSA PRIVATE KEY-----', '')
.replace(/\s/g, '');
} else if (pemData.includes('-----BEGIN PRIVATE KEY-----')) {
format = 'PKCS#8';
base64Data = pemData
.replace('-----BEGIN PRIVATE KEY-----', '')
.replace('-----END PRIVATE KEY-----', '')
.replace(/\s/g, '');
} else {
throw new Error('Unsupported key format. Please use PEM format RSA private key');
}
const derData = base64Decode(base64Data);
const parser = new ASN1Parser(derData);
let components = {};
if (format === 'PKCS#8') {
// PKCS#8 format
parser.readSequence(); // outer sequence
parser.readInteger(); // version
parser.readSequence(); // algorithm identifier sequence
parser.readOID(); // algorithm OID
// Skip NULL if present
if (parser.data[parser.pos] === 0x05) {
parser.pos += 2;
}
const privateKeyData = parser.readOctetString();
const innerParser = new ASN1Parser(privateKeyData);
innerParser.readSequence();
components.version = innerParser.readInteger();
components.modulus = innerParser.readInteger();
components.publicExponent = innerParser.readInteger();
components.privateExponent = innerParser.readInteger();
components.prime1 = innerParser.readInteger();
components.prime2 = innerParser.readInteger();
components.exponent1 = innerParser.readInteger();
components.exponent2 = innerParser.readInteger();
components.coefficient = innerParser.readInteger();
} else {
// PKCS#1 format
parser.readSequence();
components.version = parser.readInteger();
components.modulus = parser.readInteger();
components.publicExponent = parser.readInteger();
components.privateExponent = parser.readInteger();
components.prime1 = parser.readInteger();
components.prime2 = parser.readInteger();
components.exponent1 = parser.readInteger();
components.exponent2 = parser.readInteger();
components.coefficient = parser.readInteger();
}
return {
format,
components,
keySize: getBitLength(components.modulus)
};
}
function parseKey() {
const pemData = inputEl.value.trim();
if (!pemData) {
showMessage('Please enter RSA private key', 'error');
return;
}
try {
const result = parseRSAKey(pemData);
displayResult(result);
showMessage('Parsed successfully', 'success');
} catch (e) {
showMessage('Parse failed: ' + e.message, 'error');
resultEl.classList.remove('show');
}
}
function displayResult(result) {
resultEl.classList.add('show');
document.getElementById('keyType').textContent = 'RSA';
document.getElementById('keySize').textContent = result.keySize + ' bits';
document.getElementById('keyFormat').textContent = result.format;
document.getElementById('keyVersion').textContent = 'v' + (parseInt(bytesToHex(result.components.version), 16) + 1);
const componentNames = {
modulus: {name: 'Modulus (n)', desc: 'n = p × q'},
publicExponent: {name: 'Public Exponent (e)', desc: 'public exponent'},
privateExponent: {name: 'Private Exponent (d)', desc: 'private exponent'},
prime1: {name: 'Prime 1 (p)', desc: 'first prime'},
prime2: {name: 'Prime 2 (q)', desc: 'second prime'},
exponent1: {name: 'Exponent 1 (dp)', desc: 'd mod (p-1)'},
exponent2: {name: 'Exponent 2 (dq)', desc: 'd mod (q-1)'},
coefficient: {name: 'Coefficient (qInv)', desc: 'q⁻¹ mod p'}
};
const componentsEl = document.getElementById('components');
componentsEl.innerHTML = '';
for (const [key, value] of Object.entries(result.components)) {
if (key === 'version') continue;
const info = componentNames[key];
const hex = bytesToHex(value);
const bitLen = getBitLength(value);
const div = document.createElement('div');
div.className = 'component';
div.innerHTML = `
<div class="component-header" onclick="this.parentElement.classList.toggle('open')">
<div>
<span class="component-name">${info.name}</span>
<span class="component-meta"> - ${info.desc}</span>
</div>
<div>
<span class="component-meta">${bitLen} bits</span>
<span class="toggle-icon">▼</span>
</div>
</div>
<div class="component-body">
<div class="component-value" id="value-${key}">${hex}</div>
<button class="copy-btn" onclick="copyValue('${key}')">Copy</button>
</div>
`;
componentsEl.appendChild(div);
}
}
function copyValue(key) {
const el = document.getElementById('value-' + key);
navigator.clipboard.writeText(el.textContent).then(() => {
showMessage('Copied to clipboard', 'success');
});
}
function loadSample() {
// This is a sample 512-bit RSA key for demonstration only
inputEl.value = `-----BEGIN RSA PRIVATE KEY-----
MIIBOgIBAAJBALRiMLAHudeSA2ai2l5M0u/7xlm1OT5qq3VWzKS+0+MKk/xe6xvD
t2oIv1HxZH5GJn8zPHgLzwbVRaMMPqxWlKUCAwEAAQJASxoz3x0dM5UhUvLDp7wu
VCdHLnBbQxPokHriGeoAPlAP/5hu8DLZ1ISh1GlPwvMOoL5SVm+I2fFdLz8DdBsr
QQIhAN2V2GIxFg8IWB7kGxjWPBNi3/RQfvvWBR3Lxr5STgCZAiEAz9gMhuJDnQl8
J+N8bsJO8sLO0NoNKCxmDAzqEyKzVfUCIFCL2rRcLXEcs+x2RjBM/n+FkkDlpPDz
Iw5/eVaXnN4JAiEAh4BrYmXbxJVfMdWPaYPr6PL3evPRDNDF5RkOYptBeS0CIBC4
J/5J64TQnykq8E5kZGk5SrVtQqm0BfR7L10Gh4EK
-----END RSA PRIVATE KEY-----`;
autoResize();
}
function clearAll() {
inputEl.value = '';
resultEl.classList.remove('show');
autoResize();
showMessage('Cleared', 'success');
}
</script>
</body>
</html>