A logbook for curious waters
← All Reckonings

The Daily Reckoning

How Can One Exponent Lock a Message and Another Unlock It?

Euler’s theorem explains the arithmetic hinge inside RSA: choose two exponents whose product is one full modular cycle plus one step.

Arithmetic Behind Secure Messages · Part 3 of 3

Difficulty
Intermediate
Area
Numbers and algebra
Reading time
10 minutes
Prerequisites
Modular arithmetic and congruence · Prime numbers · Fermat’s little theorem · Greatest common divisors
A blue route carries the number 12 through a circular public gate marked e equals 3 and modulus 55 to the number 23; a pink return route passes through a private gate marked d equals 27 and arrives back at 12 over a dark maritime blueprint grid.

Can you publish a rule that anyone may use to scramble a message, yet keep a different rule that reverses the scrambling?

The Rivest–Shamir–Adleman cryptosystem, usually called RSA, answers yes with two exponents. A public exponent sends a message around a modular clock. A private exponent completes enough additional laps to return the message to its starting point. The lock and the key are both powers.

This lesson finishes the three-part Arithmetic Behind Secure Messages series. Part one introduced modular clocks. Part two used Fermat’s little theorem to guarantee a loop when the modulus is prime. RSA needs a composite modulus, so one more counting function enters the story.

Count the numbers that can multiply backward

Two positive integers are coprime when their greatest common divisor is 11. For example, 88 and 1515 are coprime even though neither number is prime. The only positive integer dividing both is 11.

Euler’s totient function counts the positive integers below a chosen modulus that are coprime to it. The notation is

φ(n)=#{k:1≤k<n and gcd⁡(k,n)=1}.\varphi(n)=\#\{k:1\le k<n\text{ and }\gcd(k,n)=1\}.

Here nn is a positive integer, kk ranges over the integers from 11 through n−1n-1, gcd⁡(k,n)\gcd(k,n) is the greatest common divisor of kk and nn, and the symbol #\# means “the number of members in the set.”

For n=10n=10, the eligible integers are

1,3,7,9,1,3,7,9,

so φ(10)=4\varphi(10)=4.

When pp and qq are two different primes, the product n=pqn=pq has a useful count:

φ(pq)=(p−1)(q−1).\varphi(pq)=(p-1)(q-1).

To see why, inspect the integers from 11 through pqpq. There are pqpq integers in total. Exactly qq of them are multiples of pp, and exactly pp are multiples of qq. The number pqpq appeared in both rejected groups, so add it back once after subtracting twice:

pq−q−p+1=(p−1)(q−1).pq-q-p+1=(p-1)(q-1).

Every integer left by that count is coprime to pqpq.

Euler extends the prime loop

Euler’s theorem states:

Let nn be a positive integer, and let aa be an integer with gcd⁡(a,n)=1\gcd(a,n)=1. Then

aφ(n)≡1(modn).a^{\varphi(n)}\equiv1\pmod n.

The theorem says that raising a number coprime to nn to the power φ(n)\varphi(n) completes a modular loop.

A modern proof extends the rearrangement used in part two. List every nonzero remainder that is coprime to nn. Multiplication by aa cannot create a collision among those remainders, and each product remains coprime to nn. Multiplication by aa therefore rearranges the φ(n)\varphi(n) entries.

Let PP be the product of every entry in the original list. Multiplying each entry by aa inserts φ(n)\varphi(n) factors of aa, while rearrangement leaves the product congruent to PP. Thus

aφ(n)P≡P(modn).a^{\varphi(n)}P\equiv P\pmod n.

Every factor of PP is coprime to nn, so PP has a multiplicative inverse modulo nn. Multiply both sides by that inverse to cancel PP and obtain aφ(n)≡1(modn)a^{\varphi(n)}\equiv1\pmod n.

Fermat’s little theorem is the prime case. If n=pn=p is prime, every integer from 11 through p−1p-1 is coprime to pp, so φ(p)=p−1\varphi(p)=p-1.

Build a lock from two small primes

Real RSA keys use enormous integers and carefully specified encoding rules. The following numbers are deliberately tiny so every operation can be inspected.

Choose two primes:

p=5,q=11.p=5,\qquad q=11.

Their product is the modulus

n=pq=55,n=pq=55,

and the totient count is

φ(55)=(5−1)(11−1)=4⋅10=40.\varphi(55)=(5-1)(11-1)=4\cdot10=40.

Choose the public exponent e=3e=3. Next, choose a private exponent dd so that eded leaves remainder 11 after division by 4040. The value d=27d=27 works because

ed=3⋅27=81=2⋅40+1.ed=3\cdot27=81=2\cdot40+1.

The public key is the pair (e,n)=(3,55)(e,n)=(3,55). The private exponent is d=27d=27.

Represent a message by an integer mm from 00 through 5454. Encryption produces a ciphertext cc by

c≡me(modn).c\equiv m^e\pmod n.

Decryption raises that ciphertext to the private exponent:

cd≡(me)d=med=m2φ(55)+1(mod55).c^d\equiv(m^e)^d=m^{ed}=m^{2\varphi(55)+1}\pmod{55}.

If mm is coprime to 5555, Euler’s theorem gives m40≡1(mod55)m^{40}\equiv1\pmod{55}. Therefore

m81=m2⋅40+1≡12m≡m(mod55).m^{81}=m^{2\cdot40+1}\equiv1^2m\equiv m\pmod{55}.

The extra “plus one” in 81=2⋅40+181=2\cdot40+1 is the whole trick. Eighty powers make two complete loops; the final power restores the original message.

The diagram below runs this toy system. Move the message slider and watch both exponentiations. The fact cards identify whether Euler’s theorem supplies the shortest proof for the selected message.

Toy RSA workbench

One public turn, one private return

Public (e, n) = (3, 55) · Private d = 27

12³ ≡ 23 (mod 55), then 23²⁷ ≡ 12 (mod 55).

Greatest common divisor
gcd(12, 55) = 1
Shortest proof available
Euler’s theorem
Round trip
12 → 23 → 12
Choose any integer from 0 through 54. The public exponent 3 changes the message; the private exponent 27 returns it. Messages sharing a factor with 55 still return, but they require the prime-by-prime argument explained below.

Worked example: lock and recover 12

Take m=12m=12. Because gcd⁡(12,55)=1\gcd(12,55)=1, Euler’s theorem applies.

Encrypt by cubing 1212 modulo 5555:

122=144=2⋅55+34,123≡34⋅12=408=7⋅55+23(mod55).\begin{aligned} 12^2&=144=2\cdot55+34,\\ 12^3&\equiv34\cdot12=408=7\cdot55+23\pmod{55}. \end{aligned}

The ciphertext is c=23c=23.

Now decrypt by computing 232723^{27} modulo 5555. Repeated squaring keeps the numbers small:

232=529=9⋅55+34,234≡342=1156=21⋅55+1≡1(mod55).\begin{aligned} 23^2&=529=9\cdot55+34,\\ 23^4&\equiv34^2=1156=21\cdot55+1\equiv1\pmod{55}. \end{aligned}

Write 27=4⋅6+327=4\cdot6+3. Then

2327=(234)6⋅233≡16⋅(23⋅232)(mod55)≡23⋅34=782=14⋅55+12(mod55).\begin{aligned} 23^{27} &=(23^4)^6\cdot23^3\\ &\equiv1^6\cdot(23\cdot23^2)\pmod{55}\\ &\equiv23\cdot34=782=14\cdot55+12\pmod{55}. \end{aligned}

Decryption recovers 1212.

What if the message shares a factor with 55?

Euler’s theorem requires gcd⁡(m,55)=1\gcd(m,55)=1, but the toy RSA round trip also succeeds for m=5m=5, 1111, and the other non-coprime messages. Prove the claim separately modulo 55 and modulo 1111. If a prime divides mm, then both m81m^{81} and mm are 00 modulo that prime. If the prime does not divide mm, Fermat’s little theorem reduces the exponent. The two prime-by-prime conclusions combine to give m81≡m(mod55)m^{81}\equiv m\pmod{55}. This last combination is an instance of the Chinese remainder theorem.

Try the arithmetic hinge

Problem 1

Let p=7p=7 and q=11q=11. Find n=pqn=pq and φ(n)\varphi(n).

Reveal the solution

The modulus is

n=pq=7⋅11=77.n=pq=7\cdot11=77.

Because 77 and 1111 are different primes,

φ(77)=(7−1)(11−1)=6⋅10=60.\varphi(77)=(7-1)(11-1)=6\cdot10=60.

Therefore n=77n=77 and φ(n)=60\varphi(n)=60.

Problem 2

For the modulus 7777 from Problem 1, suppose the public exponent is e=7e=7. Find a positive integer d<60d<60 such that ed≡1(mod60)ed\equiv1\pmod{60}.

Reveal the solution

We need 7d7d to be one more than a multiple of 6060. Try multiples of 6060 until adding 11 produces a multiple of 77:

60⋅5+1=301=7⋅43.60\cdot5+1=301=7\cdot43.

Thus

7⋅43≡1(mod60).7\cdot43\equiv1\pmod{60}.

One valid private exponent is d=43d=43.

From an academy theorem to public and secret cryptography

Leonhard Euler developed the composite-modulus theorem over many years. The Euler Archive dates Theoremata arithmetica nova methodo demonstrata to 1758 and its publication in the St Petersburg Academy’s journal to 1763. The archive record also notes presentations to the Berlin Academy in 1758 and the St Petersburg Academy in 1759. Euler’s route from Fermat’s prime theorem to a count of integers coprime to a composite modulus supplied the mathematical cycle used here.

Two centuries later, electronic communication created a different problem: how could people exchange encrypted messages without first transporting a shared secret key? Ronald Rivest, Adi Shamir, and Leonard Adleman answered with the system now named from their initials. Their 1978 paper explicitly targets electronic mail and electronic funds transfer, defines encryption and decryption as modular powers, and proves the round trip for a modulus made from two primes. The paper also records support from the U.S. National Science Foundation and Office of Naval Research. A theorem that grew inside eighteenth-century European academies became infrastructure research shaped by twentieth-century computer networks, commerce, and military funding.

The public history has a classified parallel. James Ellis and Clifford Cocks developed comparable public-key ideas at Britain’s Government Communications Headquarters; Cocks produced an RSA-like construction in 1973. The intelligence agency kept the work secret, and it did not shape the open development of RSA. GCHQ disclosed the earlier work in 1997. Steven Levy’s detailed history of the episode shows how classification delayed both recognition and public use. The same mathematical possibility emerged in two institutions, but secrecy determined which discovery could circulate, be tested, and become a standard.

What the toy lock leaves out

Encryption and signatures. The current Internet Engineering Task Force specification, RFC 8017, defines RSA primitives for encryption, decryption, signing, and verification. It also defines encoding schemes such as Optimal Asymmetric Encryption Padding (OAEP) and Probabilistic Signature Scheme (PSS). Raw textbook RSA—the deterministic calculation in this lesson—is not a safe encryption protocol.

The trapdoor. Multiplying the secret primes pp and qq is easy. Recovering them from a suitably large public modulus n=pqn=pq is believed to be hard for conventional computers. Anyone who knows the factorization can compute φ(n)\varphi(n) and derive the private exponent. Euler’s theorem explains why decryption works; the presumed difficulty of factorization helps explain why the private exponent can remain private.

Future pressure. A sufficiently capable quantum computer would threaten RSA and other current public-key systems. This is not a claim that such a machine exists today. The U.S. National Institute of Standards and Technology finalized its first post-quantum standards in August 2024 and urged organizations to begin migrating. The arithmetic in RSA remains beautiful even as security engineering prepares to rely on different hard problems.

One idea to remember

Choose exponents ee and dd so that eded is a whole number of Euler cycles plus 11. The cycles disappear modulo nn; the extra 11 returns the message.

Revisit the modular clock or the prime rearrangement behind Fermat’s little theorem. A natural next step is the Chinese remainder theorem, which explains how separate conclusions modulo two primes combine into one conclusion modulo their product.

Sources