Prime Numbers in CS
🔑 Prime Numbers in Computer Science & Cryptography
Prime numbers play a crucial role in computer science, particularly in the field of cryptography. While it's relatively easy for computers to determine if a number is prime or not, finding its prime factors is significantly more challenging. This asymmetry forms the foundation for many modern security protocols.
🛡️ Applications in Cryptography
The difficulty of prime factorization is a cornerstone for secure communication.
Diffie-Hellman Key Exchange: One application where prime numbers are utilized for secure key exchange.
RSA Algorithm: Stands for Rivest, Shamir, and Adelman, the developers of the algorithm. RSA is heavily reliant on prime numbers and is ubiquitous in internet security.
🌐 RSA and Public Key Cryptography
RSA is a type of public key cryptography, a system where encryption and decryption keys are different.
HTTPS: The secure version of HTTP, crucial for internet security, is built upon technologies like RSA.
Main Flavors:
RSA: The older, more established algorithm, based on prime numbers.
Elliptic Curve Cryptography (ECC): A more modern alternative, not directly based on prime numbers in the same way.
💻 Real-World Example: University Website Certificate
Examining a website's security certificate, such as a university's, reveals the use of RSA.
Padlock Icon: Indicates a secure connection (HTTPS).
Certificate Details: Accessible via browser settings, these show:
Algorithm: Often RSA.
Key Size: For example, 2048 bits.
Exponent: A public number, e.g., around 65,000.
Modulus: A massive number (e.g., 600 decimal digits long) displayed in hexadecimal. This modulus is not prime itself but is the product of two very large prime numbers (e.g., two 300-digit prime numbers).
The security of RSA relies on the fact that while the product of these two large prime numbers (the modulus) is public, the individual prime factors are unknown and extremely difficult to compute. Discovering these factors would allow unauthorized decryption.
⏰ Modular Arithmetic: The Foundation
To understand how RSA works, modular arithmetic is essential.
Concept: Modular arithmetic deals with remainders after division. It's like a clock that "wraps around" after a certain number.
Analog Clock Example: A 12-hour clock operates on modulo 12. 15 hours is 3 o'clock (15 mod 12 = 3). Similarly, -1 hour is 11 o'clock (-1 mod 12 = 11).
🔢 RSA in Action (Simplified Example with Modulo 91)
Let's use a simplified "91-hour clock" where 91 is the product of two small prime numbers: 7 and 13.
Encryption (Public Operation):
A sender chooses a secret number,
m, (e.g., 18) between 0 and 90.They raise
mto a public exponente(e.g., 5) modulo 91:c = m^e mod 91.For
m=18ande=5,18^5 mod 91 = 44. The resultc(44) is sent.
Decryption (Private Operation):
The receiver takes the received number
c(44) and raises it to a private exponentd(e.g., 29) modulo 91:m = c^d mod 91.For
c=44andd=29,44^29 mod 91 = 18. The result is the original secret numberm(18).
The private exponent
d(29) is a "magical" number that reverses the operation ofe(5) on the 91-hour clock. This means(m^e)^d mod 91 = m.
🔑 The Magic Behind the Numbers
Three key questions arise from this process:
Where does the magic number
d(29) come from?How does the computation work to reverse the operation?
Why is it secure?
1. 🧙♀️ Deriving the Private Key d
The private key d is derived using a different modular clock.
Constructing the
(p-1)(q-1)Clock:Start with the two prime factors
pandqof the modulusN(e.g., 7 and 13 forN=91).Calculate
(p-1) * (q-1)(e.g.,(7-1) * (13-1) = 6 * 12 = 72). This forms a "72-hour clock".
Choosing the Public Exponent
e:Select a prime number
ethat does not divide(p-1)(q-1)(e.g., 5 does not divide 72). Thiseis the public encryption exponent.
Finding the Private Exponent
d:Find a value
dsuch thate * d = 1on the(p-1)(q-1)clock (e.g.,5 * d = 1 mod 72).This
dis found using the Extended Euclidean Algorithm.For
e=5and modulo 72,dis 29 because5 * 29 = 145, and145 mod 72 = 1(145 = 2 * 72 + 1).This
d(29) is the private decryption exponent.
2. 🔄 How the Reversal Works
The core mathematical principle for the reversal is based on Fermat's Little Theorem.
We established that
e * d - 1is a multiple of(p-1)(q-1)(e.g.,5 * 29 - 1 = 144, which is a multiple of 72).This also means
e * d - 1is a multiple of(p-1)and(q-1)individually.Fermat's Little Theorem states that if
pis a prime number, then for any integeranot divisible byp,a^(p-1) ≡ 1 (mod p).Applying this, we can show that
m^(e*d - 1)simplifies to1on our clock, leavingm^1which is justm.Therefore,
(m^e)^d mod N = m^(e*d) mod N = m^(e*d - 1) * m mod N = 1 * m mod N = m. The original messagemis recovered.
3. 🔒 Why RSA is Secure
The security of RSA hinges on the computational difficulty of factoring large numbers.
The Problem: To find the magic number
d, one needs to know(p-1)(q-1). To calculate(p-1)(q-1), one must first know the prime factorspandqof the public modulusN.Asymmetry:
Generating Keys: The entity (e.g., University of Nottingham) generates two large prime numbers
pandq, multiplies them to getN, and then calculateseandd. This is computationally easy.Factoring
N: For an attacker, given onlyN(the product ofpandq), it is extremely difficult to findpandqwhen they are very large (e.g., 300 digits each).
Public Information:
Modulus
N: Publicly known (product ofpandq).Public Exponent
e: Publicly known (e.g., 65,537).
Secret Information:
Prime Factors
pandq: Kept secret.Private Exponent
d: Kept secret, derivable only ifpandqare known.
Key Size: A 2048-bit key size for
Nmeans searching fordwould involve an astronomical number of operations, making brute-force attacks infeasible with current technology.
The core of RSA's security lies in the fact that while multiplication is easy, factorization is hard. This allows for public key distribution while keeping the private key secure.

Ask anything
We've read this whole study set, so questions about it get the best answers.