Skip to content

Instantly share code, notes, and snippets.

@robinsound
Created February 3, 2016 15:29
Show Gist options
  • Save robinsound/ca9a53d387e084437f82 to your computer and use it in GitHub Desktop.
Save robinsound/ca9a53d387e084437f82 to your computer and use it in GitHub Desktop.
php aes-256-cbc required
<?php
// encrypt
$text = json_encode(array(
'user' => 'username',
'first' => 'firstname',
'last' => 'lastname',
'any_data' => '11212 12123312 132 3'
));
$key = 'zebc5adt0y345630gcj483bu3886ybz3';
$algorithm = MCRYPT_RIJNDAEL_256;
$mode = MCRYPT_MODE_CBC;
$ivSize = mcrypt_get_iv_size($algorithm, $mode);
$cryptKey = substr($key, 0, 32);
$iv = mcrypt_create_iv($ivSize, MCRYPT_RAND);
// $result with '$$' as separator between IV and Cipherdata, and base64
$result = base64_encode($iv . '$$' . mcrypt_encrypt($algorithm, $cryptKey, $text, $mode, $iv));
// to decode
$data = base64_decode($data);
$iv = substr($data, 0, $ivSize);
$text = substr($data, $ivSize + 2);
$plain = rtrim(mcrypt_decrypt($algorithm, $cryptKey, $text, $mode, $iv), "\0");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment