Signing and encryption
Encrypting a message to someone needs a key of theirs and an agreement on algorithms. This lesson follows the collage app as it publishes an encryption key, registers its choices with the photo service, and opens Ava's UserInfoAn OpenID Provider endpoint that returns claims about the signed-in person to a client presenting an access token. Its sub must match the ID token's before the client uses anything it returns. response layer by layer: an encrypted JWTA JSON Web Token: a compact token made of Base64url-encoded parts that carries claims and is usually signed. Anyone holding it can decode and read the claims, so the signature shows who issued it but hides nothing. around a signed one.
A key for encryption
After the review in Choosing which messages to protect, the collage app receives its UserInfo responses signed and encrypted, and signs and encrypts its request objects. Before the photo service can encrypt anything for the collage app, the collage app needs a key the photo service can encrypt to, and the two sides need to agree on algorithms. All domains, identifiers, keys and tokens in these examples are fictional, and key values are shortened.
The collage app already publishes a key setA JSON document, published at a fixed HTTPS address, that lists an issuer's current public keys so others can verify its signatures. It never contains private keys. at https://collage.example/oauth/jwks.json, registered with the photo service as its jwks_uri. It holds collage-2026-10, the elliptic curve key that signs the collage app's client assertions and request objects. The collage app adds a second key, an RSA key pairA private key and a public key generated together and mathematically linked. The private key creates signatures and the public key checks them, but the private key cannot practically be worked out from the public one. used only for encryption:
GET /oauth/jwks.json HTTP/1.1
Host: collage.example
HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: max-age=3600
{
"keys": [
{ "kid": "collage-2026-10", "kty": "EC", "crv": "P-256", "use": "sig", "alg": "ES256", "x": "...", "y": "..." },
{ "kid": "collage-enc-2026-10", "kty": "RSA", "use": "enc", "alg": "RSA-OAEP-256", "n": "...", "e": "AQAB" }
]
}The new key has its own identifier, collage-enc-2026-10, and two fields that say what it is for. use is enc, for encryption, where the signing key says sig. Once a key set contains both kinds, every key in it must carry use, so that nobody encrypts to a signing key or verifies with an encryption key. alg ties the key to a single algorithm, RSA-OAEP-256. Some algorithms could use one key for both purposes, but the specifications advise against it, for the reasons Storing and using keys gave for limiting each key to one job.
The private half stays with the collage app's sign-in service, ideally in a key service that decrypts on request without ever releasing the key. The Cache-Control header tells the photo service how long it may keep its copy of this set before fetching it again, which will matter when the key is replaced.
Registering the choices
The collage app records its choices in its registration with the photo service. Registering a relying party introduced id_token_signed_response_alg. The rest of the fields come in families:
{
"client_id": "photo-collage",
"jwks_uri": "https://collage.example/oauth/jwks.json",
"id_token_signed_response_alg": "RS256",
"userinfo_signed_response_alg": "RS256",
"userinfo_encrypted_response_alg": "RSA-OAEP-256",
"userinfo_encrypted_response_enc": "A256GCM",
"request_object_signing_alg": "ES256",
"request_object_encryption_alg": "RSA-OAEP-256",
"request_object_encryption_enc": "A256GCM"
}Only the fields this lesson discusses are shown. A field ending in _signed_response_alg names the algorithm the photo service must sign with. For UserInfo, registering one is what turns the response from plain JSONA text format for structured information, written as named values inside curly braces. APIs, tokens, and discovery documents all use it. into a signed JWT. The collage app leaves out id_token_encrypted_response_alg, so its ID tokens stay signed only.
Encryption needs two algorithms, so each encrypted message has a pair of fields. The _alg field names the key management algorithm, which protects the key that the content is encrypted with. The _enc field names the content encryption algorithm, which encrypts the message itself. An _enc value cannot be registered without its _alg, and when only the _alg is registered, the _enc defaults to A128CBC-HS256. The collage app chooses A256GCM explicitly.
The request objectAn OAuth extension, RFC 9101, that puts the authorization request's parameters in a signed JWT called a request object. The authorization server can then tell they came from the client and were not changed in the browser. fields read slightly differently, because here the collage app is the sender. request_object_signing_alg is a rule the photo service enforces: every request object from the collage app must be signed with ES256, and any other is rejected. The encryption pair only declares what the collage app may use. To encrypt, the collage app needs a public keyThe half of a key pair that can be shared freely. It checks signatures made with the matching private key and can encrypt data that only that private key can decrypt. belonging to the photo service, so the photo service's key set now includes one, photos-enc-2026-09, marked use enc, beside its signing keys photos-rs-2026-09 and photos-2026-09.
Every algorithm the collage app registers must be one the photo service supports. Its discovery metadata lists them under names such as userinfo_encryption_alg_values_supported, userinfo_encryption_enc_values_supported and request_object_encryption_alg_values_supported.
Inside an encrypted response
With the registration in place, the collage app's next UserInfo request for Ava looks the same as ever. The response does not:
HTTP/1.1 200 OK
Content-Type: application/jwt
eyJhbGciOiJSU0EtT0FFUC0yNTYiLCJlbmMiOiJBMjU2R0NNIiwia2lkIjoiY29sbGFnZS1lbmMtMjAyNi0xMCIsImN0eSI6IkpXVCJ9
.(encrypted key)
.(initialization vector)
.(ciphertext)
.(authentication tag)The line breaks are for display, and the last four parts are named rather than shown. A UserInfo response that is signed or encrypted is a JWT, with the content type application/jwt. This one is a JWEThe JOSE format for encrypted content, defined in RFC 7516, which only holders of the right key can read. A JWT is sent as a JWE when its claims must stay confidential, not just protected from change. in its compact form: five Base64urlA way of encoding data as ordinary text characters so it can travel safely in headers, URLs, and JSON. Anyone can decode it, so it hides nothing; Base64url is a variant safe for URLs, used in JWTs. parts separated by dots, where a signed JWT has three. The first part, the protected header, decodes to:
{
"alg": "RSA-OAEP-256",
"enc": "A256GCM",
"kid": "collage-enc-2026-10",
"cty": "JWT"
}Encrypting the whole response with the collage app's RSA key would be slow, and RSA can only encrypt a small amount of data at a time. As Secrets and key pairs described for TLSTransport Layer Security, the protection behind HTTPS. It encrypts traffic between two connected systems, detects changes on the way, and uses certificates so the client can confirm which server it reached., and Encrypting tokens with JWE showed for tokens, the key pair protects a fresh shared key instead, and the shared key does the bulk of the work. The five parts follow that design:
| Part | What it holds |
|---|---|
| Protected header | The algorithms and key identifier. Anyone can read it, and it is covered by the integrity check. |
| Encrypted key | A random 256-bit content encryption key, generated for this one message and encrypted to collage-enc-2026-10 with RSA-OAEP-256. |
| Initialization vector | A value the content encryption needs alongside the key, chosen fresh for each message. |
| Ciphertext | The message, encrypted with the content encryption key using AES-GCM. |
| Authentication tag | The integrity check. If the ciphertext, the initialization vector or the protected header has changed, decryption fails. |
So alg says how the content encryption key reaches the collage app, and enc says how the content itself is encrypted. JWE allows only authenticated encryption for enc, which checks integrity as it decrypts. kid tells the collage app which of its private keys to use, and cty set to JWT says that the decrypted content is itself a JWT.
Other key management algorithms change the second part. With ECDH-ES, the sender generates a temporary elliptic curve key pair, places its public half in the header as epk, and both sides derive the content encryption key from it and the recipient's key, so the encrypted key part is empty. You may also meet RSA1_5, an older RSA scheme that current guidance says to avoid, for reasons that come up in Key selection and validation failures.
Signed, then encrypted
Decrypting the ciphertext gives the collage app an ordinary signed JWT, which begins eyJhbGciOiJSUzI1NiIsImtpZCI6InBob3Rvcy1ycy0yMDI2LTA5IiwidHlwIjoiSldUIn0.eyJpc3MiOiJodHRwczovL2F1dGgucGhvdG9zLmV4YW1wbGUi... and decodes to:
Header:
{
"alg": "RS256",
"kid": "photos-rs-2026-09",
"typ": "JWT"
}
Claims:
{
"iss": "https://auth.photos.example",
"aud": "photo-collage",
"sub": "user-2048",
"name": "Ava Archer",
"given_name": "Ava",
"family_name": "Archer",
"email": "[email protected]",
"email_verified": true,
"picture": "https://photos.example/avatars/user-2048.jpg",
"locale": "en-US",
"updated_at": 1788220800
}This is a nested JWT, the structure Building a request object used for encrypted request objects: a signed JWT carried as the content of an encrypted one. The photo service signed Ava's profile with the RSA key that signs its ID tokens, then encrypted the result to the collage app. The signed form contains iss and aud, as every signed UserInfo response must.
OpenID ConnectAn identity layer on top of OAuth 2.0 that lets an application sign people in with an account at another service. It adds the ID token, standard claims, and discovery to the OAuth exchange. requires this order whenever a message is both signed and encrypted, and the order does real work. The signatureA value made with a private key that lets anyone holding the matching public key check that a message came from the key's holder and has not been changed. It proves origin and integrity; it does not hide the message. covers Ava's profile itself, so it keeps its meaning after decryption. The sign-in service can pass the signed JWT to the order service, and the order service can verify that the photo service made these statements without ever holding the collage app's decryption key. A signature over the ciphertext would only show who produced one particular encrypted form, and the specification notes that many jurisdictions do not treat signatures over encrypted text as valid. Encrypting last needs no second signature either, because the authenticated encryption already detects any change to the outer layer.
Signed, then encrypted
- Signature covers
- Ava's profile itself
- After decryption
- Still verifies
- Order service needs
- The photo service's public key
- Outer layer
- Authenticated encryption
The order service can verify the photo service made these statements.
Encrypted, then signed
- Signature covers
- One encrypted form
- After decryption
- Nothing left to verify
- Order service needs
- The encrypted form as well
- Outer layer
- A second signature
Shows only who produced one particular encrypted form.
A library that skips the inner signature has accepted a message that anyone could have written.
The order also explains the most common mistake in handling nested JWTs. As Choosing which messages to protect pointed out, encryption to the collage app's public key says nothing about the sender. A library that decrypts the outer layer and hands back the inner JWT without verifying its signature has accepted a message that anyone could have written. Both layers must pass, each with keys and algorithms the collage app chose itself.
Check your understanding
Try these questions before moving on. If an answer isn't right, use the feedback and try again.
Try it in the Lab
- Receive signed and encrypted responses, and encrypt a request objectRegister an encryption key for lab-collage, ask for encrypted ID tokens and signed then encrypted UserInfo, open each layer with your own key, verify the inner JWT, and encrypt a signed request object to your tenant.