End-to-End Encryption in Web Apps via Web Crypto API
Building secure web utilities requires end-to-end encryption (E2EE), ensuring data is encrypted before leaving the sender's device and remains encrypted until decrypted by the recipient. Historically, web developers relied on heavy external JS libraries for encryption, which degraded performance and introduced security risks. Modern browsers solve this with the Web Crypto API, a fast, hardware-accelerated cryptographic framework. This article demonstrates how to build a secure E2EE pipeline using WebCrypto, ECDH key exchanges, and AES-GCM-256.
1. Key Exchange via Elliptic Curve Diffie-Hellman (ECDH)
For secure encryption, the sender and receiver must share a symmetric key without exposing it to eavesdroppers. ECDH (using the P-256 curve) solves this problem. Both parties generate an ephemeral key pair in their respective browsers. They swap public keys via the signaling server. Using their private keys and the peer's public key, both independently derive the exact same shared secret. The raw AES key never crosses the network.
- Forward Secrecy: A new key pair is generated for each transfer, protecting past sessions.
- Zero Key Leakage: Only public keys are shared over the wire; private keys remain secure.
- Zero Server Knowledge: The signaling server only relays public keys, making decryption impossible.
2. AES-GCM-256 Symmetric Encryption
Once the shared secret is established, the file or text is encrypted using AES-GCM-256 (Advanced Encryption Standard with Galois/Counter Mode). GCM is preferred over standard CBC mode because it provides both confidentiality and authentication. This ensures that if any part of the encrypted chunk is altered in transit, the decryption process will fail immediately, preventing man-in-the-middle tampering.
3. Cryptographic Web Workers for Smooth Performance
Running cryptographic operations on the browser's main thread causes user interfaces to freeze, especially for large files. Web Workers execute code in a background thread, resolving performance issues. The file buffer is transferred to the worker, split into chunks, encrypted, and posted back as binary packages, ensuring UI responsiveness.
Conclusion
By combining ephemeral ECDH key exchange with AES-GCM-256 encryption inside Web Workers, we build a secure, browser-native file transfer pipeline. Share2Me integrates this E2EE model, ensuring your private data remains completely private.