RSA Encryption Unveiled: A Simplified Guide with a Toy Mathematical Example

Abhisheyk Gaur
3 min readMar 25, 2023

--

RSA encryption, named after its inventors Rivest, Shamir, and Adleman, is a widely used public key cryptosystem that provides secure communication between parties in the digital world. This asymmetric encryption algorithm relies on the mathematical principles of prime numbers and modular arithmetic, making it computationally infeasible to break when implemented correctly. In this introductory article, we’ll explain the basics of RSA encryption and walk you through a toy mathematical example to illustrate the core concepts.

Understanding RSA Encryption

RSA encryption operates on the principle of public and private keys, which are mathematically linked but practically impossible to derive from one another. The public key is used for encryption, while the private key is used for decryption. When a sender wants to securely transmit a message to the receiver, they encrypt the message using the receiver’s public key. Upon receiving the encrypted message, the receiver uses their private key to decrypt it.

The security of RSA encryption lies in the difficulty of factoring large composite numbers into their prime factors, a problem that remains computationally challenging even for modern computers.

A Toy Mathematical Example

Let’s walk through a simplified example of RSA encryption and decryption to better understand the underlying concepts.

  1. Key Generation
  • Choose two distinct prime numbers, p and q. For this example, let p = 5 and q = 7.
  • Compute n = p * q. In our example, n = 5 * 7 = 35.
  • Calculate the totient of n, φ(n) = (p — 1) * (q — 1). In our example, φ(35) = (5–1) * (7–1) = 24.
  • Choose an integer e, such that 1 < e < φ(n) and gcd(e, φ(n)) = 1. In our example, we can choose e = 5.
  • Compute the modular multiplicative inverse of e, which is d ≡ e⁻¹ (mod φ(n)). In our example, d = 29 (since 5 * 29 ≡ 1 (mod 24)).

Now we have the public key (n, e) = (35, 5) and the private key (n, d) = (35, 29).

  1. Encryption Suppose the sender wants to transmit a message, M = 10, to the receiver. The sender encrypts the message using the receiver’s public key (n, e) as follows:

C ≡ Mᵉ (mod n)

Plugging in our values, we get:

C ≡ 10⁵ (mod 35) C = 22

The encrypted message, C, is now 22.

  1. Decryption Upon receiving the encrypted message, the receiver uses their private key (n, d) to decrypt it:

M ≡ Cᵈ (mod n)

Using our example values, we get:

M ≡ 22²⁹ (mod 35), M = 10

The receiver successfully decrypts the message, obtaining the original value M = 10.

Conclusion

This toy mathematical example illustrates the basic concepts behind RSA encryption and decryption. In real-world applications, RSA employs much larger prime numbers, typically hundreds of digits long, which provide a higher level of security against brute-force attacks. RSA encryption has become an integral part of secure communication over the internet, including its use in secure web browsing (HTTPS) and email encryption (PGP).

Other Topics:

--

--

Abhisheyk Gaur
0 Followers

Abhisheyk Gaur - Principal Engineer @Amazon