Skip to content

Instantly share code, notes, and snippets.

@danieljs777
Created April 23, 2020 15:04
Show Gist options
  • Save danieljs777/28b89e754389f19a3809fa9dd96ad2f0 to your computer and use it in GitHub Desktop.
Save danieljs777/28b89e754389f19a3809fa9dd96ad2f0 to your computer and use it in GitHub Desktop.
PHP Encryption Sample
<?php
$token = "2";
$cipher_method = 'aes-128-ctr';
$enc_key = openssl_digest(php_uname(), 'SHA256', TRUE);
$enc_iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($cipher_method));
$crypted_token = openssl_encrypt($token, $cipher_method, $enc_key, 0, $enc_iv) . "::" . bin2hex($enc_iv);
unset($token, $cipher_method, $enc_key, $enc_iv);
dump($crypted_token);
list($crypted_token, $enc_iv) = explode("::", $crypted_token);
$cipher_method = 'aes-128-ctr';
$enc_key = openssl_digest(php_uname(), 'SHA256', TRUE);
$token = openssl_decrypt($crypted_token, $cipher_method, $enc_key, 0, hex2bin($enc_iv));
unset($crypted_token, $cipher_method, $enc_key, $enc_iv);
dump($token);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment