๐ 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.
The Mathematics Behind RSA (Simplified)
RSA's security rests on the difficulty of factoring large composite numbers. In simple terms:
- Pick two very large prime numbers
pandq(e.g., 1024 bits each) - Compute
n = p ร q. This "n" is the modulus, part of both public and private keys - Choose a public exponent
e(usually 65537) - Compute the private exponent
dsuch that(m^e)^d โก m (mod n)
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 Scheme | Security | Recommended? |
|---|---|---|
| None (Textbook RSA) | โ Multiple attacks | Never use |
| PKCS#1 v1.5 | โ ๏ธ Known weaknesses | Avoid |
| 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?
| Parameter | 2048-bit | 4096-bit |
|---|---|---|
| Security level | ~112-bit equivalent | ~140-bit equivalent |
| Key generation time | ~1-3 seconds | ~10-30 seconds |
| Encryption speed | Fast | 4-8ร slower |
| Max plaintext size | ~190 bytes | ~446 bytes |
| NIST recommendation | Valid through 2030 | Long-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:
- Generate a random AES key
- Use AES to encrypt the actual data (fast, no length limit)
- Use RSA to encrypt that AES key (only 32 bytes โ RSA handles this easily)
- 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?
- ๐ Secure key exchange: Negotiating a shared key over an insecure channel (TLS handshake)
- โ๏ธ Digital signatures: Sign with your private key, anyone verifies with your public key โ proving the message is from you
- ๐ง Encrypted email: Encrypt email content with the recipient's public key
- ๐ SSH login: Generate a key pair locally, place your public key on the server, authenticate with your private key
- ๐ API authentication: Sign requests with a private key, server verifies with the public key โ JWT's RS256 algorithm
๐ Generate an RSA key pair now
2048/4096-bit, one-click generation and copy in PEM format
๐ Open RSA Encryption ToolFAQ
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.