Curious Minds

Clear answers to science and history questions

RSA Algorithm: How Public Key Encryption Works

· 6 min read · Science

The RSA algorithm works by using a pair of mathematically linked keys: a public key that anyone can use to encrypt a message, and a private key that only the intended recipient holds to decrypt it. The security relies on the practical difficulty of factoring the product of two large prime numbers, a problem that is easy to create but extremely hard to reverse without the private key.

Background: Why RSA Was Needed

Before public-key cryptography, secure communication required both parties to share a secret key in advance. This created a chicken-and-egg problem: how do you securely exchange the key over an insecure channel? RSA, developed in 1977 by Ron Rivest, Adi Shamir, and Leonard Adleman, solved this by introducing asymmetric cryptography. Instead of one shared secret, each person has two keys: one public, one private. The public key can be freely distributed, while the private key never leaves its owner's control. This breakthrough enabled secure communication between parties who have never met, and it underpins much of today's digital security, including HTTPS, email encryption, and digital signatures.

How RSA Key Generation Works

Creating an RSA key pair involves several mathematical steps. The security of the system depends on keeping certain values secret and choosing sufficiently large primes.

  1. Choose two distinct large prime numbers, typically called p and q. These primes are generated randomly and kept secret. Their size (often 1024 to 2048 bits each) determines the strength of the key.
  2. Compute the modulus n = p × q. This value n is part of both the public and private keys. Its length in bits (e.g., 2048 bits) is commonly used to describe the key size.
  3. Compute Euler's totient φ(n) = (p − 1)(q − 1). This counts the number of integers less than n that are coprime to n.
  4. Choose a public exponent e such that 1 < e < φ(n) and e is coprime to φ(n). A common choice is 65537, which is efficient and secure.
  5. Compute the private exponent d as the modular multiplicative inverse of e modulo φ(n). That is, d satisfies e × d ≡ 1 (mod φ(n)).
  6. Discard p, q, and φ(n) securely. The public key is (n, e), and the private key is (n, d).

The security comes from the fact that while n is public, factoring it back into p and q is believed to be computationally infeasible for large enough keys. Without p and q, an attacker cannot compute φ(n) and thus cannot derive d from e.

Encryption and Decryption Process

Once a key pair exists, anyone can encrypt a message using the recipient's public key. The message is first converted into an integer m that is less than n, typically using a padding scheme (see below). Then the ciphertext c is computed as:

c = me mod n

The recipient decrypts by computing:

m = cd mod n

This works because of the mathematical relationship between e and d modulo φ(n). The operations are efficient using modular exponentiation, even for very large numbers. However, RSA is slow compared to symmetric ciphers, so in practice it is often used to encrypt a symmetric key (like an AES key) rather than the entire message.

Padding Schemes and Their Importance

Raw RSA without padding is insecure. Padding adds random data to the message before encryption, ensuring that even identical messages produce different ciphertexts and preventing certain mathematical attacks. Common padding standards include:

  • PKCS#1 v1.5: An older standard, still widely supported but considered less secure than OAEP.
  • OAEP (Optimal Asymmetric Encryption Padding): A more secure padding scheme that uses a hash function and random seed, recommended for new applications.

Using improper padding can lead to vulnerabilities such as Bleichenbacher's attack, which exploits error messages to gradually decrypt data. Always use a well-vetted library that implements proper padding.

Digital Signatures with RSA

RSA can also be used for digital signatures, which provide authentication and integrity. To sign a message, the sender computes a hash of the message and then encrypts it with their private key. Anyone can verify the signature by decrypting it with the sender's public key and comparing the hash. This proves that the message was created by the holder of the private key and has not been altered. Digital signatures are crucial for certificates, software distribution, and secure email.

Key Size and Security Considerations

The strength of RSA depends on the size of the modulus n. As computing power increases, recommended key sizes grow. Current guidelines suggest:

  • 2048-bit keys: Minimum for new applications, considered secure until at least 2030.
  • 3072-bit keys: Recommended for longer-term security.
  • 4096-bit keys: Used in high-security contexts, though slower.

Keys smaller than 2048 bits are no longer considered safe for sensitive data. Additionally, RSA is vulnerable to quantum computers running Shor's algorithm, which could factor large numbers efficiently. This has spurred interest in post-quantum cryptography, but RSA remains secure against classical computers for now.

A frequent misconception is that RSA encrypts messages directly. In reality, it is almost always used to encrypt a symmetric key, which then encrypts the actual data. Another misconception is that larger keys are always better; beyond a certain size, the performance penalty outweighs the marginal security gain. Also, RSA should not be used without proper padding, as raw RSA is deterministic and insecure.

Frequently Asked Questions

Can RSA be broken?

No known classical method can efficiently break RSA with sufficiently large keys (2048 bits or more). However, small keys or flawed implementations can be vulnerable. Quantum computers could potentially break RSA in the future, but practical quantum computers of sufficient scale do not yet exist.

Why is RSA considered secure?

RSA's security relies on the difficulty of factoring the product of two large primes. While multiplying primes is easy, factoring the result is believed to be computationally infeasible for large numbers, even with the best known algorithms.

What is the difference between RSA and AES?

RSA is an asymmetric algorithm used for key exchange and digital signatures, while AES is a symmetric algorithm used for bulk data encryption. They are often used together: RSA to securely share an AES key, and AES to encrypt the data.

How long should an RSA key be?

For most applications, a 2048-bit key is sufficient until around 2030. For data that must remain secure for decades, 3072-bit or 4096-bit keys are recommended.

Is RSA still safe to use?

Yes, RSA with proper key sizes and padding is still considered secure for classical computers. However, organizations should plan for a transition to post-quantum algorithms as quantum computing advances.

What happens if I lose my private key?

If you lose your private key, you cannot decrypt messages encrypted with your public key, nor can you sign new messages. There is no recovery mechanism; you must generate a new key pair and inform correspondents.

In this article
  1. Background: Why RSA Was Needed
  2. How RSA Key Generation Works
  3. Encryption and Decryption Process
  4. Padding Schemes and Their Importance
  5. Digital Signatures with RSA
  6. Key Size and Security Considerations
  7. Common Misconceptions and Related Questions
  8. Frequently Asked Questions