Last active
July 8, 2020 15:02
-
-
Save Shelob9/66d7f21872e9fe88afdc68de1cbf478b to your computer and use it in GitHub Desktop.
Basic Hi Roy example of PHP encryption/decryption. #ApexSecurity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$key = random_bytes(SODIUM_CRYPTO_SECRETBOX_KEYBYTES); | |
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); | |
$ciphertext = sodium_crypto_secretbox("Hi Roy", $nonce, $key); | |
$plaintext = sodium_crypto_secretbox_open($ciphertext, $nonce, $key); | |
if ($plaintext === false) { | |
throw new Exception("Bad ciphertext"); | |
} | |
echo $plaintext; // "Hi Roy" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment