Encrypt or decrypt payloads using secure RSA key pairs.
RSA encryption allows anyone to encrypt a message using the recipient's Public Key. However, only the recipient, holding the mathematically linked Private Key, can decrypt and read the message.
Raw ("textbook") RSA is deterministic and highly vulnerable to chosen-ciphertext attacks. To fix this, padding schemes introduce randomness before encryption.
A common misconception is that RSA can encrypt files. RSA cannot encrypt data larger than its key size (minus padding overhead). For a 2048-bit key using OAEP with SHA-256, the maximum payload is just 190 bytes!
Real-world usage: Instead of encrypting the actual file with RSA, systems use Hybrid Encryption. They generate a random symmetric key (like AES-256), encrypt the large file rapidly with AES, and then use RSA to encrypt only the AES key.
const encrypted = await crypto.subtle.encrypt(
{
name: "RSA-OAEP" // Required modern padding
},
publicKey,
encodedData
);If you have a 1GB video file that you want to send securely to Alice, how should you use RSA?