Web Crypto API: फ़ाइल एन्क्रिप्शन के लिए संपूर्ण ट्यूटोरियल
ब्राउज़र-आधारित फ़ाइल एन्क्रिप्शन के लिए Web Crypto API में महारत हासिल करें। JavaScript अनुप्रयोगों में AES-GCM, RSA-OAEP और की मैनेजमेंट।
Web Crypto API native window.crypto.subtle methods का उपयोग करके browser में directly files encrypt करने देता है — कोई external libraries required नहीं। File encryption के लिए, आप typically PBKDF2 (210,000 iterations, SHA-256) के माध्यम से password से key derive करेंगे, फिर 96-bit IV और 128-bit auth tag के साथ AES-GCM का उपयोग करके file bytes encrypt करेंगे। Public-key workflows symmetric key wrap करने के लिए 4096-bit keys के साथ RSA-OAEP उपयोग करते हैं। API हर modern browser में HTTPS पर available है और JavaScript के बजाय native crypto backend में चलता है।
SubtleCrypto Pure-JS Libraries से क्यों बेहतर है
window.crypto.subtle browser के audited native crypto backend में call करता है — Chromium में usually BoringSSL या Safari पर CommonCrypto। CryptoJS या sjcl जैसे pure-JS options की तुलना में, SubtleCrypto AES-GCM के लिए 30-80x faster चलता है, JavaScript interpreters में timing side-channels avoid करता है, और users को zero bytes ship करता है। Tradeoff एक Promise-based API है जो केवल ArrayBuffer और CryptoKey objects पर operate करता है, इसलिए आप Uint8Array, Blob, और ReadableStream के बीच shuffle करने में बहुत समय spend करते हैं। 100 MB से अधिक file sizes के लिए, वह plumbing raw crypto speed से अधिक मायने रखता है।
PBKDF2 के साथ Password से Key Derive करना
Password को directly AES key के रूप में कभी उपयोग न करें। इसके बजाय, password को raw material के रूप में import करें, फिर 256-bit key derive करें:
async function deriveKey(password, salt) {
const enc = new TextEncoder();
const material = await crypto.subtle.importKey(
'raw', enc.encode(password), 'PBKDF2', false, ['deriveKey']
);
return crypto.subtle.deriveKey(
{ name: 'PBKDF2', salt, iterations: 210000, hash: 'SHA-256' },
material,
{ name: 'AES-GCM', length: 256 },
false,
['encrypt', 'decrypt']
);
}
OWASP का 2026 guidance SHA-256 के साथ कम से कम 600,000 iterations call करता है, हालांकि 210,000 low-risk contexts के लिए acceptable रहता है। crypto.getRandomValues के साथ प्रति file fresh 16-byte salt generate करें और इसे ciphertext के साथ store करें। Argon2id stronger होगा लेकिन SubtleCrypto द्वारा अभी exposed नहीं है।
AES-GCM के साथ File Encrypt करना
AES-GCM एक pass में confidentiality और authenticity देता है। Critical rule है (key, IV) pair कभी reuse न करें:
async function encryptFile(file, key) {
const iv = crypto.getRandomValues(new Uint8Array(12));
const plaintext = await file.arrayBuffer();
const ciphertext = await crypto.subtle.encrypt(
{ name: 'AES-GCM', iv, tagLength: 128 },
key,
plaintext
);
return { iv, ciphertext };
}
2 GB file के लिए, file.arrayBuffer() पूरा buffer allocate करेगा, जो अक्सर mobile Safari crash करता है। File को 4 MB chunks में split करें, प्रत्येक को counter से concatenated random prefix से derived unique IV के साथ encrypt करें, और version byte और salt prepend करें ताकि decryptor जाने कि किससे dealing है।
TransformStream के माध्यम से Large Files Streaming करना
Memory blowup avoid करने के लिए, encryption को TransformStream में wrap करें और file को pipe करें:
const chunkSize = 4 * 1024 * 1024;
const encryptor = new TransformStream({
async transform(chunk, controller) {
const iv = nextIV(counter++);
const ct = await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, key, chunk);
controller.enqueue(new Uint8Array([...iv, ...new Uint8Array(ct)]));
}
});
await file.stream()
.pipeThrough(sliceByChunks(chunkSize))
.pipeThrough(encryptor)
.pipeTo(uploadSink);
file.stream() एक ReadableStream<Uint8Array> return करता है जो disk से lazily reads करता है। Slicer fixed-size chunks produce करता है ताकि GCM tags predictably align हों। 10 GB upload पर भी Peak memory 20 MB से कम रहती है।
RSA-OAEP के साथ Symmetric Key Wrap करना
जब आपको specific recipient के साथ file share करनी हो, उनका RSA keypair एक बार generate करें और public key publish करें:
const keypair = await crypto.subtle.generateKey(
{ name: 'RSA-OAEP', modulusLength: 4096,
publicExponent: new Uint8Array([1,0,1]), hash: 'SHA-256' },
true, ['wrapKey', 'unwrapKey']
);
File के लिए AES-GCM key generate करें, फिर wrap करें:
const wrapped = await crypto.subtle.wrapKey(
'raw', fileKey, keypair.publicKey,
{ name: 'RSA-OAEP' }
);
4096-bit RSA keys NIST SP 800-57 के अनुसार 2030 तक roughly 150-bit security देती हैं। यदि आपको forward secrecy या post-quantum resistance चाहिए, RSA-OAEP को P-384 पर ECDH के साथ pair करें या WebCrypto working group इसे land करने के बाद ML-KEM (Kyber) पर migrate करें।
IndexedDB में Keys Safely Store करना
CryptoKey objects default रूप से non-extractable हैं, जिसका मतलब है आप उन्हें raw bytes JavaScript पर कभी expose किए बिना IndexedDB में persist कर सकते हैं:
const db = await openDB('keystore', 1);
await db.put('keys', keypair.privateKey, 'user-signing-key');
Browsers structured clone algorithm का उपयोग करके key serialize करते हैं और actual bytes crypto backend में रखते हैं। Compromised script stored key के साथ encrypt या decrypt call कर सकता है लेकिन उसका material out नहीं read कर सकता। यह base64 keys को localStorage में stuffing की तुलना में meaningful hardening step है।
API के Throw Errors Handle करना
SubtleCrypto authenticated decryption failures के लिए OperationError throw करता है, जिसका आमतौर पर मतलब है ciphertext tampered था, IV गलत है, या user ने गलत password type किया। यह DataError throw करता है जब input buffer की wrong length हो, NotSupportedError जब algorithm implement नहीं हो, और InvalidAccessError जब key को right usage flags के साथ import नहीं किया गया। Decryption को हमेशा try/catch में wrap करें, neutral "file decrypt नहीं हो सकी" message surface करें, और यह leak होने से avoid करें कि tag failed हुआ या structure।
Real-World Gotchas
Android पर Firefox UI thread freeze होने से पहले deriveKey iterations को roughly 1 million पर cap करता है, इसलिए key derivation को dedicated Worker के अंदर run करें। 16.4 से नीचे Safari PSS padding के साथ crypto.subtle.verify support नहीं करता। Chrome invocation पर getRandomValues calls को 64 KB से ऊपर throttle करता है, इसलिए अधिक entropy चाहिए तो loop करें। और postMessage के माध्यम से ArrayBuffer transfers zero-copy हैं लेकिन original को detach करते हैं, जो लोगों को off guard पकड़ता है।
HexaTransfer हर upload के लिए exactly यह AES-GCM plus PBKDF2 pipeline उपयोग करता है, Worker में derived keys के साथ और ciphertext storage पर streamed बिना server plaintext देखे। hexatransfer.com पर मुफ्त में आज़माएं — कोई खाता नहीं, 10 GB अधिकतम।
सब कुछ एक साथ जोड़ना
एक minimal encrypted-upload flow: getRandomValues के साथ salt और IV generate करें, PBKDF2 के माध्यम से user के password से AES-GCM key derive करें, file को TransformStream के माध्यम से stream करें जो प्रत्येक 4 MB chunk encrypt करता है, version, salt, और chunk count वाला small header prepend करें, और result आपके server पर POST करें। Download पर, process को chunk by chunk reverse करें, wrong password या corruption के signal के रूप में OperationError catch करें। Web Crypto API आपको जो चाहिए सब देता है, और browser का native implementation किसी भी JavaScript alternative को एक order of magnitude outrun करेगा।
एंड-टू-एंड एन्क्रिप्शन के साथ बड़ी फ़ाइलें सुरक्षित रूप से भेजें
एंड-टू-एंड एन्क्रिप्शन के साथ 10 GB तक की फ़ाइलें मुफ़्त में ट्रांसफ़र करें। अकाउंट की आवश्यकता नहीं। अपलोड से पहले आपकी फ़ाइलें ब्राउज़र में एन्क्रिप्ट की जाती हैं — कोई और उन्हें पढ़ नहीं सकता।
फ़ाइल भेजें