1. Home
  2. Explore
  3. Computer Science
  4. Prime Numbers in CS

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

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.

  1. Encryption (Public Operation):

    • A sender chooses a secret number, m, (e.g., 18) between 0 and 90.

      • They raise m to a public exponent e (e.g., 5) modulo 91: c = m^e mod 91.

      • For m=18 and e=5, 18^5 mod 91 = 44. The result c (44) is sent.

  2. Decryption (Private Operation):

    • The receiver takes the received number c (44) and raises it to a private exponent d (e.g., 29) modulo 91: m = c^d mod 91.

      • For c=44 and d=29, 44^29 mod 91 = 18. The result is the original secret number m (18).

The private exponent d (29) is a "magical" number that reverses the operation of e (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:

  1. Where does the magic number d (29) come from?

  2. How does the computation work to reverse the operation?

  3. 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 p and q of the modulus N (e.g., 7 and 13 for N=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 e that does not divide (p-1)(q-1) (e.g., 5 does not divide 72). This e is the public encryption exponent.

  • Finding the Private Exponent d:

    • Find a value d such that e * d = 1 on the (p-1)(q-1) clock (e.g., 5 * d = 1 mod 72).

    • This d is found using the Extended Euclidean Algorithm.

    • For e=5 and modulo 72, d is 29 because 5 * 29 = 145, and 145 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 - 1 is a multiple of (p-1)(q-1) (e.g., 5 * 29 - 1 = 144, which is a multiple of 72).

  • This also means e * d - 1 is a multiple of (p-1) and (q-1) individually.

  • Fermat's Little Theorem states that if p is a prime number, then for any integer a not divisible by p, a^(p-1) ≡ 1 (mod p).

  • Applying this, we can show that m^(e*d - 1) simplifies to 1 on our clock, leaving m^1 which is just m.

  • 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 message m is 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 factors p and q of the public modulus N.

  • Asymmetry:

    • Generating Keys: The entity (e.g., University of Nottingham) generates two large prime numbers p and q, multiplies them to get N, and then calculates e and d. This is computationally easy.

    • Factoring N: For an attacker, given only N (the product of p and q), it is extremely difficult to find p and q when they are very large (e.g., 300 digits each).

  • Public Information:

    • Modulus N: Publicly known (product of p and q).

    • Public Exponent e: Publicly known (e.g., 65,537).

  • Secret Information:

    • Prime Factors p and q: Kept secret.

    • Private Exponent d: Kept secret, derivable only if p and q are known.

  • Key Size: A 2048-bit key size for N means searching for d would 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.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent