RSA Encryption and Prime Numbers

Modern encryption relies on the fact that some math problems are easy to do but hard to undo. One of the most famous examples is RSA encryption, which is built on the mathematics of prime numbers and modular exponentiation.

At its core, encryption is the process of converting readable information, called "plaintext," into an unreadable format known as "ciphertext" to protect it from unauthorized access. Think of it as putting your data in a locked box that can only be opened with the right key. This transformation uses mathematical algorithms combined with secret keys to scramble data in a way that makes it meaningless to anyone who doesn't possess the corresponding decryption key.

The fundamental principle behind modern cryptographic systems is mathematical asymmetry - certain mathematical operations are computationally easy to perform in one direction but extremely difficult to reverse without additional information. For instance, multiplying two large prime numbers together is straightforward, but factoring the resulting product back into its original prime components becomes practically impossible as the numbers grow larger. This mathematical one-way property forms the foundation of cryptographic security.

There are two primary approaches to encryption. Symmetric encryption uses the same key for both encrypting and decrypting data. While this method is fast and efficient - making it ideal for encrypting large amounts of data - it presents the challenge of securely sharing the secret key between communicating parties. Common symmetric algorithms include the Advanced Encryption Standard (AES), which secures everything from your Wi-Fi connection to government communications.

Asymmetric encryption, on the other hand, employs a mathematically related pair of keys: a public key that can be freely shared and a private key that must be kept secret. Data encrypted with the public key can only be decrypted with the corresponding private key, and vice versa. This elegant solution resolves the key distribution problem inherent in symmetric systems, though it operates more slowly and is typically used for smaller amounts of data or for securely exchanging symmetric keys.

Encryption has become the invisible foundation of our digital world. It protects your online banking transactions, secures your messaging applications, safeguards stored passwords, and enables secure web browsing - that familiar "https" in web addresses indicates that encryption is protecting your connection. Without these mathematical safeguards, digital communication and commerce as we know them would be impossible, leaving sensitive information vulnerable to interception and theft by malicious actors.

What Is a Prime Number?

A prime number is a number greater than 1 that has no positive divisors other than 1 and itself.

Examples: \[ 2, 3, 5, 7, 11, 13, 17, 19, 23, \ldots \]

Composite numbers are numbers that have more than two factors (e.g., 4, 6, 8, 9, 10...).

Why Primes Matter in Cryptography

When you multiply two large prime numbers together, the result is easy to compute. But doing the reverse - factoring that large number back into primes - is extremely difficult. This "one-way" nature is what makes RSA encryption secure.

How RSA Works (Overview)

RSA uses three key ideas:

  1. Pick two large prime numbers: \( p \) and \( q \)
  2. Compute their product \( n = p \cdot q \) and a value called \( \phi(n) = (p-1)(q-1) \)
  3. Select two numbers: a public exponent \( e \) and a private exponent \( d \)

The public key is \( (e, n) \). The private key is \( (d, n) \).

Step-by-Step RSA Example (With Small Numbers)

Let's encrypt the message "I LOVE YOU" using small primes for clarity.

1. Choose Two Prime Numbers

Let's pick: \[ p = 17, \quad q = 11 \] Then: \[ n = p \cdot q = 187 \] \[ \phi(n) = (p - 1)(q - 1) = 16 \cdot 10 = 160 \]

2. Choose Public Key \( e \)

We choose \( e = 7 \), which satisfies: \[ 1 < e < \phi(n), \quad \gcd(e, \phi(n)) = 1 \]

3. Compute Private Key \( d \)

We find \( d \) such that: \[ e \cdot d \equiv 1 \mod \phi(n) \Rightarrow 7 \cdot d \equiv 1 \mod 160 \] Two possible methods to find \( d \):

1) Trial and Error (for small numbers)

Therefore \( d = 23 \)

2) Using the extended Euclidean algorithm method.

We need to find \( d \) suchn that \[ 7 \cdot d \equiv 1 \mod 160 \quad (I) \] We also know (from above ) that \[ \gcd(7, 160) = 1 \] We can therefore write equation (I) as \[ 7 \cdot d \equiv \gcd(7, 160) \mod 160 \quad (II) \] which equivalent to finding

\( d \) and \( k \) such that \[ 7 \cdot d + 160 k = \gcd(7, 160) \quad (III) \]

Step 1: Apply the Euclidean algorithm to find gcd(7, 160)

Divide 160 by 7 and write: \[ 160 = 7 \times 22 + 6 \] Divide the last quotient 7 by the last remainder 6 and write: \[ 7 = 6 \times 1 + 1 \] Divide the last quotient 6 by the last remainder 1 and write: \[ 6 = 1 \times 6 + 0 \]
The remainder is zero, we therefore stop.

Step 2: Work backwards to express \( 1 \) as a linear combination using the equations \( 7 = 6 \times 1 + 1 \) and \( 160 = 7 \times 22 + 6 \) found above:

Substitute the second equation into the first:

Step 3: Extract the result

From \(7 \times 23 - 160 \times 1 = 1 \), we can see that:

\( 7 \times 23 = 1 (mod 160) \)

Therefore, \[ d = 23 \].

4. Summary of Keys

Encrypting a Message

We use a simple letter-to-number conversion:

A = 01, B = 02, ..., Z = 26, space = 00

Message: "I LOVE YOU" becomes: \[ 09,\ 00,\ 12,\ 15,\ 22,\ 05,\ 00,\ 25,\ 15,\ 21 \]

Encrypt each number \( M \) using: \[ C = M^e \mod n \] With \( e = 7 \) and \( n = 187 \)

Encrypted values (using modular exponentiation):

Encrypted message: \[ [84, 0, 95, 137, 47, 106, 0, 116, 137, 154] \]

Decrypting the Message

To decrypt, use: \[ M = C^d \mod n \quad \text{(with } d = 23,\ n = 187\text{)} \]

Why does this work?

In RSA, encryption is: \[ C = M^e \mod n \]

Decryption is: \[ M = C^d \mod n \] So: \[ M = (M^e)^d \mod n = M^{ed} \mod n \]

If we set up \( e \cdot d \equiv 1 \mod \phi(n) \), Euler's theorem guarantees that this equation brings us back to the original message: \[ M^{ed} \equiv M \mod n \]

This is what makes RSA work: raising to the power \( e \) encrypts, and raising to power \( d \) decrypts - because of the math of modular exponentiation and number theory.

Why RSA Is Secure

Even if someone knows \( n = 187 \) and \( e = 7 \), they can't easily decrypt the message without knowing \( d \). To find \( d \), they'd have to factor 187 into \( 17 \cdot 11 \), then compute \( \phi(n) \) and solve for \( d \). That's easy here - but for a 2048-bit key, it would take billions of years.

RSA Encryption Tool

Encrypt a Message

Enter your message (supports letters, numbers, and special characters) and RSA parameters:




Encryption Results:

Enter a message and click encrypt to see results...

RSA Decryption Tool

Decrypt a Message

Enter the encrypted numbers (comma-separated) and RSA parameters:




Decryption Results:

Enter encrypted numbers and click decrypt to see results...

Practice Problems

Questions
  1. Is 29 a prime number? What about 91?
  2. Use \( p = 13 \), \( q = 11 \) to compute \( n \) and \( \phi(n) \).
  3. Choose a public key \( e = 7 \). Find a private key \( d \) so that \( e \cdot d \equiv 1 \mod \phi(n) \).
  4. Encrypt the number 4 using your keys and RSA.
  5. Decrypt your result to recover the original number.

Important Security Note

These questions use small numbers for educational clarity. Real RSA implementations use much larger primes (1024-4096 bits) corresponding to decimals in the range 309 to 124 digits and additional security measures to prevent various attacks.

Solutions

Question 1: Is 29 a prime number? What about 91?

For 29:

To check if 29 is prime, we need to test divisibility by all primes up to \(\sqrt{29} \approx 5.4\).

Testing divisors: 2, 3, 5

\(29 \div 2 = 14.5\) (not divisible)
\(29 \div 3 = 9.67...\) (not divisible)
\(29 \div 5 = 5.8\) (not divisible)
Answer: 29 is prime.

For 91:

Testing divisors: 2, 3, 5, 7 (since \(\sqrt{91} \approx 9.5\))

\(91 \div 2 = 45.5\) (not divisible)
\(91 \div 3 = 30.33...\) (not divisible)
\(91 \div 5 = 18.2\) (not divisible)
\(91 \div 7 = 13\) ?

Since \(91 = 7 \times 13\),

91 is not prime.

Question 2: Use \(p = 13\), \(q = 11\) to compute \(n\) and \(\phi(n)\)

Computing \(n\):

\[n = p \times q = 13 \times 11 = 143\]

Computing \(\phi(n)\):

\[\phi(n) = (p-1) \times (q-1) = (13-1) \times (11-1) = 12 \times 10 = 120\]
Answer: \(n = 143\), \(\phi(n) = 120\)

Question 3: Choose a public key \(e = 7\). Find a private key \(d\) so that \(e \cdot d \equiv 1 \pmod{\phi(n)}\)

We need to find \(d\) such that:

\[7 \cdot d \equiv 1 \pmod{120}\]

First, verify that \(\gcd(7, 120) = 1\):

\(120 = 7 \times 17 + 1\)
\(7 = 1 \times 7 + 0\)

So \(\gcd(7, 120) = 1\) ?

Using Extended Euclidean Algorithm:

Forward pass:

\(120 = 7 \times 17 + 1\)
\(7 = 1 \times 7 + 0\)

Backward pass:

\(1 = 120 - 7 \times 17\)

This gives us: \(7 \times (-17) + 120 \times 1 = 1\)

Therefore: \(7 \times (-17) \equiv 1 \pmod{120}\)

Since \(-17 < 0\), we add 120: \(d = -17 + 120 = 103\)

Verification:

\[7 \times 103 = 721 = 6 \times 120 + 1 \equiv 1 \pmod{120} \text{ ?}\]
Answer: \(d = 103\)

Question 4: Encrypt the number 4 using your keys and RSA

RSA Encryption Formula:

\[c \equiv m^e \pmod{n}\]

Where:

\(m = 4\) (plaintext)
\(e = 7\) (public exponent)
\(n = 143\)

Calculation:

\[c \equiv 4^7 \pmod{143}\]

Computing \(4^7\):

\(4^1 = 4\)
\(4^2 = 16\)
\(4^3 = 64\)
\(4^4 = 256 \equiv 256 - 143 = 113 \pmod{143}\)
\(4^5 = 4 \times 113 = 452 \equiv 452 - 3 \times 143 = 452 - 429 = 23 \pmod{143}\)
\(4^6 = 4 \times 23 = 92 \pmod{143}\)
\(4^7 = 4 \times 92 = 368 \equiv 368 - 2 \times 143 = 368 - 286 = 82 \pmod{143}\)
Answer: The encrypted message is \(c = 82\)

Question 5: Decrypt your result to recover the original number

RSA Decryption Formula:

\[m \equiv c^d \pmod{n}\]

Where:

\(c = 82\) (ciphertext)
\(d = 103\) (private exponent)
\(n = 143\)

Calculation:

\[m \equiv 82^{103} \pmod{143}\]

This is a large computation, so we'll use modular exponentiation by repeated squaring:

\[82^{103} = 82^{(64+32+4+2+1)} = 82^{64} \times 82^{32} \times 82^4 \times 82^2 \times 82^1\]

Computing powers of 82 mod 143:

\(82^1 \equiv 82 \pmod{143}\)
\(82^2 \equiv 6724 \equiv 6724 - 47 \times 143 = 6724 - 6721 = 3 \pmod{143}\)
\(82^4 \equiv 3^2 = 9 \pmod{143}\)
\(82^8 \equiv 9^2 = 81 \pmod{143}\)
\(82^{16} \equiv 81^2 = 6561 \equiv 6561 - 45 \times 143 = 6561 - 6435 = 126 \pmod{143}\)
\(82^{32} \equiv 126^2 = 15876 \equiv 15876 - 111 \times 143 = 15876 - 15873 = 3 \pmod{143}\)
\(82^{64} \equiv 3^2 = 9 \pmod{143}\)

Therefore:

\[82^{103} \equiv 9 \times 3 \times 9 \times 3 \times 82 \equiv 81 \times 9 \times 82 \equiv 729 \times 82 \pmod{143}\]

Simplifying:

\(729 \equiv 729 - 5 \times 143 = 729 - 715 = 14 \pmod{143}\)
\(14 \times 82 = 1148 \equiv 1148 - 8 \times 143 = 1148 - 1144 = 4 \pmod{143}\)
Answer: The decrypted message is \(m = 4\)

Summary

The RSA encryption/decryption process successfully worked:

Original message: 4
Encrypted: 4 ? 82
Decrypted: 82 ? 4

The message was successfully recovered, confirming our RSA implementation is correct.

Key RSA Parameters Used:

\(p = 13\), \(q = 11\) (prime numbers)
\(n = 143\) (modulus)
\(\phi(n) = 120\) (Euler's totient)
\(e = 7\) (public exponent)
\(d = 103\) (private exponent)
Public Key: \((n, e) = (143, 7)\)
Private Key: \((n, d) = (143, 103)\)