A fully functional end-to-end encrypted (E2EE) chat application built with vanilla JavaScript and Python. Zero external crypto librariesβuses only the native Web Crypto API.
β οΈ Educational Project: This is a demonstration of E2EE concepts. While the cryptography is solid, a production system would need additional hardening.
- True End-to-End Encryption β Messages are encrypted in the browser. The server sees only ciphertext.
- No Account Required β No email, phone, or password. Just a cryptographic identity.
- Recovery Key System β A 64-character hex key for account backup and restore.
- Group Chats β Encrypted group messaging with AES key sharing.
- Real-time Messaging β WebSocket-based instant delivery.
- Rate Limiting β Built-in protection against abuse (registration, messages, groups).
- Key Verification β Safety numbers to verify contact identity with a visual verification banner.
- Cross-Platform β Web app and Android APK (via Capacitor).
- Self-Hostable β Run your own server with full control.
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β KEY GENERATION β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Recovery Key (64 hex) βββΊ PBKDF2 βββΊ RSA-2048 Key Pair β
β βββ Public Key (shared) β
β βββ Private Key (local) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β MESSAGE ENCRYPTION β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β 1. Generate random AES-256 key for this message. β
β 2. Encrypt message with AES-256-GCM. β
β 3. Encrypt AES key with recipient's RSA public key. β
β 4. Send: [Encrypted AES Key + IV + Encrypted Message]. β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β MESSAGE DECRYPTION β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β 1. Decrypt AES key using own RSA private key. β
β 2. Decrypt message using AES key + IV. β
β 3. Display plaintext to user. β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
| Purpose | Algorithm | Details |
|---|---|---|
| Key Exchange | RSA-OAEP | 2048-bit modulus, SHA-256. |
| Message Encryption | AES-GCM | 256-bit key, 96-bit IV. |
| Key Derivation | PBKDF2 | SHA-256, 100,000 iterations. |
| User ID | SHA-256 | First 128 bits of hash. |
// Server storageβcompletely opaque.
{
"sender_id": "a1b2c3d4e5f6...",
"recipient_id": "9z8y7x6w5v4...",
"encrypted_content": "Base64(RSA(AES_Key) + IV + AES(message))",
"created_at": "2024-01-15T10:30:00Z"
}
// Server CANNOT read message contentβno private keys.ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β FRONTEND β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β app.js β crypto.js β websocket.js β β
β β - UI Logic β - Web Crypto β - Real-time β β
β β - State Mgmt β - RSA/AES β - Reconnection β β
β β - API Calls β - Key Storage β - Event Handling β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββ
β HTTPS / WSS
βΌ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β BACKEND β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β FastAPI β β
β β - REST API (messages, users, groups). β β
β β - WebSocket server (real-time delivery). β β
β β - Rate limiting (registration, messages, groups). β β
β β - NO encryption/decryption (just stores blobs). β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β MySQL Database β β
β β - Users (public keys only). β β
β β - Messages (encrypted blobs). β β
β β - Groups (encrypted group keys per member). β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
- Python 3.8+
- MySQL 8.0+
- Node.js 16+ (for APK build only)
git clone https://github.com/yourusername/securechat.git
cd securechat
# Create virtual environment.
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
# Install dependencies.
pip install fastapi uvicorn mysql-connector-python websockets python-dotenvCREATE DATABASE securechat;
CREATE USER 'securechat'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON securechat.* TO 'securechat'@'localhost';Create .env in the backend folder:
DB_HOST=localhost
DB_USER=securechat
DB_PASSWORD=your_password
DB_NAME=securechatcd backend
python main.pyVisit http://localhost:8000βthat's it!
After configuring your server host in frontend/js/app.js, run the SRI script to enable code signing:
.\generate-sri.ps1This generates SHA-384 hashes for all JS/CSS files and adds integrity attributes to index.html. If anyone tampers with your files, the browser will refuse to load them.
Important: Re-run this script every time you modify JS or CSS files.
cd frontend
# Install Capacitor.
npm install @capacitor/core @capacitor/cli @capacitor/android
# Initialize (first time only).
npx cap init SecureChat com.securechat.app
# Copy web files to www/.
.\build.ps1 # Or: node build.js
# Add Android platform.
npx cap add android
# Sync and build.
npx cap sync android
npx cap open android # Opens Android Studio.Build the APK in Android Studio: Build β Build Bundle(s) / APK(s) β Build APK(s).
securechat/
βββ backend/
β βββ main.py # FastAPI server + WebSocket + Rate Limiting.
β βββ schema.sql # Database schema.
β βββ .env # Configuration (not in repo).
β
βββ frontend/
β βββ index.html # Single page app.
β βββ css/
β β βββ style.css # Dark theme UI.
β βββ js/
β β βββ app.js # Main application logic.
β β βββ crypto.js # Web Crypto API wrapper.
β β βββ websocket.js # Real-time connection.
β βββ package.json
β
βββ docs/
β βββ API.md # API documentation.
β βββ ENCRYPTION.md # Encryption details.
β
βββ generate-sri.ps1 # SRI hash generator script.
βββ README.md
β
Proper E2EE β Private keys never leave the device.
β
Standard Algorithms β RSA-OAEP, AES-GCM, PBKDF2.
β
No Crypto Libraries β Native Web Crypto API only.
β
Server-Side Ignorance β Server cannot read messages.
β
Rate Limiting β Protection against brute-force and spam.
β
Key Verification β Safety numbers with visual verification banner.
β
Code Signing (SRI) β Subresource Integrity verifies frontend files haven't been tampered with. Run generate-sri.ps1 to enable.
| Threat | Mitigation |
|---|---|
| Server compromise | Server has no keys, only encrypted blobs. |
| Network interception | TLS + E2EE double protection. |
| Database leak | Messages remain encrypted. |
| APK decompilation | Security is in keys, not code. |
| Device theft | Keys in localStorage (use device encryption). |
| Brute-force attacks | Rate limiting on registration, messages, and groups. |
Contributions welcome! Areas that need work:
- Forward secrecy (Double Ratchet).
- File/image sharing.
- Message search (client-side).
- Desktop app (Electron).
- iOS build.
- Automated tests.
If you're learning about E2EE, check out:
MIT License β See LICENSE for details.
Built for learning. Use responsibly. Stay secure. π