Skip to content

Core\Jwe\Key::recipient

Makes a Core\Jwe\Key from the public key of another person or service. Use it when you want to send them a message that only they can read. Core\Jwe::encrypt encrypts the message with this key. The key cannot decrypt anything. Only the matching key pair can do that, with Core\Jwe\Key::own.

The public key must be a P256 or an X25519 key. An Ed25519 key or an RSA key throws a LogicError, because these keys cannot make a shared secret (a secret value that both sides compute from their own keys).

A token made with this key uses the JWE algorithm ECDH-ES. A public key does not need to be secret, so the other side can publish it or send it over the network.

The examples below encrypt a message for a key pair, show that this key cannot decrypt, and send a card number to a payment service that published its public key as JSON.

Parameters

  • $keyCore\Crypto\PublicKeyA P256 or X25519 public key, already validated by Core\Crypto\PublicKey::read.

Return value

Returns Core\Jwe\Key. A key for encrypt. Each call makes a new temporary key pair and writes its public key into the header as epk. This key cannot decrypt, because it is a public key.

Errors

  • LogicError$key is an Ed25519 or RSA key. Neither can be used for key agreement.

Examples

Basics

<?nvs
// Shows how to encrypt a message that only the owner of a key pair can read.
// The receiver makes a key pair and gives its public key to the sender.
Core\Crypto\KeyPair $pair = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::X25519);
Core\Crypto\PublicKey $public = $pair->publicKey();
// The sender encrypts the message with the public key.
string $token = Core\Jwe::encrypt('meet at noon', Core\Jwe\Key::recipient($public));
// The receiver decrypts it with the key pair. This prints `meet at noon`.
echo Core\Jwe::decrypt($token, [Core\Jwe\Key::own($pair)]), "\n";
Output
meet at noon

It only encrypts

<?nvs
// Shows that a key made from a public key can encrypt, but it cannot decrypt.
Core\Crypto\KeyPair $pair = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::P256);
Core\Jwe\Key $key = Core\Jwe\Key::recipient($pair->publicKey());
string $token = Core\Jwe::encrypt('hello', $key);
// `Core\Jwe::decrypt` throws a `LogicError` for this key.
// The message says that `Core\Jwe\Key::own` is the key that decrypts.
try {
Core\Jwe::decrypt($token, [$key]);
} catch (LogicError $error) {
echo $error->message, "\n";
}
Output
Core\Jwe::decrypt(): $keys holds a key built by Core\Jwe\Key::recipient, which is the other party's public key and opens nothing. The half that opens a token is the pair, through Core\Jwe\Key::own.

Send a card number to a payment service

<?nvs
// Shows how a shop sends a card number to a payment service. Only the payment service can read it.
class Shop
{
// The shop reads the published public key and encrypts the card number with it.
public static function pay(string $card, bytes $published): string
{
Core\Crypto\PublicKey $service = Core\Crypto\PublicKey::read(
$published,
Core\Crypto\KeyKind::X25519,
Core\Crypto\PublicKey::Format::Jwk,
);
return Core\Jwe::encrypt($card, Core\Jwe\Key::recipient($service));
}
}
class PaymentService
{
// The payment service decrypts the card number with its own key pair.
public static function receive(string $token, Core\Crypto\KeyPair $pair): tainted string
{
return Core\Jwe::decrypt($token, [Core\Jwe\Key::own($pair)]);
}
}
// The payment service makes its key pair once. It publishes the public key as JSON.
Core\Crypto\KeyPair $pair = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::X25519);
bytes $published = $pair->publicKey()->write(Core\Crypto\PublicKey::Format::Jwk);
string $token = Shop::pay('4111 1111 1111 1111', $published);
// This prints `4111 1111 1111 1111`.
echo PaymentService::receive($token, $pair), "\n";
Output
4111 1111 1111 1111