What Is RSA Encryption?
RSA (Rivest–Shamir–Adleman) is an asymmetric encryption scheme that relies on the mathematical difficulty of factoring large composite numbers. Unlike symmetric encryption, where one key both encrypts and decrypts, RSA uses a pair: a public key that anyone can access and a private key that remains secret.
When Alice wishes to send a secure message to Bob, she uses Bob's public key to encrypt it. Only Bob, holding the corresponding private key, can decrypt the message. This one-way property—easy to encrypt with the public key, but computationally infeasible to decrypt without the private key—makes RSA suitable for protecting everything from email to financial transactions.
The security of RSA depends on two large prime numbers. The product of these primes forms part of the public key, yet determining the original primes from their product remains extraordinarily difficult with current computing power, provided the primes are sufficiently large.
The RSA Algorithm: Key Generation Steps
Generating an RSA key pair involves four main steps:
- Select two distinct prime numbers (p and q): Choose primes with similar bit lengths to maximize security. For real-world use, these should each contain hundreds of digits.
- Compute the modulus (N): Multiply p and q together. This value forms part of both keys and determines the maximum message size.
- Calculate the Carmichael function (λ(N)): This is the least common multiple of (p − 1) and (q − 1). It governs which values can serve as encryption exponents.
- Choose the encryption exponent (e): Select an integer between 2 and λ(N) that shares no common factors with λ(N). The value 65,537 is standard in practice.
- Derive the decryption exponent (d): Calculate the modular multiplicative inverse of e modulo λ(N).
Your public key is the pair (e, N). Your private key is (d, N). Guard d carefully.
RSA Encryption and Decryption Formulas
Both encryption and decryption rely on modular exponentiation. The sender raises the plaintext to the power of the encryption exponent, while the recipient uses the decryption exponent to recover the original message.
N = p × q
λ(N) = lcm(p − 1, q − 1)
e × d ≡ 1 (mod λ(N))
C = M^e mod N
M = C^d mod N
p, q— Large distinct prime numbersN— Modulus; the product of p and qλ(N)— Carmichael function of N; the least common multiple of (p−1) and (q−1)e— Encryption exponent (public); chosen such that gcd(e, λ(N)) = 1d— Decryption exponent (private); the modular multiplicative inverse of e modulo λ(N)M— Plaintext message, a number less than NC— Ciphertext; the encrypted message
Common RSA Implementation Pitfalls
Mistakes in RSA deployment can completely undermine security, even if the mathematics is sound.
- Reusing a Key to Encrypt Different Messages — The same plaintext encrypted with the same public key always produces the same ciphertext. An attacker can detect repeated messages or patterns. Modern systems use padding schemes (like OAEP) that randomize each encryption, eliminating this vulnerability.
- Choosing a Small Encryption Exponent Without Padding — While e = 3 or e = 17 speeds up encryption, unpadded small messages can be recovered by taking cube roots or 17th roots of the ciphertext without modular reduction. Always use proper padding for any real deployment.
- Generating Weak Prime Numbers — Primes close together, primes with special structure, or primes generated carelessly allow factorization attacks. Use cryptographically secure random number generators and proven primality tests when selecting p and q.
- Neglecting the Secrecy of d — If the private exponent d leaks, the entire security of RSA collapses. An attacker can decrypt all past and future messages. Store d offline or in a hardware security module, and rotate keys immediately if compromise is suspected.
Why RSA Is Asymmetric Cryptography
In symmetric encryption (such as AES), both parties share a single secret key. This creates a distribution problem: before any secure communication, the key must be transmitted, risking interception.
RSA sidesteps this by using two mathematically linked keys. The public key can be published in a directory or embedded in a certificate, while the private key never leaves the owner's control. This eliminates the need to securely exchange keys beforehand.
The trade-off is speed. RSA is slower than symmetric encryption, so in practice, RSA often encrypts a symmetric key (like an AES session key) rather than large volumes of plaintext. This hybrid approach combines the convenience of public-key distribution with the efficiency of symmetric encryption.