Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Last active July 8, 2020 15:02
Show Gist options
  • Save Shelob9/66d7f21872e9fe88afdc68de1cbf478b to your computer and use it in GitHub Desktop.
Save Shelob9/66d7f21872e9fe88afdc68de1cbf478b to your computer and use it in GitHub Desktop.
Basic Hi Roy example of PHP encryption/decryption. #ApexSecurity
<?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