๐Ÿ”‘ RSA Asymmetric Encryption: The Magic of Two Keys

Understanding public/private key pairs, RSA-OAEP padding, and how to securely generate and use RSA keys

What Is Asymmetric Encryption?

The defining feature of asymmetric encryption is two different keys: a public key and a private key. The public key can be shared with anyone and is used to encrypt data. The private key must be kept strictly secret and is used to decrypt data.

This is fundamentally different from AES โ€” where encryption and decryption use the same key (symmetric). Asymmetric encryption solves the "key distribution" problem: how do you securely deliver a key to someone? With RSA, you only need the recipient's public key to encrypt, and their private key never needs to leave their device.

Analogy: The public key is like a mailbox address โ€” anyone can drop a letter in. The private key is like the mailbox key โ€” only the key holder can open it and read the letters inside.

The Mathematics Behind RSA (Simplified)

RSA's security rests on the difficulty of factoring large composite numbers. In simple terms:

Encryption is c = m^e mod n, decryption is m = c^d mod n. The core challenge: if you only know n (which is public), factoring it to recover p and q requires splitting a 2048-bit integer โ€” with current computing power, this would take longer than the age of the universe.

RSA-OAEP: Why You Can't Use "Plain" RSA

The textbook "RSA encryption = m^e mod n" is just the foundation โ€” using it directly has serious security flaws. Real-world RSA always includes a padding scheme. OAEP (Optimal Asymmetric Encryption Padding) is the current standard.

Padding SchemeSecurityRecommended?
None (Textbook RSA)โŒ Multiple attacksNever use
PKCS#1 v1.5โš ๏ธ Known weaknessesAvoid
OAEP (SHA-256)โœ… Secureโœ… Recommended

OAEP introduces random padding before encryption, ensuring the same plaintext produces different ciphertext every time and preventing multiple mathematical attacks. The browser Web Crypto API supports RSA-OAEP natively:

// RSA-OAEP encryption with Web Crypto API
const pubKey = await crypto.subtle.importKey(
  'spki', pemToArrayBuffer(pem),
  { name: 'RSA-OAEP', hash: 'SHA-256' },
  false, ['encrypt']
);
const ciphertext = await crypto.subtle.encrypt(
  { name: 'RSA-OAEP' }, pubKey, new TextEncoder().encode(plaintext)
);

2048-bit vs 4096-bit: Which to Choose?

Parameter2048-bit4096-bit
Security level~112-bit equivalent~140-bit equivalent
Key generation time~1-3 seconds~10-30 seconds
Encryption speedFast4-8ร— slower
Max plaintext size~190 bytes~446 bytes
NIST recommendationValid through 2030Long-term security

General recommendation: 2048-bit is sufficient for most scenarios. If you need longer-term security (e.g., encrypting data that must remain secure for 10+ years), or your security policy mandates it, choose 4096-bit. Modern browsers generate 2048-bit keys in 1-3 seconds; 4096-bit takes 10-30 seconds.

RSA's Length Limit and the Solution

A core limitation of RSA: the plaintext cannot exceed the key size minus padding overhead. With 2048-bit RSA-OAEP (SHA-256), you can encrypt at most ~190 bytes. If you need to encrypt an image or a long document, RSA alone won't work.

The standard solution โ€” Hybrid Encryption:

  1. Generate a random AES key
  2. Use AES to encrypt the actual data (fast, no length limit)
  3. Use RSA to encrypt that AES key (only 32 bytes โ€” RSA handles this easily)
  4. Send the RSA-encrypted key together with the AES-encrypted data

This is exactly how HTTPS, PGP, SSH, and virtually every modern encryption protocol works. RSA securely delivers the key; AES efficiently encrypts the data โ€” they complement each other.

When Should You Use RSA?

๐Ÿ‘‰ Generate an RSA key pair now

2048/4096-bit, one-click generation and copy in PEM format

๐Ÿ”‘ Open RSA Encryption Tool

FAQ

Q: Can the public key be used to derive the private key?

No. Deriving the private key (exponent d) from the public key (modulus n and exponent e) requires knowing the prime factors p and q. Factoring a 2048-bit integer would take all the world's computing power longer than the age of the universe. However, note that quantum computers (running Shor's algorithm) can factor large integers in polynomial time โ€” RSA is not "post-quantum secure."

Q: Why is the public exponent e usually 65537?

65537 (binary: 10000000000000001) is prime and has only two '1' bits. This makes encryption (m^e mod n) extremely fast via the square-and-multiply algorithm. 65537 was chosen over 3 (an even smaller option) to prevent low-exponent attacks. It represents the optimal balance between performance and security.

Q: How often should RSA keys be rotated?

NIST recommends 2048-bit RSA keys remain safe through 2030. For high-security applications, rotate annually; for general use, every 2-3 years is reasonable. Critical point: if you suspect a private key has been compromised, rotate immediately โ€” RSA keys lack forward secrecy; a leaked private key can decrypt all historical ciphertexts.