PHP implementation of cookie encryption method, Phpcookie encryption
In this paper, we explain how PHP implements cookie encryption. Share to everyone for your reference. The implementation method is as follows:
Copy CodeThe code is as follows: <?php
Class Cookie
{
/**
* Decrypt a cookie that has already been encrypted
*
* @param string $encryptedText
* @return String
*/
private static function _decrypt ($encryptedText)
{
$key = Config::get (' Secret_key ');
$cryptText = Base64_decode ($encryptedText);
$ivSize = Mcrypt_get_iv_size (mcrypt_rijndael_256, MCRYPT_MODE_ECB);
$iv = Mcrypt_create_iv ($ivSize, Mcrypt_rand);
$decryptText = Mcrypt_decrypt (mcrypt_rijndael_256, $key, $cryptText, MCRYPT_MODE_ECB, $IV);
Return trim ($decryptText);
}
/**
* Encrypt cookies
*
* @param string $plainText
* @return String
*/
private static function _encrypt ($plainText)
{
$key = Config::get (' Secret_key ');
$ivSize = Mcrypt_get_iv_size (mcrypt_rijndael_256, MCRYPT_MODE_ECB);
$iv = Mcrypt_create_iv ($ivSize, Mcrypt_rand);
$encryptText = Mcrypt_encrypt (mcrypt_rijndael_256, $key, $plainText, MCRYPT_MODE_ECB, $IV);
Return Trim (Base64_encode ($encryptText));
}
/**
* Delete Cookies
*
* @param array $args
* @return Boolean
*/
public static function del ($args)
{
$name = $args [' name '];
$domain = Isset ($args [' domain '])? $args [' domain ']: null;
return Isset ($_cookie[$name])? Setcookie ($name, ', Time ()-86400, '/', $domain): true;
}
/**
* Get the value of the specified cookie
*
* @param string $name
*/
public static function Get ($name)
{
return Isset ($_cookie[$name])? Self::_decrypt ($_cookie[$name]): null;
}
/**
* Set Cookies
*
* @param array $args
* @return Boolean
*/
public static function set ($args)
{
$name = $args [' name '];
$value = Self::_encrypt ($args [' value ']);
$expire = Isset ($args [' expire '])? $args [' expire ']: null;
$path = Isset ($args [' path '])? $args [' Path ']: '/';
$domain = Isset ($args [' domain '])? $args [' domain ']: null;
$secure = Isset ($args [' secure '])? $args [' secure ']: 0;
Return Setcookie ($name, $value, $expire, $path, $domain, $secure);
}
}
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/965564.html www.bkjia.com true http://www.bkjia.com/PHPjc/965564.html techarticle PHP Implementation of the method of cookie encryption, Phpcookie encryption This article describes the PHP implementation of cookie encryption method. Share to everyone for your reference. The implementation method is as follows: Copy code code ...