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 potentially smaller exponent.
The reason the mailbox opens
Factoring the modulus breaks RSA; the converse equivalence between general RSA inversion and factoring is not known. Correctness is a separate question, supplied here by modular arithmetic and the Chinese remainder theorem. This raw textbook-RSA round trip lacks secure encodings and must not be used for real encryption.
Sources and scope: RFC 8017 (2016), RSA key relations and complete encryption/signature schemes.
Continue with RSA or explore modular arithmetic.