Explainer
The math behind encryption: modular arithmetic and prime numbers
Public-key encryption such as RSA is built on two ideas from number theory: prime numbers, which every whole number factors into in exactly one way, and modular ("remainder") arithmetic.[1][2] RSA works modulo the product of two large primes, and its security rests on the unproven conjecture that factoring such a product is infeasible.[3][4] NIST's signature standard requires RSA moduli of at least 2048 bits.[5]
This is a prerequisite page in the cryptography course. It explains the number theory that RSA and similar systems use; it is not a guide to implementing cryptography. Read How encryption works first, then come back here before RSA and elliptic-curve cryptography. Choose a reading level in each section.
Prime numbers
A prime is a whole number greater than 1 that only 1 and itself divide evenly, such as 2, 3, 5, 7 and 11.[6] Every positive whole number can be broken into primes in exactly one way. For example, 12 is always two 2s and one 3, whatever order you write them in. This is why primes are called the building blocks of the integers.[1]
Primes get rarer as numbers get bigger. Near a number n, roughly one number in every ln n (the natural logarithm of n) is prime.[7]
The Fundamental Theorem of Arithmetic states that every positive integer has a unique prime factorization, up to ordering.[1] The Prime Number Theorem gives the density: about one integer in every ln n near n is prime. Legendre conjectured it in 1798, and it was proved in 1896.[7]
Modular arithmetic: arithmetic with remainders
Modular arithmetic is “clock arithmetic”. On a 12-hour clock, 15 o’clock is 3 o’clock, because only the remainder after dividing by 12 matters. Mathematicians say two numbers are congruent modulo n when they leave the same remainder after division by n. Gauss introduced the idea.[2]
A useful property is that you can take the remainder after every step of a long calculation. The numbers never grow beyond n, even if the calculation involves huge powers.[8] That is what makes the arithmetic in encryption practical on a computer.
a ≡ b (mod n) holds exactly when a and b have the same remainder on division by n.[2] Addition, subtraction and multiplication are compatible with reduction, so any chain of these operations can be reduced mod n at each step and stays in [0, n).[8] Division is the subtle part. A number has a multiplicative inverse mod n only in some cases. Mod 15, 8 has the inverse 2, but 3 has none.[9] Euler’s theorem, which governs remainders of large powers, underpins RSA and other current schemes.[10]
How RSA uses this
RSA was proposed in 1977 by three researchers at MIT.[11] Its big advance was that two people do not need to share a secret beforehand. The receiver publishes a public key that anyone can use to lock a message, and keeps a private key that unlocks it.[12]
To build the keys, the receiver picks two large secret primes and multiplies them. The product goes into the public key; the primes stay secret.[13] Messages are scrambled by raising them to a power and taking the remainder modulo that product.[3]
In FIPS 186-5, an RSA public key is (n, e), where n = pq for primes p and q. The primes p and q and the private exponent d must be kept secret.[13] RSA works by raising numbers to secret powers modulo n, which is why Euler’s theorem is central to it.[3] The standard requires an even modulus length of at least 2048 bits, with p and q each half that length.[5] It allows “probable primes” found with an approved probabilistic primality test such as Miller-Rabin.[14] The same standard no longer approves the older DSA for generating signatures.[15] Signatures are covered in How digital signatures work.
Why factoring is believed to be hard
Multiplying two big primes is quick. Going backwards, from the product to the two primes, is not: no efficient method is known. The best known method becomes infeasible for numbers of 300 digits or more.[16] Anyone who could factor the public number could work out the private key. RSA’s safety therefore rests on factoring staying hard.[4]
Finding big primes in the first place is much easier than factoring. Computers use fast tests that are wrong only with negligible probability.[17][14]
The fastest known classical factoring algorithm, the number field sieve, runs in time that grows faster than any polynomial in log n. It is infeasible once n has 300 or more digits.[16] Knowing p and q gives d directly, so RSA’s security rests on the factoring conjecture.[4] Primality testing is different. A deterministic polynomial-time test was published in 2002, but probabilistic tests remain the method used in practice.[17]
What is known and what is open
The rules of modular arithmetic and unique factorization are proved theorems.[1][2] That factoring is hard is a conjecture, not a proof.[16] Simple-sounding questions about primes also remain unsolved, such as the Twin Prime and Goldbach conjectures.[18]
Large quantum computers would change the factoring picture. How quantum computers break encryption explains why, and How post-quantum cryptography works covers the replacements, which rest on different mathematical problems.
Read next
Next in the crash course
Information theory explained: Shannon, bits and entropy
How Claude Shannon's 1948 theory measures information in bits, what entropy and channel capacity mean, why data compresses, and where the theory stops.
Questions readers ask
What is a prime number?
A whole number greater than 1 that is divisible only by itself and 1. Every positive whole number can be written as a product of primes in exactly one way, which is why primes are called the building blocks of the integers.[6][1]
What is modular arithmetic?
Arithmetic with remainders. Two numbers are congruent modulo n when they leave the same remainder after division by n. Because remainders can be taken at every step, the calculation never leaves the range 0 to n - 1.[2][8]
Why does RSA use prime numbers?
An RSA public key contains a modulus n that is the product of two secret primes p and q. Multiplying them is easy, but no efficient way to recover p and q from n is known, and anyone who knew them could compute the private key.[13][4][16]
How big are RSA keys?
NIST's Digital Signature Standard, FIPS 186-5, requires an RSA modulus of at least 2048 bits, with the two primes each half that length.[5]
Sources
· 18 claims from 2 publishers
Each numbered claim is a statement we checked against the sources listed with it. Tap a number in the text to see its sources in place.
- [1]
The Fundamental Theorem of Arithmetic says every positive integer has a unique prime factorization, so primes are the building blocks of the integers. reportedas of 2026-11-22
- Mathematics for Computer Science (MIT 6.042J course text, revised 2018) · MIT (Lehman, Leighton and Meyer; CC BY-SA 3.0) · Secondary source · 2018-06-06 · Section 9.4 (retrieved 2026-11-22)
- [2]
Gauss introduced the idea of congruence in Disquisitiones Arithmeticae; two numbers are congruent modulo n exactly when they leave the same remainder on division by n. reportedas of 2026-11-22
- Mathematics for Computer Science (MIT 6.042J course text, revised 2018) · MIT (Lehman, Leighton and Meyer; CC BY-SA 3.0) · Secondary source · 2018-06-06 · Section 9.6 (retrieved 2026-11-22)
- Mathematics for Computer Science (MIT 6.042J course text, revised 2018) · MIT (Lehman, Leighton and Meyer; CC BY-SA 3.0) · Secondary source · 2018-06-06 · Section 9.7 (retrieved 2026-11-22)
- [3]
RSA works modulo the product of two large primes and encrypts by raising the message to a power, which is why Euler's theorem is central to it. reportedas of 2026-11-22
- Mathematics for Computer Science (MIT 6.042J course text, revised 2018) · MIT (Lehman, Leighton and Meyer; CC BY-SA 3.0) · Secondary source · 2018-06-06 · Section 9.11 (retrieved 2026-11-22)
- Mathematics for Computer Science (MIT 6.042J course text, revised 2018) · MIT (Lehman, Leighton and Meyer; CC BY-SA 3.0) · Secondary source · 2018-06-06 · Section 9.11 (retrieved 2026-11-22)
- [4]
RSA's security rests on the conjecture that factoring the product of two primes with hundreds of digits is infeasible; anyone who knew the two primes could easily compute the private key. reportedas of 2026-11-22
- Mathematics for Computer Science (MIT 6.042J course text, revised 2018) · MIT (Lehman, Leighton and Meyer; CC BY-SA 3.0) · Secondary source · 2018-06-06 · Section 9.11 (retrieved 2026-11-22)
- Mathematics for Computer Science (MIT 6.042J course text, revised 2018) · MIT (Lehman, Leighton and Meyer; CC BY-SA 3.0) · Secondary source · 2018-06-06 · Section 9.11 (retrieved 2026-11-22)
- [5]
FIPS 186-5 requires an RSA modulus of at least 2048 bits, with p and q each half that length. confirmedas of 2026-11-22
- FIPS 186-5: Digital Signature Standard (DSS) · NIST · Primary source · 2023-02-03 · Section 5.1 (retrieved 2026-11-22)
- [6]
MIT's Mathematics for Computer Science course text defines a prime as a number greater than 1 that is divisible only by itself and 1. reportedas of 2026-11-22
- Mathematics for Computer Science (MIT 6.042J course text, revised 2018) · MIT (Lehman, Leighton and Meyer; CC BY-SA 3.0) · Secondary source · 2018-06-06 · Section 9.3, Definition 9.3.1 (retrieved 2026-11-22)
- [7]
Primes thin out gradually; by the Prime Number Theorem, about 1 integer in every ln n near n is prime. Legendre conjectured the theorem in 1798 and it was proved about a century later, in 1896. reportedas of 2026-11-22
- Mathematics for Computer Science (MIT 6.042J course text, revised 2018) · MIT (Lehman, Leighton and Meyer; CC BY-SA 3.0) · Secondary source · 2018-06-06 · Section 9.3 (retrieved 2026-11-22)
- Mathematics for Computer Science (MIT 6.042J course text, revised 2018) · MIT (Lehman, Leighton and Meyer; CC BY-SA 3.0) · Secondary source · 2018-06-06 · Section 9.3 (retrieved 2026-11-22)
- Mathematics for Computer Science (MIT 6.042J course text, revised 2018) · MIT (Lehman, Leighton and Meyer; CC BY-SA 3.0) · Secondary source · 2018-06-06 · Section 9.3 (retrieved 2026-11-22)
- [8]
When only the remainder modulo n of a chain of additions, subtractions and multiplications is needed, remainders can be taken at every step, so all the arithmetic stays among the numbers 0 to n - 1. reportedas of 2026-11-22
- Mathematics for Computer Science (MIT 6.042J course text, revised 2018) · MIT (Lehman, Leighton and Meyer; CC BY-SA 3.0) · Secondary source · 2018-06-06 · Section 9.7 (retrieved 2026-11-22)
- [9]
In arithmetic modulo n, some numbers have multiplicative inverses and others do not; for example, modulo 15, 2 is the inverse of 8 but 3 has no inverse. reportedas of 2026-11-22
- Mathematics for Computer Science (MIT 6.042J course text, revised 2018) · MIT (Lehman, Leighton and Meyer; CC BY-SA 3.0) · Secondary source · 2018-06-06 · Section 9.9 (retrieved 2026-11-22)
- Mathematics for Computer Science (MIT 6.042J course text, revised 2018) · MIT (Lehman, Leighton and Meyer; CC BY-SA 3.0) · Secondary source · 2018-06-06 · Section 9.9 (retrieved 2026-11-22)
- Mathematics for Computer Science (MIT 6.042J course text, revised 2018) · MIT (Lehman, Leighton and Meyer; CC BY-SA 3.0) · Secondary source · 2018-06-06 · Section 9.9 (retrieved 2026-11-22)
- [10]
RSA and other current encryption schemes involve computing remainders of numbers raised to large powers, which is where Euler's theorem about congruences comes in. reportedas of 2026-11-22
- Mathematics for Computer Science (MIT 6.042J course text, revised 2018) · MIT (Lehman, Leighton and Meyer; CC BY-SA 3.0) · Secondary source · 2018-06-06 · Section 9.10 (retrieved 2026-11-22)
- [11]
RSA was proposed in 1977 by Ronald Rivest, Adi Shamir and Leonard Adleman at MIT as a cryptosystem based on number theory. reportedas of 2026-11-22
- Mathematics for Computer Science (MIT 6.042J course text, revised 2018) · MIT (Lehman, Leighton and Meyer; CC BY-SA 3.0) · Secondary source · 2018-06-06 · Section 9.11 (retrieved 2026-11-22)
- [12]
In public-key systems such as RSA, the sender and receiver need not agree a secret key beforehand; the receiver publishes a public key and keeps a private key. reportedas of 2026-11-22
- Mathematics for Computer Science (MIT 6.042J course text, revised 2018) · MIT (Lehman, Leighton and Meyer; CC BY-SA 3.0) · Secondary source · 2018-06-06 · Section 9.11 (retrieved 2026-11-22)
- [13]
In NIST's Digital Signature Standard (FIPS 186-5), an RSA public key is a modulus n, the product of two primes p and q, plus a public exponent e; p, q and the private exponent d must be kept secret. confirmedas of 2026-11-22
- FIPS 186-5: Digital Signature Standard (DSS) · NIST · Primary source · 2023-02-03 · Section 5.1 (retrieved 2026-11-22)
- FIPS 186-5: Digital Signature Standard (DSS) · NIST · Primary source · 2023-02-03 · Section 5.1 (retrieved 2026-11-22)
- [14]
FIPS 186-5 allows RSA primes to be "probable primes", numbers judged prime by a probabilistic test such as Miller-Rabin with only a negligible chance of being composite. confirmedas of 2026-11-22
- FIPS 186-5: Digital Signature Standard (DSS) · NIST · Primary source · 2023-02-03 · Section 2.1, definition of probable prime (retrieved 2026-11-22)
- FIPS 186-5: Digital Signature Standard (DSS) · NIST · Primary source · 2023-02-03 · Appendix B.3 (retrieved 2026-11-22)
- FIPS 186-5: Digital Signature Standard (DSS) · NIST · Primary source · 2023-02-03 · Appendix B.3 (retrieved 2026-11-22)
- [15]
FIPS 186-5 no longer approves the older Digital Signature Algorithm (DSA) for generating signatures, though it may still be used to verify older ones. confirmedas of 2026-11-22
- FIPS 186-5: Digital Signature Standard (DSS) · NIST · Primary source · 2023-02-03 · Section 4 (retrieved 2026-11-22)
- FIPS 186-5: Digital Signature Standard (DSS) · NIST · Primary source · 2023-02-03 · Section 4 (retrieved 2026-11-22)
- [16]
No efficient way to factor the product of two large primes is known; the best known algorithm, the number field sieve, becomes infeasible for numbers of 300 digits or more, and efficient factoring is treated as an open problem. reportedas of 2026-11-22
- Mathematics for Computer Science (MIT 6.042J course text, revised 2018) · MIT (Lehman, Leighton and Meyer; CC BY-SA 3.0) · Secondary source · 2018-06-06 · Section 9.3, Conjectured Inefficiency of Factoring (retrieved 2026-11-22)
- Mathematics for Computer Science (MIT 6.042J course text, revised 2018) · MIT (Lehman, Leighton and Meyer; CC BY-SA 3.0) · Secondary source · 2018-06-06 · Section 9.3 (retrieved 2026-11-22)
- Mathematics for Computer Science (MIT 6.042J course text, revised 2018) · MIT (Lehman, Leighton and Meyer; CC BY-SA 3.0) · Secondary source · 2018-06-06 · Section 9.3 (retrieved 2026-11-22)
- Mathematics for Computer Science (MIT 6.042J course text, revised 2018) · MIT (Lehman, Leighton and Meyer; CC BY-SA 3.0) · Secondary source · 2018-06-06 · Section 9.3, Conjectured Inefficiency of Factoring (retrieved 2026-11-22)
- [17]
Testing whether a number is prime is much easier than factoring it. A polynomial-time primality test was published in 2002, but probabilistic tests remain the method used in practice. reportedas of 2026-11-22
- Mathematics for Computer Science (MIT 6.042J course text, revised 2018) · MIT (Lehman, Leighton and Meyer; CC BY-SA 3.0) · Secondary source · 2018-06-06 · Section 9.3 (retrieved 2026-11-22)
- Mathematics for Computer Science (MIT 6.042J course text, revised 2018) · MIT (Lehman, Leighton and Meyer; CC BY-SA 3.0) · Secondary source · 2018-06-06 · Section 9.3 (retrieved 2026-11-22)
- Mathematics for Computer Science (MIT 6.042J course text, revised 2018) · MIT (Lehman, Leighton and Meyer; CC BY-SA 3.0) · Secondary source · 2018-06-06 · Section 9.3 (retrieved 2026-11-22)
- [18]
Basic questions about primes remain open, including the Twin Prime Conjecture (infinitely many primes p with p + 2 also prime) and Goldbach's Conjecture (every even integer greater than two is the sum of two primes). reportedas of 2026-11-22
- Mathematics for Computer Science (MIT 6.042J course text, revised 2018) · MIT (Lehman, Leighton and Meyer; CC BY-SA 3.0) · Secondary source · 2018-06-06 · Section 9.3 (retrieved 2026-11-22)
- Mathematics for Computer Science (MIT 6.042J course text, revised 2018) · MIT (Lehman, Leighton and Meyer; CC BY-SA 3.0) · Secondary source · 2018-06-06 · Section 9.3, Goldbach's Conjecture (retrieved 2026-11-22)
- Mathematics for Computer Science (MIT 6.042J course text, revised 2018) · MIT (Lehman, Leighton and Meyer; CC BY-SA 3.0) · Secondary source · 2018-06-06 · Section 1.1, Goldbach's Conjecture (retrieved 2026-11-22)
- Mathematics for Computer Science (MIT 6.042J course text, revised 2018) · MIT (Lehman, Leighton and Meyer; CC BY-SA 3.0) · Secondary source · 2018-06-06 · Section 9.3 (retrieved 2026-11-22)
Revision history (1)
- Page created.
Created Nov 22, 2026. Last reviewed by an editor on Nov 22, 2026. Next scheduled review: Aug 16, 2027.
Cite this page
"The math behind encryption: modular arithmetic and prime numbers." ContentLora, updated Nov 22, 2026. https://contentlora.com/explain/modular-arithmetic-and-primes-explained
Spotted an error? Suggest a correction or emailcorrections@contentlora.com.