php Function sodium_crypto_Box_PublicKey
Asymmetric encryption involves two parties exchanging a private key and a public key. This allows one party to encrypt messages that only the other can decrypt, and verify that the message came from the sender. This is used to secure email, chat, and other online communication. This article covers a simple way to implement this type of cryptography with PHP, using the libsodium library.
This library is a modern, lightweight, and opinionated software library for doing cryptography in PHP. It provides high-level APIs that hide the low-level details of the underlying algorithms. It is based on NaCl, but has evolved to provide more secure and sensible defaults. Libsodium is available as a PECL extension and has been included in PHP core since version 7.2.
php function sodium_crypto_box_publickey
This function encrypts a message with asymmetric (public-key) encryption. It requires the sender's X25519 secret key and the recipient's X25519 public key, as well as a nonce value for each message. It returns a ciphertext string plus an authentication tag. The ciphertext is 16 bytes longer than the plaintext and is a raw binary string. It should be converted to a text format with functions such as base64_encode and bin2hex prior to printing or transmitting it to a medium that cannot handle raw byte streams.
The resulting ciphertext is a unique string that can be used to authenticate the message. It does not guarantee that the message was sent by the intended recipient, however, because it does not guarantee that the recipient's public key matches the one used to encrypt the message. If you need to guarantee that the message was actually signed by the sender, you should use the sodium_crypto_box_seal() function instead.