JSON Web Encryption (RFC7516) is the encryption counterpart of the JSON Web Signature. A signed token can be read by anyone, the signature only proves that it has not been tampered with. An encrypted token hides the content, so only the recipient with the right key can read it.
The jose package has two pairs of functions for
encryption:
jwe_encrypt() and jwe_decrypt() encrypt
arbitrary data: a string or raw vector. Decrypting returns a raw vector
with the original data.jwt_encrypt() and jwt_decrypt() encrypt a
JWT claim. This is the same JWE format, but the payload is the JSON
claim created with jwt_claim(), and the header has
typ set to JWT. The decrypt function parses
the claim and checks the exp and nbf fields,
just like jwt_decode_sig() does.So an encrypted JWT is simply a JWE that contains a JWT claim. Use
the jwt_ functions for tokens, and the jwe_
functions for any other data.
Data is encrypted with the public key of the recipient, who then decrypts it using the private key. This works with both RSA and EC keys.
Linking to: OpenSSL 3.5.5 27 Jan 2026
library(jose)
# Recipient generates a keypair
key <- ec_keygen()
pubkey <- as.list(key)$pubkey
# Encrypt a message for the recipient
(jwe <- jwe_encrypt("This is a secret message", pubkey))[1] "eyJhbGciOiJFQ0RILUVTIiwiZW5jIjoiQTI1NkdDTSIsImVwayI6eyJrdHkiOiJFQyIsImNydiI6IlAtMjU2IiwieCI6Imh1ZFlOWDF5NXVSc3F5eHQ1VTQtN3gybFJ6N0N6VGQyUXFJalpqanZsSEEiLCJ5IjoicXpyVDd3Xy11WlpCb05KUTU4UEhiMEZJTGdvcExRanNSZWE1TEwyUUlCSSJ9fQ..uTxCvujj4F2itr_a.6QrqtEWtUjCNcZHYocnus7V_vFeP6xJD.W5B4KcGPAzMY2C23mtgJdg"
[1] "This is a secret message"
Decryption errors if the key is wrong or if the data was modified.
Error:
! JWE decryption failed: wrong key or data has been modified
The jwt_encrypt() and jwt_decrypt()
functions do the same for a JWT claim.
claim <- jwt_claim(user = "jeroen", session_key = 123456)
# Encrypt for the recipient
(jwt <- jwt_encrypt(claim, pubkey))[1] "eyJhbGciOiJFQ0RILUVTIiwiZW5jIjoiQTI1NkdDTSIsImVwayI6eyJrdHkiOiJFQyIsImNydiI6IlAtMjU2IiwieCI6IkJlNmVudDFndGJLd1JrdjB0bjl5OEdINDg5QUU3Y25fZzZ6cnVnM3RIVUUiLCJ5IjoibEhaeUxsY1gwZzFpa2JLNi12eXhrQUlMcGp0enBQcnppdVBzQmh2aVdIbyJ9LCJ0eXAiOiJKV1QifQ..0EU2P0XBuilROcK2.DG6wiwMs6A5d2Kd1v-9ARmC37G3rNQIfcZl_ZV3eZF4fbRSDwKTDKtF8k86jrgeVdTQcu5zcyg.qe7OXkS7nT7-rfEBkASm_w"
$iat
[1] 1795030675
$user
[1] "jeroen"
$session_key
[1] 123456
Each JWE uses two algorithms. The alg determines how the
random content encryption key is encrypted for the recipient, and
enc determines how the payload itself is encrypted with
that key. By default alg is chosen based on your key, and
enc is A256GCM. You can specify other
algorithms:
rsa_key <- rsa_keygen()
jwe <- jwe_encrypt("Hello", as.list(rsa_key)$pubkey, alg = "RSA-OAEP", enc = "A128CBC-HS256")
rawToChar(jwe_decrypt(jwe, rsa_key))[1] "Hello"
See ?jwe for all supported algorithms. The header shows
which algorithms were used:
{"alg":"RSA-OAEP","enc":"A128CBC-HS256"}
Encryption hides the content of a token, but it does not prove who
created it, because anyone with the public key of the recipient can
create a valid JWE. If you need both, you can first sign the JWT with
your own key, and then encrypt the signed token for the recipient. The
cty header indicates that the content is a JWT.
# Sender signs with own key, then encrypts for the recipient
sender_key <- ec_keygen()
signed <- jwt_encode_sig(claim, key = sender_key)
jwe <- jwe_encrypt(signed, pubkey, header = list(cty = "JWT"))
# Recipient decrypts, then verifies the signature of the sender
signed <- rawToChar(jwe_decrypt(jwe, key))
jwt_decode_sig(signed, pubkey = as.list(sender_key)$pubkey)$iat
[1] 1795030675
$user
[1] "jeroen"
$session_key
[1] 123456