Skip to content

Core\Jwe::encrypt

Encrypts a text and returns it as a JWE token, a short string you can put in a cookie, a URL or a message. Only a program with the right key can read the text again, with Core\Jwe::decrypt.

The key is a Core\Jwe\Key. The method that made the key chooses the algorithm: shared for a secret key that both sides already have, password for a password, and recipient for another party's public key. The content is always encrypted with AES-GCM.

The token is different each time, even for the same text and the same key. So you cannot compare two tokens to find out if their texts are equal.

The examples below encrypt a text with a shared key, encrypt a note with a password, and send data to a partner who published a public key.

Parameters

  • $payloadstringThe text to encrypt. decrypt returns exactly this text, as a tainted value.
  • $keyCore\Jwe\KeyThe key, made by one of the four static methods of Jwe\Key. That method also sets alg. A key made by Jwe\Key::own encrypts to its own public key.

Return value

Returns string. Five base64url parts without padding, joined by dots. They are the protected header, the encrypted key, the 12-byte IV, the ciphertext and the 16-byte tag. The encrypted key is empty for dir and ECDH-ES. Each call uses a new IV, so the token is different every time.

Errors

  • LogicErrorThe public key or key pair in $key has a point that gives no usable shared secret.
  • RuntimeErrorThis process does not have enough memory for a buffer the size of the token.

Examples

Basics

<?nvs
// Shows how to encrypt a text with a secret key and read it back.
// `Core\Crypto::generateKey` returns 32 random secret bytes.
// `Core\Jwe\Key::shared` makes a key from them.
secret bytes $secret = Core\Crypto::generateKey();
Core\Jwe\Key $key = Core\Jwe\Key::shared($secret);
// `encrypt` returns the token. The token has five parts, separated by dots.
// This prints `5`.
string $token = Core\Jwe::encrypt('order 1042 is paid', $key);
echo Core\Arr::count(Core\Str::split($token, '.')), "\n";
// The same text gives a different token each time. This prints `different`.
string $again = Core\Jwe::encrypt('order 1042 is paid', $key);
echo $token == $again ? 'equal' : 'different', "\n";
// `decrypt` with the same key returns the text. This prints `order 1042 is paid`.
echo Core\Jwe::decrypt($token, [$key]), "\n";
Output
5
different
order 1042 is paid

A password

<?nvs
// Shows how to encrypt a note with a password.
// `Core\Jwe\Key::password` makes a key from a password.
Core\Jwe\Key $key = Core\Jwe\Key::password('correct horse battery staple');
string $token = Core\Jwe::encrypt('The safe code is 4711.', $key);
// The same password opens the token. This prints `The safe code is 4711.`
echo Core\Jwe::decrypt($token, [Core\Jwe\Key::password('correct horse battery staple')]), "\n";
// A wrong password throws a `RuntimeError`. This prints `wrong password`.
try {
Core\Jwe::decrypt($token, [Core\Jwe\Key::password('correct horse')]);
echo 'opened', "\n";
} catch (RuntimeError $wrong) {
echo 'wrong password', "\n";
}
Output
The safe code is 4711.
wrong password

Sending data to a partner

<?nvs
// Shows how to send data that only one partner can read, with the public key the partner published.
// The partner makes a key pair and publishes the public half.
// In real code, you read the published key with `Core\Crypto\PublicKey::read`.
Core\Crypto\KeyPair $partner = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::X25519);
Core\Crypto\PublicKey $published = $partner->publicKey();
// `Core\Jwe\Key::recipient` makes a key that encrypts for the owner of this public key.
// The data is a JSON document with the customer's details.
string $details = Core\Json::encode({ customer: 1042, iban: 'AT61 1904 3002 3457 3201' });
string $token = Core\Jwe::encrypt($details, Core\Jwe\Key::recipient($published));
// Only the partner's key pair can read the token. `Core\Jwe\Key::own` makes that key.
// This prints the JSON document: `{"customer":1042,"iban":"AT61 1904 3002 3457 3201"}`.
echo Core\Jwe::decrypt($token, [Core\Jwe\Key::own($partner)]), "\n";
Output
{"customer":1042,"iban":"AT61 1904 3002 3457 3201"}