Skip to content

Core\Jwe::decrypt

Reads the text inside a JWE token that Core\Jwe::encrypt made. You give it the token and a list of keys. It tries each key in order and returns the text from the first key that opens the token.

A list of keys lets you change a key without breaking old tokens. Put the new key first and keep the old key after it until the old tokens expire.

If no key opens the token, decrypt throws a RuntimeError. A changed token, a token for another key and a text that is not a token all give the same error message. An empty list, or a list with a password key and another key, throws a LogicError.

The text it returns is tainted. This means Novis treats it like user input. The key proves who wrote the text, not that the text is safe.

The examples below read a token back, keep an old key in the list, and read a session cookie.

Parameters

  • $tokenstringThe token text, as it arrived. A tainted value is allowed here, because tokens usually come from outside.
  • $keysarray<Core\Jwe\Key>The keys to try, in order. A token encrypted with any of these keys can be decrypted. A key made by Jwe\Key::password must be the only key, because each try is slow. A key made by Jwe\Key::recipient is a public key and decrypts nothing.

Return value

Returns tainted string. The payload, exactly as it was encrypted, as a tainted string.

Errors

  • LogicError$keys is empty, or it contains a Jwe\Key::recipient key, or it contains a Jwe\Key::password key together with other keys.
  • RuntimeErrorNo key in $keys can decrypt $token. The token may not have five parts, or its header has fields this class does not read. Its alg may not match $keys, or the token was changed. All cases have the same message, so an attacker cannot learn which check failed.
  • ParseErrorThe token was decrypted, but its payload is not valid UTF-8 text.

Examples

Basics

<?nvs
// Shows how to read the text inside a token, and what happens when the token was changed.
Core\Jwe\Key $key = Core\Jwe\Key::shared(Core\Crypto::generateKey());
string $token = Core\Jwe::encrypt('invoice 2024-117', $key);
// `decrypt` needs a list of keys. With the key that made the token,
// it returns the text. This prints `invoice 2024-117`.
echo Core\Jwe::decrypt($token, [$key]), "\n";
// One extra character makes the token invalid. `decrypt` throws a `RuntimeError`.
// This prints `the token was changed`.
try {
Core\Jwe::decrypt($token . 'A', [$key]);
echo 'opened', "\n";
} catch (RuntimeError $changed) {
echo 'the token was changed', "\n";
}
Output
invoice 2024-117
the token was changed

Changing the key

<?nvs
// Shows how to change to a new key and still read the tokens made with the old key.
Core\Jwe\Key $old = Core\Jwe\Key::shared(Core\Crypto::generateKey());
Core\Jwe\Key $new = Core\Jwe\Key::shared(Core\Crypto::generateKey());
// One token was made before the change, and one after it.
string $before = Core\Jwe::encrypt('made with the old key', $old);
string $after = Core\Jwe::encrypt('made with the new key', $new);
// `decrypt` tries the keys in order. The new key is first, the old key is second.
// This prints both texts.
array<Core\Jwe\Key> $keys = [$new, $old];
echo Core\Jwe::decrypt($before, $keys), "\n";
echo Core\Jwe::decrypt($after, $keys), "\n";
// When the old key is removed from the list, the old token cannot be read.
// This prints `the old token is no longer valid`.
try {
Core\Jwe::decrypt($before, [$new]);
} catch (RuntimeError $expired) {
echo 'the old token is no longer valid', "\n";
}
Output
made with the old key
made with the new key
the old token is no longer valid

Reading a session cookie

<?nvs
// Shows how a server reads a session cookie that it encrypted. A visitor with an invalid cookie is a guest.
class Session
{
// `user` returns the user name inside the cookie. It returns `guest` when no key opens the cookie.
public static function user(string $cookie, array<Core\Jwe\Key> $keys): tainted string
{
try {
return Core\Jwe::decrypt($cookie, $keys);
} catch (RuntimeError $invalid) {
return 'guest';
}
}
}
// In real code, the key comes from the server's configuration.
array<Core\Jwe\Key> $keys = [Core\Jwe\Key::shared(Core\Crypto::generateKey())];
// The server made this cookie when the user signed in.
string $cookie = Core\Jwe::encrypt('maria', $keys[0]);
// This prints `maria`.
echo Session::user($cookie, $keys), "\n";
// A visitor wrote their own cookie. This prints `guest`.
echo Session::user('maria', $keys), "\n";
Output
maria
guest