|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 6 | + <title>Image Encryptor & Decryptor</title> |
| 7 | + <style> |
| 8 | + :root { --purple: #9c27b0; --dark-bg: #121212; --dark-card: #1e1e1e; --text-light: #e0e0e0; } |
| 9 | + body { font-family: 'Segoe UI', sans-serif; background-color: var(--dark-bg); color: var(--text-light); padding: 20px; display: flex; flex-direction: column; align-items: center; } |
| 10 | + .container { background-color: var(--dark-card); padding: 30px; border-radius: 10px; width: 100%; max-width: 600px; box-shadow: 0 4px 20px rgba(0,0,0,0.3); } |
| 11 | + h1 { color: var(--purple); text-align: center; } |
| 12 | + .preview-area { margin: 20px 0; text-align: center; } |
| 13 | + #imagePreview { max-width: 100%; max-height: 300px; border: 2px dashed var(--purple); border-radius: 8px; display: none; margin: 0 auto; } |
| 14 | + .placeholder { width: 100%; height: 200px; background: rgba(156,39,176,0.1); border: 2px dashed var(--purple); display: flex; justify-content: center; align-items: center; } |
| 15 | + input, button { padding: 12px; margin-bottom: 15px; border-radius: 5px; border: none; width: 100%; box-sizing: border-box; } |
| 16 | + input { background: #2a2a2a; color: white; } |
| 17 | + button { background: var(--purple); color: white; cursor: pointer; font-weight: bold; } |
| 18 | + button:hover { background: #7b1fa2; } |
| 19 | + .status { padding: 10px; border-radius: 5px; display: none; text-align: center; margin-top: 10px; } |
| 20 | + .success { background: rgba(76,175,80,0.2); color: #4CAF50; } |
| 21 | + .error { background: rgba(244,67,54,0.2); color: #F44336; } |
| 22 | + .tab-buttons { display: flex; gap: 10px; margin-bottom: 20px; } |
| 23 | + .tab-btn { background: #333; flex: 1; color: white; border-radius: 5px; border: none; cursor: pointer; padding: 10px; } |
| 24 | + .tab-btn.active { background: var(--purple); } |
| 25 | + </style> |
| 26 | +</head> |
| 27 | +<body> |
| 28 | + |
| 29 | +<div class="container"> |
| 30 | + <h1>Secure Image Tool</h1> |
| 31 | + <div class="tab-buttons"> |
| 32 | + <button class="tab-btn active" id="tabEnc">Encrypt</button> |
| 33 | + <button class="tab-btn" id="tabDec">Decrypt</button> |
| 34 | + </div> |
| 35 | + |
| 36 | + <div id="encryptSection"> |
| 37 | + <input type="file" id="imageUpload" accept="image/*"> |
| 38 | + <div class="preview-area"> |
| 39 | + <div class="placeholder" id="placeholder">Image Preview</div> |
| 40 | + <img id="imagePreview"> |
| 41 | + </div> |
| 42 | + <input type="password" id="encPassword" placeholder="Set Encryption Password"> |
| 43 | + <button id="encryptBtn">Encrypt & Download (.txt)</button> |
| 44 | + </div> |
| 45 | + |
| 46 | + <div id="decryptSection" style="display:none;"> |
| 47 | + <input type="file" id="fileUpload" accept=".txt"> |
| 48 | + <input type="password" id="decPassword" placeholder="Enter Password to Decrypt"> |
| 49 | + <button id="decryptBtn">Decrypt & View Image</button> |
| 50 | + </div> |
| 51 | + |
| 52 | + <div class="status" id="status"></div> |
| 53 | +</div> |
| 54 | + |
| 55 | +<script> |
| 56 | + const statusDiv = document.getElementById('status'); |
| 57 | + const imagePreview = document.getElementById('imagePreview'); |
| 58 | + const placeholder = document.getElementById('placeholder'); |
| 59 | + |
| 60 | + function switchTab(tab) { |
| 61 | + document.getElementById('encryptSection').style.display = tab === 'encrypt' ? 'block' : 'none'; |
| 62 | + document.getElementById('decryptSection').style.display = tab === 'decrypt' ? 'block' : 'none'; |
| 63 | + document.getElementById('tabEnc').classList.toggle('active', tab === 'encrypt'); |
| 64 | + document.getElementById('tabDec').classList.toggle('active', tab === 'decrypt'); |
| 65 | + statusDiv.style.display = 'none'; |
| 66 | + } |
| 67 | + |
| 68 | + document.getElementById('tabEnc').onclick = () => switchTab('encrypt'); |
| 69 | + document.getElementById('tabDec').onclick = () => switchTab('decrypt'); |
| 70 | + |
| 71 | + function showStatus(msg, type) { |
| 72 | + statusDiv.innerText = msg; |
| 73 | + statusDiv.className = 'status ' + type; |
| 74 | + statusDiv.style.display = 'block'; |
| 75 | + } |
| 76 | + |
| 77 | + // Stack-safe conversion using native browser APIs |
| 78 | + async function bufferToBase64(buffer) { |
| 79 | + const blob = new Blob([buffer]); |
| 80 | + return new Promise((resolve, reject) => { |
| 81 | + const reader = new FileReader(); |
| 82 | + reader.onload = () => resolve(reader.result.split(',')[1]); |
| 83 | + reader.onerror = reject; |
| 84 | + reader.readAsDataURL(blob); |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + function base64ToBuffer(b64) { |
| 89 | + const bin = atob(b64); |
| 90 | + const bytes = new Uint8Array(bin.length); |
| 91 | + for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i); |
| 92 | + return bytes; |
| 93 | + } |
| 94 | + |
| 95 | + async function deriveKey(password, salt, usage) { |
| 96 | + const encoder = new TextEncoder(); |
| 97 | + const baseKey = await crypto.subtle.importKey("raw", encoder.encode(password), "PBKDF2", false, ["deriveKey"]); |
| 98 | + return crypto.subtle.deriveKey( |
| 99 | + { name: "PBKDF2", salt, iterations: 100000, hash: "SHA-256" }, |
| 100 | + baseKey, { name: "AES-GCM", length: 256 }, false, [usage] |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + document.getElementById('imageUpload').onchange = e => { |
| 105 | + const file = e.target.files[0]; |
| 106 | + if (file) { |
| 107 | + const reader = new FileReader(); |
| 108 | + reader.onload = e => { |
| 109 | + imagePreview.src = e.target.result; |
| 110 | + imagePreview.style.display = 'block'; |
| 111 | + placeholder.style.display = 'none'; |
| 112 | + }; |
| 113 | + reader.readAsDataURL(file); |
| 114 | + } |
| 115 | + }; |
| 116 | + |
| 117 | + document.getElementById('encryptBtn').onclick = async () => { |
| 118 | + const fileInput = document.getElementById('imageUpload'); |
| 119 | + const pass = document.getElementById('encPassword').value; |
| 120 | + if (!fileInput.files[0] || !pass) return showStatus("Missing file or password", "error"); |
| 121 | + |
| 122 | + showStatus("Encrypting...", "success"); |
| 123 | + try { |
| 124 | + const rawBuffer = await fileInput.files[0].arrayBuffer(); |
| 125 | + const salt = crypto.getRandomValues(new Uint8Array(16)); |
| 126 | + const iv = crypto.getRandomValues(new Uint8Array(12)); |
| 127 | + const key = await deriveKey(pass, salt, "encrypt"); |
| 128 | + const encrypted = await crypto.subtle.encrypt({ name: "AES-GCM", iv }, key, rawBuffer); |
| 129 | + |
| 130 | + const combined = new Uint8Array(28 + encrypted.byteLength); |
| 131 | + combined.set(salt, 0); |
| 132 | + combined.set(iv, 16); |
| 133 | + combined.set(new Uint8Array(encrypted), 28); |
| 134 | + |
| 135 | + const b64 = await bufferToBase64(combined); |
| 136 | + const blob = new Blob([b64], { type: "text/plain" }); |
| 137 | + const url = URL.createObjectURL(blob); |
| 138 | + const a = document.createElement('a'); |
| 139 | + a.href = url; |
| 140 | + a.download = `secure_${fileInput.files[0].name}.txt`; |
| 141 | + a.click(); |
| 142 | + showStatus("Success! File downloaded.", "success"); |
| 143 | + } catch (err) { showStatus("Encryption failed", "error"); } |
| 144 | + }; |
| 145 | + |
| 146 | + document.getElementById('decryptBtn').onclick = async () => { |
| 147 | + const fileInput = document.getElementById('fileUpload'); |
| 148 | + const pass = document.getElementById('decPassword').value; |
| 149 | + if (!fileInput.files[0] || !pass) return showStatus("Missing file or password", "error"); |
| 150 | + |
| 151 | + try { |
| 152 | + const b64Data = await fileInput.files[0].text(); |
| 153 | + const combined = base64ToBuffer(b64Data); |
| 154 | + const salt = combined.slice(0, 16); |
| 155 | + const iv = combined.slice(16, 28); |
| 156 | + const ciphertext = combined.slice(28); |
| 157 | + |
| 158 | + const key = await deriveKey(pass, salt, "decrypt"); |
| 159 | + const decrypted = await crypto.subtle.decrypt({ name: "AES-GCM", iv }, key, ciphertext); |
| 160 | + |
| 161 | + imagePreview.src = URL.createObjectURL(new Blob([decrypted])); |
| 162 | + imagePreview.style.display = 'block'; |
| 163 | + placeholder.style.display = 'none'; |
| 164 | + switchTab('encrypt'); |
| 165 | + showStatus("Decrypted successfully!", "success"); |
| 166 | + } catch (err) { showStatus("Wrong password or corrupted file", "error"); } |
| 167 | + }; |
| 168 | +</script> |
| 169 | +</body> |
| 170 | +</html> |
0 commit comments