3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * La fonction sodium_crypto_box_keypair() génère aléatoirement une clé secrète * et une clé publique correspondante. */ $aliceKeypair = sodium_crypto_box_keypair(); $alicePublicKey = sodium_crypto_box_publickey($aliceKeypair); $aliceSecretKey = sodium_crypto_box_secretkey($aliceKeypair); $bobKeypair = sodium_crypto_box_keypair(); $bobPublicKey = sodium_crypto_box_publickey($bobKeypair); $bobSecretKey = sodium_crypto_box_secretkey($bobKeypair); /** * Le nombre arbitraire n'a pas besoin d'être confidentiel, mais il doit être * utilisé avec une seule invocation de crypto_box_open() pour une paire * particulière de clés publiques et secrètes. */ $nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); $aliceToBobKeyPair = sodium_crypto_box_keypair_from_secretkey_and_publickey($aliceSecretKey, $bobPublicKey); $message = 'Signed using Alice\'s secret key and to be encrypted using Bob\'s public key.'; $ciphertext = sodium_crypto_box($message, $nonce, $aliceToBobKeyPair); $bobToAliceKp = sodium_crypto_box_keypair_from_secretkey_and_publickey($bobSecretKey, $alicePublicKey); $plaintext = sodium_crypto_box_open($ciphertext, $nonce, $bobToAliceKp); echo $plaintext;

preferences:
34.7 ms | 402 KiB | 5 Q