PHP Function Sodium Crypto Box
php function sodium_crypto_box allows us to use a Cryptographically-secure pseudorandom number generator. The generated random numbers are used to encrypt and decrypt messages. A key is also generated for a message and must be kept secret. A nonce value is also generated for each message to prevent reuse of the same message by an attacker. The function is safe to use, and can be used for example:
It uses libsodium, a modern, portable, lightweight, and opinionated library to do cryptography, which simplifies the decision making for users. It supports a wide range of ciphers (AES, Blowfish, Twofish, GCM), provides authentication support, and much more. It is recommended to use the XChaCha20-Poly1305 – IETF cipher, as it is the most secure and is most likely supported by your CPU.
Aside from symmetric encryption, Sodium offers support for asymmetric encryption as well. With asymmetric encryption, each user has a pair of keys: One public and the other private. The public key can be distributed publicly and anyone can encrypt a message for that user, but the message will only be decrypted by the person who has the private key. To authenticate asymmetric encrypted messages, both parties must exchange their public keys with each other securely, which can be done via a key-exchange protocol or another means.
Sodium is a great alternative to the mcrypt and OpenSSL extensions, with better support for authentication and key lengths. Moreover, it offers a variety of additional functions like generate nonce or random_bytes, that are missing from the other extensions.