Advertisement
krot

crypt cart product

Mar 2nd, 2017
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.57 KB | None | 0 0
  1. <?php
  2. // Define a 32-byte (64 character) hexadecimal encryption key
  3. // Note: The same encryption key used to encrypt the data must be used to decrypt the data
  4. define('ENCRYPTION_KEY', '75bbd89dcc2878554122f2909d7916873c6bb37dfd5fafee3c26e81c439a88b1');
  5.  
  6. // Encrypt Function
  7. function mc_encrypt($encrypt, $key){
  8.     $encrypt = json_encode($encrypt);
  9.     //print_r($encrypt);
  10.     $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC), MCRYPT_DEV_URANDOM);
  11.     $key = pack('H*', $key);
  12.     $mac = hash_hmac('sha256', $encrypt, substr(bin2hex($key), -32));
  13.     $passcrypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $encrypt.$mac, MCRYPT_MODE_CBC, $iv);
  14.     $encoded = base64_encode($passcrypt).'|'.base64_encode($iv);
  15.     return $encoded;
  16. }
  17. // Decrypt Function
  18. function mc_decrypt($decrypt, $key){
  19.     $decrypt = explode('|', $decrypt.'|');
  20.     $decoded = base64_decode($decrypt[0]);
  21.     $iv = base64_decode($decrypt[1]);
  22.     if(strlen($iv)!==mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC)){ return false; }
  23.     $key = pack('H*', $key);
  24.     $decrypted = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $decoded, MCRYPT_MODE_CBC, $iv));
  25.     $mac = substr($decrypted, -64);
  26.     $decrypted = substr($decrypted, 0, -64);
  27.     $calcmac = hash_hmac('sha256', $decrypted, substr(bin2hex($key), -32));
  28.     if($calcmac!==$mac){ return false; }
  29.     $decrypted = json_decode($decrypted);
  30.     return $decrypted;
  31. }
  32.  
  33. ..
  34. value=<?=mc_encrypt(array(
  35.              $id,
  36.              $name,
  37.              ), ENCRYPTION_KEY);?>
  38. $Data=mc_decrypt($Data, ENCRYPTION_KEY);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement