Why RSA
undoes itself
RSA encrypts by raising a message to a power, and decrypts by raising the result to another. That these two operations cancel — that you land back on the exact message you started with — is not luck. It's a 400-year-old theorem of Fermat, generalized by Euler.
Fermat's little theorem
For any prime p and any a not divisible by p, raising a to the (p−1) power lands exactly on 1, modulo p. Every base, no exceptions.
Euler generalized it from primes to any modulus n, replacing p−1 with φ(n) — the count of numbers below n that share no factor with it. This is the version RSA runs on.
The round trip
RSA picks exponents e and d that are inverses modulo φ(n), so that e·d ≡ 1. Then encrypting (raise to e) and decrypting (raise to d) compose to raising by e·d — which, by Euler's theorem, does nothing at all. The message returns untouched. Type a small number and send it through.
One honest caveat: Euler's theorem as stated needs gcd(m, n) = 1, and with this toy key a few messages — the multiples of 61 or 53 — fail that test. RSA still round-trips them, but the reason is slightly different: you check the congruence separately modulo p and modulo q, where it holds either trivially or by Fermat, then glue the two halves back together with the Chinese Remainder Theorem. Real implementations also use Carmichael's λ(n) rather than φ(n) to pick d; it divides φ(n) and yields the same round trip with a smaller exponent.
The reason the mailbox opens
The security of RSA rests on factoring being hard (see The One-Way Function). But its correctness — the promise that the right key recovers the message — rests entirely here, on Euler's theorem. Hardness keeps the attacker out; this theorem lets the owner back in.