Last active
October 11, 2016 07:13
-
-
Save antonydandrea/bbde60ea7bdbe96e93ac703f23b4ff65 to your computer and use it in GitHub Desktop.
A simple mcrypt example being set and retrieved in a cookie. For some reasion, in PHP 7, setcookie doesn't appear to set anything and $_COOKIE returns null
This file contains 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 | |
$cookie_name = 'my_cookie'; | |
$key = 'L+Hu+ ~bK$BUd;Jcc;M+/d7LP#qzU_i6P&Aq86Q/}=wYuUo:Q}@Y)(=d|y}+E5.oK)hgHH1R|/)v-zR-cp}E3|FdnY/&jd^&o/mKO&|Vhn2liz#PY~|^}6Zc:u? O^|p'; | |
$secure = '0'; | |
$session_id = 'ABCDEFGHIJKLMNOP'; | |
$data = $session_id.'~~'.$secure; | |
$mode = MCRYPT_MODE_NOFB; | |
$cipher = MCRYPT_RIJNDAEL_128; | |
$key_size = mcrypt_get_key_size($cipher, $mode); | |
if (isset($key[$key_size])) { | |
// Shorten the key to the maximum size | |
$key = substr($key, 0, $key_size); | |
} | |
$iv_size = mcrypt_get_iv_size($cipher, $mode); | |
$iv = mcrypt_create_iv($iv_size, MCRYPT_DEV_URANDOM); | |
$data_encrypt = mcrypt_encrypt($cipher, $key, $data, $mode, $iv); | |
$data_encode = base64_encode($iv.$data_encrypt); | |
$stored = urlencode($data_encode); | |
$stored_correct = setcookie($cookie_name, $stored, 31536000, "/"); | |
echo '<pre>'; | |
var_dump($data_encode); | |
var_dump($stored); | |
var_dump($stored_correct); | |
echo '</pre>'; | |
$cookie = $_COOKIE[$cookie_name]; | |
$unstore = urldecode($cookie); | |
$data_decode = base64_decode($unstore, true); | |
$iv_part = substr($data_decode, 0, $iv_size); | |
$data_part = substr($data_decode, $iv_size); | |
$data_decrypt = mcrypt_decrypt($cipher, $key, $data_part, $mode, $iv_part); | |
echo '<pre>'; | |
var_dump($cookie); | |
var_dump($unstore); | |
var_dump($data_decrypt); | |
echo '</pre>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment