Encrypting data and tokens with JSON Web Encryption (JWE) in R

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.

JWE versus JWT encryption

The jose package has two pairs of functions for encryption:

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.

Encrypting with a public key

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.

library(openssl)
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"
# The recipient decrypts it with the private key
rawToChar(jwe_decrypt(jwe, key))
[1] "This is a secret message"

Decryption errors if the key is wrong or if the data was modified.

wrong_key <- ec_keygen()
jwe_decrypt(jwe, wrong_key)
Error:
! JWE decryption failed: wrong key or data has been modified

Encrypting a JWT

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"
# Decrypt and get the claim
jwt_decrypt(jwt, key)
$iat
[1] 1795030675

$user
[1] "jeroen"

$session_key
[1] 123456

Encrypting with a shared secret

Instead of a keypair you can also use a shared secret key, which must be a raw vector of 16, 24 or 32 bytes.

secret <- rand_bytes(32)
jwe <- jwe_encrypt("This is a secret message", secret)
rawToChar(jwe_decrypt(jwe, secret))
[1] "This is a secret message"

Algorithms

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:

strings <- strsplit(jwe, ".", fixed = TRUE)[[1]]
cat(rawToChar(base64url_decode(strings[1])))
{"alg":"RSA-OAEP","enc":"A128CBC-HS256"}

Signing and encrypting

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