PHP Implementation Cookie Encryption decryption

Source: Internet
Author: User
Tags mcrypt set cookie setcookie
1. Encryption and Decryption class

 1 class Mcrypt 2 {3/** 4 * Decrypt 5 * 6 * @param string $encryptedText encrypted string 7 * @param str     ing $key key 8 * @return String 9/A public static function _decrypt ($encryptedText, $key = null) 11 {$key = $key = = null?
Config::get (' Secret_key '): $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);        18} 19 20/** 21 * Encryption * * @param string $plainText unencrypted string @param string $key  Key/Num public static function _encrypt ($plainText, $key = null) $key = $key = = NULL ?
Config::get (' Secret_key '): $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)); 33} 34}

2.cookie Encryption and Decryption class

 1 <?php 2 class Cookie extends Mcrypt 3 {4/** 5 * Delete Cookie 6 * 7 * @param array $args 8         * @return Boolean 9/A public static function del ($args) one {$name = $args [' name ']; 13 $domain = Isset ($args [' domain '])?
$args [' domain ']: null; return Isset ($_cookie[$name])?
Setcookie ($name, ', Time ()-86400, '/', $domain): true; 15} 16 17/** 18 * Get the value of the specified cookie * * @param string $name/public stat     IC function Get ($name) {return isset ($_cookie[$name])? Parent::_decrypt ($_cookie[$name]): null; 25     26 27/** 28 * Set Cookie * * @param array $args * @return Boolean 32/33 The public static function set ($args) is $name = $args [' name ']; $value = Parent::_encrypt ($args
[' value ']); Panax Notoginseng $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); 42} 43}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.