Lecture 26: The RSA Algorithm (Part 2): The Math of Encryption and Decryption

Clean infographic flowchart showing the RSA encryption and decryption process. On the left, 'Bob' has a message 'M'. An arrow points to a box labeled 'Encryption' with the formula C = M^e mod n and a 'Public Key' icon. The arrow continues to the resulting 'Ciphertext C'. A dashed line shows 'C' traveling across an insecure channel. On the right, 'Alice' receives 'C'. An arrow points to a box labeled 'Decryption' with the formula M = C^d mod n and a 'Private Key' icon. The final arrow points to the original message 'M'. Use a simple, modern, educational style.

Sequentia Explores: The Mathematics of Crypto

Part III: Historical Ciphers & the Dawn of Cryptanalysis

Lecture 26: The RSA Algorithm (Part 2): The Math of Encryption and Decryption

In our last lecture, we followed Alice through the five steps of forging an RSA key pair. She started with two secret prime numbers (p and q) and, using our mathematical toolkit, constructed:

  • A Public Key (n, e), which she can share with the world.
  • A Private Key (n, d), which she keeps absolutely secret.

Today, we put these keys to work. We’ll see how Bob can use Alice’s public key to encrypt a message, and how Alice uses her private key to get the original message back. This is where the magic of modular exponentiation and Euler’s Theorem comes to life.

Preparing the Message

First, the message must be converted into a number. Real-world systems use standardized “padding schemes” for this, which are crucial for security, but for our simple example, let’s just use a basic numerical representation.

Let’s say Bob wants to send the message “HI” to Alice.

  • We can represent ‘H’ as 08 and ‘I’ as 09.
  • Let’s combine them into a single number: M = 0809 (or just 809).
  • The only rule is that our message number M must be less than our modulus n. Since 809 < 3233, we are good to go.

The Encryption Process (Done by Bob)

Bob has Alice’s Public Key (n, e). In our running example, this is (3233, 17).

To encrypt his message M, Bob performs a single modular exponentiation calculation. He computes the ciphertext C using the following formula:

C ≡ M^e (mod n)

  • For our example:
    • Bob needs to calculate C ≡ 809^17 (mod 3233).
    • This looks like a monstrous calculation! But as we learned in Lecture 12, computers can use modular exponentiation (like exponentiation by squaring) to solve this efficiently without ever calculating the full value of 809^17.
    • Plugging this into a calculator (or a computer program), we find:
    • C = 2790

Bob has now encrypted his message. The plaintext M = 809 has been transformed into the ciphertext C = 2790. He sends this number, 2790, to Alice over the insecure channel. Eve can see it, but it’s just a number. Without Alice’s private key, it’s computationally infeasible for Eve to reverse this operation and find the original message M.

The Decryption Process (Done by Alice)

Alice receives the ciphertext C = 2790. She now uses her Private Key (n, d). In our example, this is (3233, 2753).

To decrypt the ciphertext C, Alice performs a similar modular exponentiation, but this time using her private exponent d:

M ≡ C^d (mod n)

  • For our example:
    • Alice needs to calculate M ≡ 2790^2753 (mod 3233).
    • Again, this is an enormous calculation that is only feasible because of efficient modular exponentiation algorithms.
    • When Alice’s computer performs this calculation, it gets the result:
    • M = 809

Success! Alice has recovered the original message number, 809, which she can easily translate back to “HI”. The system worked.

Why Does This Work? The Magic of Euler’s Theorem

This isn’t magic; it’s a beautiful application of the number theory we’ve been studying. Let’s see a sketch of the proof.

  1. We start with the decryption of the encrypted message:
    C^d ≡ (M^e)^d ≡ M^(e×d) (mod n)
  2. Remember from our key generation step how we chose d? We chose it specifically so that e × d ≡ 1 (mod φ(n)).
  3. This means that e × d is equal to 1 plus some multiple of φ(n). We can write this as:
    e × d = 1 + k × φ(n) for some integer k.
  4. Now we can substitute this back into our exponent:
    M^(e×d) ≡ M^(1 + k × φ(n)) (mod n)
  5. Using the rules of exponents, this is the same as:
    M^1 × (M^φ(n))^k (mod n)
  6. Now, look at the part in the parentheses: M^φ(n). Euler’s Theorem (Lecture 14) tells us that if M and n are coprime, then M^φ(n) ≡ 1 (mod n).
  7. Substituting this 1 into our equation gives us:
    M × (1)^k (mod n)
  8. And 1 to any power k is just 1. So, we are left with:
    M × 1 (mod n)
    which simplifies to just M (mod n).

And there it is! The mathematics, guaranteed by Euler’s Theorem, ensures that encrypting with e and then decrypting with d will always return the original message M.

The entire security of RSA relies on the fact that Alice can easily calculate Ï†(n) to find her decryption key d, while for everyone else, calculating Ï†(n) (and thus d) is as hard as factoring the public number n.

This two-part look at RSA demonstrates the complete lifecycle of a public-key cryptosystem: using number theory to forge a key pair with a built-in mathematical trapdoor, and then using modular exponentiation to perform the one-way encryption and the secret “trapdoor” decryption.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top