§ Case study · Cryptography 2022 · School project · Source on GitHub

RSA from scratch — crypted-chat

RSA written by hand. Miller–Rabin for the primes. Java.

Pre-shared key chat over TCP, with the entire crypto stack rolled in-house.

A chat platform that establishes a secure TCP connection via a hand-implemented RSA key exchange. Probabilistic prime generation via Miller–Rabin primality testing. Built in Java + Processing during gymnasium (HTX).

  • Cryptography
  • RSA
  • Miller–Rabin
  • Java
  • Processing
  • TCP
Age at build
17
2022 · HTX gymnasium
Cryptography
From scratch
no library shortcuts
Prime test
Miller–Rabin
probabilistic
Transport
TCP
raw sockets

§ Abstract

Cryptography classes always teach RSA at the algebra level: pick two primes, multiply, compute φ, choose e, derive d. The students nod. Then they import a library and never think about the math again.

This project is what happens when you don’t import the library. Java + Processing. Hand-rolled RSA over a hand-rolled TCP chat. Pre-shared key for the initial handshake; per-session keypair generation using probabilistic prime testing.

§ What surfaced

  • Generating large primes is the slow part of RSA. A naive sieve gets you nowhere past a few hundred bits.
  • Miller–Rabin is the right primality test for everything between trial division and Lucas–Lehmer. Probabilistic, but the false-positive rate is exponentially small in the number of rounds.
  • Java’s BigInteger is generous about modular arithmetic, but writing the modular exponentiation routine by hand is the only way to actually understand it.
  • Processing is a surprisingly good prototyping environment for desktop networking demos.

§ Method

Generate two large primes p and q using random odd candidates, screened first by trial division against the small primes, then by k-round Miller–Rabin. Compute n = p·q and φ = (p−1)(q−1). Choose e = 65537 (the standard Fermat prime). Derive d via the extended Euclidean algorithm.

Encrypt: c = m^e mod n. Decrypt: m = c^d mod n.

Modular exponentiation via square-and-multiply. The pre-shared key bootstraps the initial channel; per-session RSA keypairs handshake from there.

§ Implementation

  • Java for the cryptographic core (BigInteger arithmetic)
  • Processing for the chat UI and TCP transport
  • Pre-shared key supplied out-of-band (in the project, hard-coded for demo purposes)
  • Per-session RSA keypair regeneration on each new connection

§ Outcomes

  • Working chat over TCP with hand-rolled RSA on both sides
  • Miller–Rabin primality test with configurable round count
  • Modular exponentiation via square-and-multiply
  • Source preserved at github.com/Muno459/crypted-chat for anyone who wants to read it

This is a school project. It’s not what you’d ship in production — it has no integrity layer, no key rotation, no padding. It’s here because it taught me more about RSA than any class did, and because the operator who wrote it at 17 is the same operator writing this site at 22. Same instinct, harder problems.

Let's take the housing off something.

Reverse-engineering, Web3 infrastructure, firmware teardowns, consulting. I answer email inside 24 hours.