tr-jwe
v1.0.0
Published
JSON Web Encryption (JWE) encrypt/decrypt for Node.js
Maintainers
Readme
tr-jwe
Compact JWE encrypt/decrypt for Node.js.
This package produces and consumes compact JWE tokens whose plaintext
is any JSON-serialisable value — object, array, string, number,
boolean, or null. It supports AES key wrap, AES-GCM key wrap, direct
encryption, RSA-based key transport, and ECDH-ES.
Reference
Installation
npm install tr-jweNode.js >=24.0.0 is required.
Exports
const { encrypt, decrypt, unwrap } = require('tr-jwe');encrypt(alg, jwk, data, options)
Encrypts a JSON object and returns either a compact JWE string or, when
extendedReturn is set, an object that also exposes the content-encryption
key.
alg: JWE key management algorithmjwk: recipient or wrapping key in JWK formdata: plain JavaScript objectoptions: optional object (defaults to{})
Supported options fields:
compressPayloadfalse(default): no compression.true: payload is deflated and the header carrieszip: "DEF"."auto": payload is deflated only if the result is smaller than the raw JSON; otherwise the raw JSON is encrypted and nozipheader is emitted.
extendedReturnfalse(default): the function returns the compact JWE string.true: the function returns{ token, contentEncryptionKey }wherecontentEncryptionKeyis anoctJWK suitable fordecrypt(token, contentEncryptionKey). This is useful when the caller needs to share or later re-derive the CEK without access to the wrapping key.
Unknown option keys and unexpected value types throw.
Supported alg values:
A128GCMKW,A192GCMKW,A256GCMKWA128KW,A192KW,A256KWdirRSA1_5,RSA-OAEP,RSA-OAEP-256ECDH-ES
Content encryption is selected automatically:
A128GCMKWandA128KWuseA128GCMA192GCMKWandA192KWuseA192GCMA256GCMKWandA256KWuseA256GCMdirpicksA128GCM,A192GCM, orA256GCMfrom key sizeECDH-ESpicksA128GCM,A192GCM, orA256GCMfrom EC curve- RSA picks
A128GCMfor 1024-bit keys andA256GCMfor 2048-bit or larger keys
Example:
const { encrypt, decrypt } = require('tr-jwe');
const { cipherKeyGen } = require('tr-jwk');
const key = cipherKeyGen('A256GCMKW');
const token = encrypt('A256GCMKW', key, { message: 'secret' });
const payload = decrypt(token, key);
// Compression with auto-fallback and access to the content-encryption key:
const { token: t2, contentEncryptionKey: cek } =
encrypt('A256GCMKW', key, { message: 'secret' },
{ compressPayload: 'auto', extendedReturn: true });
const samePayload = decrypt(t2, cek);decrypt(token, jwk)
Decrypts a compact JWE token and returns the parsed JSON payload.
The expected JWK depends on the token:
- AES wrap and
dir:octJWK - RSA algorithms: RSA private JWK
ECDH-ES: EC private JWK
unwrap(token, jwk)
Derives or unwraps the content-encryption key from a compact JWE token and returns it as an oct JWK.
This is useful when the recipient wants the CEK itself instead of the decrypted payload.
Notes
- Payload input may be any JSON-serialisable value (object, array,
string, number, boolean, or
null). - Only compact serialization is supported.
- Only AES-GCM content encryption is implemented.
- Compression uses raw DEFLATE (
zip: "DEF").
Author
Timo J. Rinne [email protected] — https://github.com/rinne/
Copyright
Copyright © 2023–2026 Timo J. Rinne [email protected].
See COPYING for the full MIT license text.
License
MIT License
