An End-to-End Encrypted (E2EE) chat application via a stateless WebSocket relay, featuring RSA encryption with custom modular arithmetic libraries.
An educational repository for modular arithmetic and RSA, including:
- A browser-based CryptoChat E2EE client (PyScript/Pyodide) that encrypts/decrypts using Python.
- A WebSocket relay server (Bun) that broadcasts packets to connected peers.
- Python libraries (
modularandrsa) plus demo/attack scripts.
Disclaimer: this is teaching code, not production cryptography. It uses small keys and a simplistic per-character scheme with a decimal-suffix padding, which is vulnerable to multiple attacks.
RSA works over modular arithmetic.
Key generation
- Choose two primes
$p$ and$q$ . - Compute:
- Choose an exponent
$e$ such that$\gcd(e, \varphi(n)) = 1$ . - Compute the modular inverse
$d$ :
Public key:
Private key:
Encryption / decryption
Represent the plaintext as an integer
This repo often encrypts per character (each Unicode codepoint separately). It also includes a simple teaching padding: append
- File:
index.html - What it does:
- Generates RSA keys in Python (inside the browser via Pyodide).
- Broadcasts presence (name + public key) to the network.
- Supports 1:1 chat: encrypts with the recipient public key and decrypts with your private key.
- Keeps a “Global Hub Network” tab as an encrypted traffic monitor.
- File:
server.ts - What it does:
- Accepts WebSocket connections at
ws://localhost:3000. - Subscribes clients to a global channel (
chat_global). - Republishes messages to all subscribers (excluding the sender).
- Accepts WebSocket connections at
Number theory and modular arithmetic helpers.
- Entry point:
modular/__init__.py - Implementation:
modular/core.py - Includes:
gcd,bezout,mod_inverse,mod_pow,factorize,euler_totient, CRT utilities, etc.
Basic RSA utilities and the simple decimal-suffix padding.
- Entry point:
rsa/__init__.py - Implementation:
rsa/core.py - Includes:
generate_keys,encrypt_string,decrypt_string, etc.
rsa_attacks.py: cryptanalysis helpers for small RSA (factoring-based key recovery, dictionary attack, padding brute force).criptochat.py: CLI version that stores contacts incontactos.jsonand messages in.txtfiles.modular.py: legacy/experimental script with similar routines (contains prints/tests at the end).imatlab.py,imatlab_benchmark.py: lab utilities/benchmarks.
- Bun (for the WebSocket relay).
- Python 3.11+ (for Python scripts/libraries).
- Internet access to load PyScript from the CDN used in
index.html.
If you use the Python libraries, pyproject.toml declares a dependency on numpy.
- Start the WebSocket relay:
bun run server.ts- Serve the static files (recommended; avoid opening
file://):
python -m http.server 8000- Open the client:
- In the UI:
- Set your public name and click Go online.
- Wait until it shows Python Ready ✅.
- Select an online user and send messages.
Encrypt/decrypt example:
import rsa
n, e, d = rsa.generate_keys(100, 500)
msg = "hello"
cipher = rsa.encrypt_string(msg, n, e, padding_digits=2)
plain = rsa.decrypt_string(cipher, n, d, padding_digits=2)
print(plain)Recover d by factoring n (toy example):
from rsa_attacks import recover_private_exponent
d = recover_private_exponent(n, e)index.html: web client (PyScript) + UI.server.ts: WebSocket relay (Bun).rsa/: RSA package.modular/: modular arithmetic package.rsa_attacks.py: attacks/demos.criptochat.py: CLI +contactos.json.
- Real RSA requires large keys (e.g. 2048+ bits) and standard padding (OAEP/PSS).
- Per-character encryption and ad-hoc decimal padding leak structure and are attackable.
- This project is meant to learn, experiment, and observe attacks in practice.