-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
738 lines (618 loc) · 28.7 KB
/
index.html
File metadata and controls
738 lines (618 loc) · 28.7 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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>🔐 Secure Documents</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
}
.container {
background: white;
padding: 40px;
border-radius: 20px;
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
max-width: 700px;
width: 100%;
position: relative;
}
.info-icon {
position: absolute;
top: 20px;
right: 20px;
width: 40px;
height: 40px;
background: #667eea;
color: white;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
font-size: 24px;
font-weight: bold;
transition: all 0.3s;
z-index: 10;
}
.info-icon:hover {
background: #5568d3;
transform: scale(1.1);
}
.main-title {
font-size: 2.5em;
color: #667eea;
text-align: center;
margin-bottom: 10px;
font-weight: bold;
}
h1 {
color: #667eea;
margin-bottom: 20px;
text-align: center;
}
h3 {
color: #667eea;
margin-bottom: 15px;
}
.mode-selector {
display: flex;
gap: 10px;
margin-bottom: 30px;
}
.mode-btn {
flex: 1;
padding: 15px;
background: #f0f0f0;
border: none;
border-radius: 10px;
font-size: 14px;
cursor: pointer;
transition: all 0.3s;
}
.mode-btn.active {
background: #667eea;
color: white;
}
.mode-btn:hover {
transform: translateY(-2px);
}
.mode {
display: none;
}
.mode.active {
display: block;
}
button {
background: #667eea;
color: white;
border: none;
padding: 15px 30px;
font-size: 16px;
border-radius: 10px;
cursor: pointer;
width: 100%;
margin-top: 15px;
transition: all 0.3s;
}
button:hover {
background: #5568d3;
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
}
textarea {
width: 100%;
padding: 15px;
border: 1px solid #ddd;
border-radius: 10px;
font-family: 'Courier New', monospace;
font-size: 12px;
resize: vertical;
min-height: 150px;
margin: 10px 0;
}
input[type="file"] {
display: block;
width: 100%;
padding: 10px;
margin: 20px 0;
border: 2px dashed #667eea;
border-radius: 10px;
cursor: pointer;
}
.key-display {
background: #f8f9fa;
border: 2px solid #dee2e6;
border-radius: 10px;
padding: 20px;
margin: 20px 0;
font-family: 'Courier New', monospace;
font-size: 12px;
word-break: break-all;
max-height: 200px;
overflow-y: auto;
}
.success {
color: #28a745;
font-size: 1.1em;
text-align: center;
margin: 20px 0;
padding: 15px;
background: #d4edda;
border-radius: 10px;
}
.info {
background: #e7f3ff;
border-left: 4px solid #2196F3;
padding: 15px;
margin: 20px 0;
border-radius: 5px;
font-size: 14px;
}
.warning {
background: #fff3cd;
border-left: 4px solid #ffc107;
padding: 15px;
margin: 20px 0;
border-radius: 5px;
}
.loading {
text-align: center;
padding: 20px;
}
.spinner {
border: 4px solid #f3f3f3;
border-top: 4px solid #667eea;
border-radius: 50%;
width: 40px;
height: 40px;
animation: spin 1s linear infinite;
margin: 20px auto;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.drop-zone {
border: 3px dashed #667eea;
border-radius: 10px;
padding: 40px;
text-align: center;
cursor: pointer;
transition: all 0.3s;
margin: 20px 0;
}
.drop-zone:hover {
background: #f8f9ff;
border-color: #5568d3;
}
.drop-zone.dragover {
background: #e7f3ff;
border-color: #2196F3;
}
.button-group {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
margin-top: 15px;
}
.button-group button {
margin-top: 0;
}
small {
color: #666;
font-size: 12px;
}
</style>
</head>
<body>
<div class="container">
<!-- Info Icon -->
<div class="info-icon" onclick="alert('🔐 Secure Document Vault\n\n1. Generate Keys: Create your encryption keys\n2. Encrypt: Encrypt files for others\n3. Decrypt: Decrypt files sent to you\n\nAll operations happen locally in your browser!')" title="Information">ℹ️</div>
<div class="main-title">🔐 Secure Vault</div>
<div style="background: #fff3cd; border-left: 4px solid #ffc107; padding: 12px; margin: 20px 0; border-radius: 5px;">
<p style="margin: 0; color: #856404; font-size: 14px;">
⚠️ <strong>Disclaimer:</strong> This tool is provided "as is" without warranty.
Not recommended for highly sensitive documents. Use at your own risk.
</p>
</div>
<!-- Mode Selector -->
<div class="mode-selector">
<button class="mode-btn active" onclick="switchMode('generate')">🔑 Generate Keys</button>
<button class="mode-btn" onclick="switchMode('encrypt')">🔒 Encrypt</button>
<button class="mode-btn" onclick="switchMode('decrypt')">🔓 Decrypt</button>
</div>
<!-- ════════════════════════════════════════════════ -->
<!-- GENERATE MODE -->
<!-- ════════════════════════════════════════════════ -->
<div id="generate-mode" class="mode active">
<h1>🔑 Generate Key Pair</h1>
<div class="info">
ℹ️ <strong>What are these keys?</strong><br>
• <strong>Private Key:</strong> Keep this SECRET! Used to decrypt files.<br>
• <strong>Public Key:</strong> Share this with people who want to send you encrypted files.
</div>
<div class="warning">
⚠️ <strong>Important:</strong> After generating, download and save both keys in a safe place! If you lose your private key, you cannot decrypt files sent to you.
</div>
<button onclick="generateKeys()">🎲 Generate New Key Pair</button>
<div id="keys-result" style="display:none; margin-top: 30px;">
<div class="success">
✅ Keys generated successfully!
</div>
<h3>📤 Public Key (Share this):</h3>
<div class="key-display" id="public-key-display"></div>
<button onclick="copyPublicKey()">📋 Copy Public Key</button>
<hr style="margin: 30px 0; border: none; border-top: 1px solid #dee2e6;">
<h3>🔒 Private Key (Keep SECRET!):</h3>
<div class="key-display" id="private-key-display"></div>
<button onclick="copyPrivateKey()">📋 Copy Private Key</button>
<hr style="margin: 30px 0; border: none; border-top: 1px solid #dee2e6;">
<div class="button-group">
<button onclick="downloadPublicKey()">💾 Download Public Key</button>
<button onclick="downloadPrivateKey()">💾 Download Private Key</button>
</div>
</div>
</div>
<!-- ════════════════════════════════════════════════ -->
<!-- ENCRYPT MODE -->
<!-- ════════════════════════════════════════════════ -->
<div id="encrypt-mode" class="mode">
<h1>🔒 Encrypt File</h1>
<div class="info">
ℹ️ Paste the recipient's <strong>public key</strong> below to encrypt a file for them.
</div>
<h3>👤 Recipient's Public Key:</h3>
<textarea id="recipient-pubkey" placeholder="Paste the public key here... -----BEGIN PUBLIC KEY----- MIICIjANBgkqhkiG9w0BAQ... -----END PUBLIC KEY-----"></textarea>
<h3>📁 Select File to Encrypt:</h3>
<input type="file" id="file-input-encrypt">
<button onclick="encryptFile()">🔒 Encrypt File</button>
<div id="encrypt-result"></div>
</div>
<!-- ════════════════════════════════════════════════ -->
<!-- DECRYPT MODE -->
<!-- ════════════════════════════════════════════════ -->
<div id="decrypt-mode" class="mode">
<h1>🔓 Decrypt File</h1>
<div class="info">
ℹ️ Paste your <strong>private key</strong> and select the encrypted file.<br>
<small>📱 iOS users: If the file is named .bin.json, that's okay!</small>
</div>
<h3>🔒 Your Private Key:</h3>
<textarea id="my-private-key" placeholder="Paste your private key here... -----BEGIN PRIVATE KEY----- MIIJQgIBADANBgkqhkiG9w... -----END PRIVATE KEY-----"></textarea>
<h3>📁 Select Encrypted File:</h3>
<input type="file" id="file-input-decrypt" accept=".bin,.encrypted,.json">
<small>Accepts: .bin, .encrypted, .json</small>
<button onclick="decryptFile()">🔓 Decrypt File</button>
<div id="decrypt-result"></div>
</div>
</div>
<script>
// ═══════════════════════════════════════════════════════════
// MODE SWITCHING
// ═══════════════════════════════════════════════════════════
function switchMode(mode) {
// Update buttons
document.querySelectorAll('.mode-btn').forEach(btn => btn.classList.remove('active'));
event.target.classList.add('active');
// Update content
document.querySelectorAll('.mode').forEach(m => m.classList.remove('active'));
document.getElementById(mode + '-mode').classList.add('active');
}
// ═══════════════════════════════════════════════════════════
// UTILITY FUNCTIONS
// ═══════════════════════════════════════════════════════════
function isIOS() {
return /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
}
function downloadFile(content, filename, mimeType = 'application/octet-stream') {
const blob = new Blob([content], { type: mimeType });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
// iOS braucht das Element im DOM
document.body.appendChild(a);
a.click();
// Cleanup
setTimeout(() => {
document.body.removeChild(a);
URL.revokeObjectURL(url);
}, 100);
}
// ═══════════════════════════════════════════════════════════
// GENERATE KEYS
// ═══════════════════════════════════════════════════════════
let generatedPublicKeyPEM = null;
let generatedPrivateKeyPEM = null;
async function generateKeys() {
const resultDiv = document.getElementById('keys-result');
resultDiv.style.display = 'block';
resultDiv.innerHTML = '<div class="loading"><div class="spinner"></div><p>Generating keys (this may take 10 seconds)...</p></div>';
try {
// Generate RSA-4096 key pair
const keyPair = await crypto.subtle.generateKey(
{
name: "RSA-OAEP",
modulusLength: 4096,
publicExponent: new Uint8Array([1, 0, 1]),
hash: "SHA-256"
},
true,
["encrypt", "decrypt"]
);
// Export public key
const publicKeyRaw = await crypto.subtle.exportKey("spki", keyPair.publicKey);
generatedPublicKeyPEM = formatAsPEM(publicKeyRaw, 'PUBLIC KEY');
// Export private key
const privateKeyRaw = await crypto.subtle.exportKey("pkcs8", keyPair.privateKey);
generatedPrivateKeyPEM = formatAsPEM(privateKeyRaw, 'PRIVATE KEY');
// Display keys
resultDiv.innerHTML = `
<div class="success">
✅ Keys generated successfully!
</div>
<h3>📤 Public Key (Share this):</h3>
<div class="key-display" id="public-key-display">${escapeHtml(generatedPublicKeyPEM)}</div>
<button onclick="copyPublicKey()">📋 Copy Public Key</button>
<hr style="margin: 30px 0; border: none; border-top: 1px solid #dee2e6;">
<h3>🔒 Private Key (Keep SECRET!):</h3>
<div class="key-display" id="private-key-display">${escapeHtml(generatedPrivateKeyPEM)}</div>
<button onclick="copyPrivateKey()">📋 Copy Private Key</button>
<hr style="margin: 30px 0; border: none; border-top: 1px solid #dee2e6;">
<div class="button-group">
<button onclick="downloadPublicKey()">💾 Download Public Key</button>
<button onclick="downloadPrivateKey()">💾 Download Private Key</button>
</div>
`;
} catch (error) {
resultDiv.innerHTML = `
<div style="color:#dc3545; padding: 20px; background: #f8d7da; border-radius: 10px;">
❌ Error: ${error.message}
</div>
`;
}
}
function copyPublicKey() {
copyToClipboard(generatedPublicKeyPEM, '✅ Public key copied!');
}
function copyPrivateKey() {
copyToClipboard(generatedPrivateKeyPEM, '✅ Private key copied!');
}
function downloadPublicKey() {
downloadFile(generatedPublicKeyPEM, 'public_key.txt', 'text/plain');
}
function downloadPrivateKey() {
downloadFile(generatedPrivateKeyPEM, 'private_key.txt', 'text/plain');
}
// ═══════════════════════════════════════════════════════════
// ENCRYPT FILE
// ═══════════════════════════════════════════════════════════
async function encryptFile() {
const file = document.getElementById('file-input-encrypt').files[0];
const recipientPubKeyPEM = document.getElementById('recipient-pubkey').value.trim();
if (!file) return alert('❌ Please select a file!');
if (!recipientPubKeyPEM) return alert('❌ Please paste the recipient\'s public key!');
const resultDiv = document.getElementById('encrypt-result');
resultDiv.innerHTML = '<div class="loading"><div class="spinner"></div><p>Encrypting...</p></div>';
try {
// Import recipient's public key
const recipientPubKey = await importPublicKeyFromPEM(recipientPubKeyPEM);
// Read file
const fileData = await file.arrayBuffer();
// Generate AES key
const aesKey = await crypto.subtle.generateKey(
{ name: "AES-GCM", length: 256 },
true,
["encrypt"]
);
// Encrypt file with AES
const nonce = crypto.getRandomValues(new Uint8Array(12));
const encryptedFile = await crypto.subtle.encrypt(
{ name: "AES-GCM", iv: nonce },
aesKey,
fileData
);
// Encrypt AES key with RSA
const aesKeyRaw = await crypto.subtle.exportKey("raw", aesKey);
const encryptedAESKey = await crypto.subtle.encrypt(
{ name: "RSA-OAEP" },
recipientPubKey,
aesKeyRaw
);
// Create package
const package = {
encryptedAESKey: arrayBufferToBase64(encryptedAESKey),
nonce: arrayBufferToBase64(nonce),
encryptedFile: arrayBufferToBase64(encryptedFile),
filename: file.name,
timestamp: new Date().toISOString()
};
// Download encrypted file (iOS FIX: use octet-stream!)
const packageJSON = JSON.stringify(package);
const filename = file.name.replace(/\.[^/.]+$/, "") + '_encrypted.bin';
downloadFile(packageJSON, filename, 'application/octet-stream');
const iosNote = isIOS() ? '<br><small>📱 Check your Downloads folder or Files app.</small>' : '';
resultDiv.innerHTML = `
<div class="success">
✅ File encrypted successfully!<br>
📥 <strong>${filename}</strong> has been downloaded.${iosNote}
</div>
<div class="info">
📤 Send this file to the recipient. They can decrypt it with their private key.
</div>
`;
} catch (error) {
resultDiv.innerHTML = `
<div style="color:#dc3545; padding: 20px; background: #f8d7da; border-radius: 10px;">
❌ <strong>Encryption failed!</strong><br>${error.message}
</div>
`;
}
}
// ═══════════════════════════════════════════════════════════
// DECRYPT FILE
// ═══════════════════════════════════════════════════════════
async function decryptFile() {
const file = document.getElementById('file-input-decrypt').files[0];
const privateKeyPEM = document.getElementById('my-private-key').value.trim();
if (!file) return alert('❌ Please select an encrypted file!');
if (!privateKeyPEM) return alert('❌ Please paste your private key!');
const resultDiv = document.getElementById('decrypt-result');
resultDiv.innerHTML = `<div class="loading"><div class="spinner"></div><p>Decrypting ${file.name}...</p></div>`;
try {
// Import private key
const privateKey = await importPrivateKeyFromPEM(privateKeyPEM);
// Read encrypted file
const fileData = await file.arrayBuffer();
const decoder = new TextDecoder();
const packageJSON = decoder.decode(fileData);
const package = JSON.parse(packageJSON);
// Decrypt AES key with RSA
const encryptedAESKey = base64ToArrayBuffer(package.encryptedAESKey);
const aesKeyBytes = await crypto.subtle.decrypt(
{ name: "RSA-OAEP" },
privateKey,
encryptedAESKey
);
// Import AES key
const aesKey = await crypto.subtle.importKey(
"raw",
aesKeyBytes,
{ name: "AES-GCM" },
false,
["decrypt"]
);
// Decrypt file with AES
const nonce = base64ToArrayBuffer(package.nonce);
const encryptedFile = base64ToArrayBuffer(package.encryptedFile);
const decryptedFile = await crypto.subtle.decrypt(
{ name: "AES-GCM", iv: nonce },
aesKey,
encryptedFile
);
// Download decrypted file
const decryptedFilename = package.filename || 'decrypted_file';
downloadFile(decryptedFile, decryptedFilename, 'application/octet-stream');
const iosNote = isIOS() ? '<br><small>📱 Check your Downloads folder or Files app.</small>' : '';
resultDiv.innerHTML = `
<div class="success">
✅ File decrypted successfully!<br>
📥 <strong>${decryptedFilename}</strong> has been downloaded.${iosNote}
</div>
`;
} catch (error) {
resultDiv.innerHTML = `
<div style="color:#dc3545; padding: 20px; background: #f8d7da; border-radius: 10px;">
❌ <strong>Decryption failed!</strong><br>
${error.message}<br><br>
Make sure you're using the correct private key and the file is not corrupted.
</div>
`;
}
}
// ═══════════════════════════════════════════════════════════
// CRYPTO HELPER FUNCTIONS
// ═══════════════════════════════════════════════════════════
async function importPublicKeyFromPEM(pem) {
const pemContents = pem
.replace(/-----BEGIN PUBLIC KEY-----/, '')
.replace(/-----END PUBLIC KEY-----/, '')
.replace(/\s/g, '');
const binaryDer = atob(pemContents);
const bytes = new Uint8Array(binaryDer.length);
for (let i = 0; i < binaryDer.length; i++) {
bytes[i] = binaryDer.charCodeAt(i);
}
return await crypto.subtle.importKey(
"spki",
bytes.buffer,
{
name: "RSA-OAEP",
hash: "SHA-256"
},
true,
["encrypt"]
);
}
async function importPrivateKeyFromPEM(pem) {
const pemContents = pem
.replace(/-----BEGIN PRIVATE KEY-----/, '')
.replace(/-----END PRIVATE KEY-----/, '')
.replace(/\s/g, '');
const binaryDer = atob(pemContents);
const bytes = new Uint8Array(binaryDer.length);
for (let i = 0; i < binaryDer.length; i++) {
bytes[i] = binaryDer.charCodeAt(i);
}
return await crypto.subtle.importKey(
"pkcs8",
bytes.buffer,
{
name: "RSA-OAEP",
hash: "SHA-256"
},
true,
["decrypt"]
);
}
function formatAsPEM(buffer, label) {
const base64 = arrayBufferToBase64(buffer);
const lines = base64.match(/.{1,64}/g) || [];
return `-----BEGIN ${label}-----\n${lines.join('\n')}\n-----END ${label}-----`;
}
function arrayBufferToBase64(buffer) {
const bytes = new Uint8Array(buffer);
let binary = '';
for (let i = 0; i < bytes.byteLength; i++) {
binary += String.fromCharCode(bytes[i]);
}
return btoa(binary);
}
function base64ToArrayBuffer(base64) {
const binary = atob(base64);
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i++) {
bytes[i] = binary.charCodeAt(i);
}
return bytes.buffer;
}
function copyToClipboard(text, message) {
navigator.clipboard.writeText(text).then(() => {
alert(message);
}).catch(err => {
// Fallback für ältere Browser
const textArea = document.createElement('textarea');
textArea.value = text;
textArea.style.position = 'fixed';
textArea.style.left = '-999999px';
document.body.appendChild(textArea);
textArea.select();
try {
document.execCommand('copy');
alert(message);
} catch (e) {
alert('❌ Could not copy. Please copy manually.');
}
document.body.removeChild(textArea);
});
}
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
</script>
</body>
</html>