Node.js Reference ยท crypto
Node.js crypto.publicEncrypt() function
Reference for crypto.publicEncrypt() and privateDecrypt() in Node.js: RSA-OAEP encryption of small payloads, padding options, size limits and why hybrid encryption is better.
crypto.publicEncrypt(key, buffer) encrypts a small piece of data with an RSA public key so that only the holder of the matching private key can read it, using crypto.privateDecrypt(key, buffer). There is also the reverse pair, privateEncrypt() and publicDecrypt(), which exist for legacy signature-like uses; use crypto.sign() for signatures instead.
RSA can only encrypt data smaller than its key size minus padding overhead (190 bytes for a 2048-bit key with OAEP and SHA-256), so in practice you encrypt a random symmetric key with RSA and the real data with AES-GCM. That is called hybrid encryption.
import { publicEncrypt, privateDecrypt } from 'node:crypto'; const encrypted = publicEncrypt(publicKey, Buffer.from('small secret')); const plain = privateDecrypt(privateKey, encrypted);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
crypto.publicEncrypt(key, buffer): Buffer crypto.privateDecrypt(key, buffer): Buffer crypto.privateEncrypt(key, buffer): Buffer crypto.publicDecrypt(key, buffer): Buffer
| Name | Description |
|---|---|
key |
A KeyObject, PEM string or buffer, or an object { key, passphrase?, padding?, oaepHash?, oaepLabel? }. |
buffer |
The data to encrypt or decrypt (Buffer, typed array, DataView or string). |
padding |
constants.RSA_PKCS1_OAEP_PADDING (default for encrypt and decrypt), RSA_PKCS1_PADDING or RSA_NO_PADDING. |
oaepHash |
Hash for OAEP and its mask function. Default 'sha1'; use 'sha256' for new designs. |
oaepLabel |
Optional extra label bound into OAEP; must match on both sides. |
Returns: a Buffer with the result.
Errors: an OpenSSL error with a code such as ERR_OSSL_RSA_DATA_TOO_LARGE_FOR_KEY_SIZE, or ERR_OSSL_RSA_OAEP_DECODING_ERROR when the data is too long or decryption fails. ERR_INVALID_ARG_TYPE for wrong types.
Usage notes
- Maximum size. With OAEP: key bytes minus
2 * hashLength + 2. For a 2048-bit key with SHA-256 that is 190 bytes; with SHA-1 it is 214 bytes. - Use OAEP, not PKCS#1 v1.5. v1.5 padding for decryption is vulnerable to padding-oracle attacks (Bleichenbacher). Node.js disables
RSA_PKCS1_PADDINGforprivateDecryptby default in current releases unless a flag re-enables it. - Both sides must agree on
oaepHashandoaepLabel. A mismatch looks like a generic decryption failure. - Never use RSA for bulk data. It is slow and size-limited. Encrypt a random key with RSA and use AES-GCM for the payload, as in the second example.
privateEncrypt()is not a signature API. Usecrypto.sign(), which hashes and formats the data correctly.- Elliptic-curve keys cannot use these functions. Use key agreement plus symmetric encryption.
- Key sizes. Use 2048 bits at a minimum; the RSA key must be a
KeyObjector PEM of an RSA key.