Encryption and decryption via modular exponentiation [01F9]
- October 31, 2026
- Marcelo Fiore, Jon Sterling
We now consider some other additional cryptographic protocols that are based on having separate keys for encryption and for decryption. In the Diffie–Hellman protocol, each actor had just one private key, whereas we will now consider protocols in which the private keys are split into two: an encryption key and a decryption key. The relationship between encryption and decryption keys will then be governed by Fermat’s little theorem.
We are going to use modular exponentiation to model encryption and decryption. In particular, we will be considering pairs of integers {\mathopen {}\left (e,d\right )\mathclose {}} such that k^{e\cdot d} \equiv k \mod p; such a pair is an encryption-decryption pair modulo p. To find a good source of such encryption-decryption pairs, we must return to Fermat’s Little Theorem and develop some of its consequences.
Theorem Fermat’s Little Theorem [01BK]
- September 29, 2026
- Marcelo Fiore, Jon Sterling
For all natural numbers i and primes p, we have the following:
- i^p \equiv i \mod p
- i^{p-1}\equiv 1 \mod p unless i is a multiple of p
For simplicity, we had previously established Fermat’s Little Theorem only for naturals. But it holds, in fact, for all integers, which we shall need:
Corollary Fermat’s Little Theorem for integers [01FC]
- October 31, 2026
- Jon Sterling
For all integers k and primes p, we have the following:
- k^p\equiv k \mod p
- k^{p-1}\equiv 1 \mod p unless k is a multiple of p.
Proof.
- October 31, 2026
- Jon Sterling
This follows straightaway from Fermat’s Little Theorem, because every integer k is congruent to some natural i modulo p: indeed, just add p to k enough times to get into the naturals.
Then we have: k^p \equiv i^p \equiv i \equiv k \mod p and k^{p-1}\equiv i^{p-1}\equiv 1\mod p under the stated assumptions.
Corollary A useful consequence of Fermat’s Little Theorem [01FB]
- October 31, 2026
- Jon Sterling
For every natural number c, integer k, and prime p, the following identity holds modulo p:
k^{1+c\cdot {\mathopen {}\left (p-1\right )\mathclose {}}} \equiv k \mod pProof.
- October 31, 2026
- Jon Sterling
We proceed depending on whether p\mid k.
- Suppose that p\mid k. Then k \equiv 0\mod p by definition, so k^{1+c\cdot {\mathopen {}\left (p-1\right )\mathclose {}}} \equiv 0 \equiv k\mod p\text {.}
- Suppose on the other hand that p\nmid k. Then by Fermat’s Little Theorem for integers, we have k^{p-1}\equiv 1 \mod p. We therefore deduce k^{1+c\cdot {\mathopen {}\left (p-1\right )\mathclose {}}} \equiv k \mod p by elementary equational reasoning as follows: \begin {aligned} k^{1+c\cdot {\mathopen {}\left (p-1\right )\mathclose {}}} &= k\cdot k^{c\cdot {\mathopen {}\left (p-1\right )\mathclose {}}} \\ &= k\cdot {\mathopen {}\left (k^{p-1}\right )\mathclose {}}^c \\ &\equiv k \cdot 1^c &&\mod p \\ &= k \end {aligned}
Lemma A good condition for encryption-decryption key pairs [01FD]
- October 31, 2026
- Jon Sterling
By Corollary [01FB], for prime p every pair {\mathopen {}\left (e,d\right )\mathclose {}} of positive integers such that e\cdot d \equiv 1 \mod p-1 is an encryption-decryption key pair modulo p in the sense that \forall k:\mathbb {Z}\mathpunct {.} k^{e\cdot d} \equiv k \mod p\text {.}
Proof.
- October 31, 2026
- Jon Sterling
That e\cdot d is congruent to 1 modulo p-1 is equivalent to the existence of some c:\mathbb {Z} such that e\cdot d = 1+c\cdot {\mathopen {}\left (p-1\right )\mathclose {}}:
\begin {aligned} &e\cdot d \equiv 1 \mod p - 1 \\ &\Longleftrightarrow {\mathopen {}\left (p-1\right )\mathclose {}}\mid {\mathopen {}\left (e\cdot d - 1\right )\mathclose {}} \\ &\Longleftrightarrow \exists c:\mathbb {Z}\mathpunct {.} c\cdot {\mathopen {}\left (p-1\right )\mathclose {}} = e\cdot d - 1 \\ &\Longleftrightarrow \exists c:\mathbb {Z}\mathpunct {.} 1+ c\cdot {\mathopen {}\left (p-1\right )\mathclose {}} = e\cdot d \end {aligned}Because d,e are positive, we know that e\cdot d - 1 is non-negative and hence any such c is also non-negative, so we have c\in \mathbb {N}.
Now, we fix an integer k to check that k^{e\cdot d} \equiv k\mod p, which follows below from Corollary [01FB]:
k^{e\cdot d} = k^{1+c\cdot {\mathopen {}\left (p-1\right )\mathclose {}}}\equiv k \mod p\text {.}{}Lemma A good source of encryption-decryption key pairs [01FE]
- October 31, 2026
- Marcelo Fiore, Jon Sterling
Let p be prime and let e be a positive integer coprime with p-1. Define d \coloneqq {\mathopen {}\left [\operatorname {lc}_2{\mathopen {}\left (p-1,e\right )\mathclose {}}\right ]\mathclose {}}_{p-1}\text {.}
Then for all integers k we have k^{e\cdot d}\equiv k \mod p.
Proof.
- October 31, 2026
- Marcelo Fiore, Jon Sterling
That e and p-1 are coprime means that their greatest common divisor is 1. Meanwhile, we recall the representation of greatest common divisors as integer linear combinations, so that we have:
\begin {aligned} 1 &= \gcd {\mathopen {}\left (p-1,e\right )\mathclose {}} \\ &= {\mathopen {}\left (p-1\right )\mathclose {}}\cdot \operatorname {lc}_1{\mathopen {}\left (p-1,e\right )\mathclose {}} + e\cdot \operatorname {lc}_2{\mathopen {}\left (p-1,e\right )\mathclose {}} \\ &\equiv {\mathopen {}\left (p-1\right )\mathclose {}}\cdot \operatorname {lc}_1{\mathopen {}\left (p-1,e\right )\mathclose {}} + e\cdot d &&\mod p-1 \\ &\equiv 0 \cdot \operatorname {lc}_1{\mathopen {}\left (p-1,e\right )\mathclose {}} + e\cdot d &&\mod p - 1 \\ &\equiv e\cdot d &&\mod p-1 \end {aligned}As e\cdot d\equiv 1 \mod p -1, we deduce via Lemma [01FD] that k^{e\cdot d}\equiv k for all integers k.
Example Shamir’s three-pass protocol [01F6]
- October 29, 2026
- Marcelo Fiore, Jon Sterling
Whereas Diffie-Hellman is a way for two actors to agree upon a secret key that neither of them started with, Shamir’s three-pass protocol is a way for one actor to securely send secret information to another. Here is how Alice would send a secret integer k<p to Bob without anyone else being able to intercept it:
- In public, Alice and Bob agree upon a prime p.
- In private, Alice choose a key pair (e_A,d_A) satisfying d_A\cdot e_A\equiv 1 \mod p-1. Bob similarly chooses a key pair (e_B,d_B) satisfying the same property.
- Alice encrypts k as the modular exponentiation {\mathopen {}\left [k^{e_A}\right ]\mathclose {}}_p, and sends it to Bob.
- Bob then encrypts the received value as {\mathopen {}\left [k^{e_A\cdot e_B}\right ]\mathclose {}}_p , and the result back to Alice.
- Alice decrypts the received value as {\mathopen {}\left [k^{e_A\cdot e_B\cdot d_A}\right ]\mathclose {}}_p and sends it back to Bob.
- Finally, Bob decrypts the received value as {\mathopen {}\left [k^{e_A\cdot e_B\cdot d_A\cdot d_B}\right ]\mathclose {}}_p \equiv k^{e_A\cdot e_B\cdot d_A\cdot d_B} \equiv {\mathopen {}\left (k^{e_A\cdot d_A}\right )\mathclose {}}^{e_B\cdot d_B} \equiv k\mod p\text {.} Because k<p, Bob’s final decrypted value is equal to k.
Lemma Encryption/decryption in RSA [01FF]
- October 31, 2026
- Marcelo Fiore, Jon Sterling
Let p,q be distinct primes and let d,e be positive integers such that e\cdot d \equiv 1 \mod {\mathopen {}\left (p-1\right )\mathclose {}}\cdot {\mathopen {}\left (q-1\right )\mathclose {}}. Then for all integers k, we have k^{e\cdot d} \equiv k \mod p\cdot q\text {.}
With what you are expected to know at this point, you would not be able to prove this theorem. I will cheat and use the Chinese Remainder Theorem, which we regrettably will not teach you in this course.
Proof.
- October 31, 2026
- Marcelo Fiore, Jon Sterling
By our assumptions that d,e are positive and that e\cdot d\equiv 1 \mod {\mathopen {}\left (p-1\right )\mathclose {}}\cdot {\mathopen {}\left (q-1\right )\mathclose {}}, we may choose a natural number c such that e\cdot d = 1 + c\cdot {\mathopen {}\left (p-1\right )\mathclose {}}\cdot {\mathopen {}\left (q-1\right )\mathclose {}}.
Because any two distinct primes are coprime, to verify k^{e\cdot d}\equiv k \mod p\cdot q it suffices by the Chinese Remainder Theorem to show that k^{e\cdot d}\equiv k \mod p and k^{e\cdot d}\equiv k \mod q. To that end, we make a few observations:
- If r\mid k, then k^{e\cdot d} \equiv k\mod r for any natural number r. This is immediate as e,d are positive.
- If p\nmid k then k^{e\cdot d}\equiv k \mod p, and similarly if q\nmid k then k^{e\cdot d}\equiv k\mod q. Without loss of generality, we consider just the first case, where we calculate using Fermat’s little theorem for integers: \begin {aligned} k^{e\cdot d} &= k^{1+c\cdot {\mathopen {}\left (p-1\right )\mathclose {}}\cdot {\mathopen {}\left (q-1\right )\mathclose {}}} \\ &= k\cdot {\mathopen {}\left (k^{p-1}\right )\mathclose {}}^{c\cdot {\mathopen {}\left (q-1\right )\mathclose {}}} \\ &\equiv k\cdot 1&&\mod p\\ &= k \end {aligned}
We now conclude that both k^{e\cdot d}\equiv k \mod p and k^{e\cdot d}\equiv k \mod q hold by cases on whether p\mid k and whether q\mid k.