[李景山php]每天laravel-20160828|McryptEncrypter-2
2016-06-13 08:57
676 查看
/** * Determine if the given key and cipher combination is valid. * * @param string $key * @param string $cipher * @return bool */ public static function supported($key, $cipher) {// check key and cipher is valid return defined('MCRYPT_RIJNDAEL_128') && ($cipher === MCRYPT_RIJNDAEL_128 || $cipher === MCRYPT_RIJNDAEL_256); }// if defined this 128 so we can use it /** * Encrypt the given value. * * @param string $value * @return string * * @throws \Illuminate\Contracts\Encryption\EncryptException */ public function encrypt($value) {// the big function ,is the encrypt means to this value $iv = mcrypt_create_iv($this->getIvSize(), $this->getRandomizer());// get the iv by size and randomizer $value = base64_encode($this->padAndMcrypt($value, $iv));// use this base64 function to has the true value // Once we have the encrypted value we will go ahead base64_encode the input // vector and create the MAC for the encrypted value so we can verify its // authenticity. Then, we'll JSON encode the data in a "payload" array. $mac = $this->hash($iv = base64_encode($iv), $value);// get the mac value $json = json_encode(compact('iv', 'value', 'mac'));// get the json string if (! is_string($json)) { throw new EncryptException('Could not encrypt the data.'); }// if the value is wrong throw exception return base64_encode($json);// the return the base64_encode } /** * Pad and use mcrypt on the given value and input vector. * * @param string $value * @param string $iv * @return string */ protected function padAndMcrypt($value, $iv) { $value = $this->addPadding(serialize($value));// value be change like value return mcrypt_encrypt($this->cipher, $this->key, $value, MCRYPT_MODE_CBC, $iv);// then return the encrypt value }// the value is given value and input vector /** * Decrypt the given value. * * @param string $payload * @return string */ public function decrypt($payload) { $payload = $this->getJsonPayload($payload);// use the payload get the json payload // We'll go ahead and remove the PKCS7 padding from the encrypted value before // we decrypt it. Once we have the de-padded value, we will grab the vector // and decrypt the data, passing back the unserialized from of the value. $value = base64_decode($payload['value']);// first get the payload by base64_decode $iv = base64_decode($payload['iv']);//iv use the same method return unserialize($this->stripPadding($this->mcryptDecrypt($value, $iv)));// too big function }
相关文章推荐
- Android Native 绘图方法
- Ruby 之 class 中的 private、 protected、public
- PHP中return 和 exit 、break和contiue 区别与用法
- Ruby中的public、private、protected区别小结
- javascript 面向对象编程 function是方法(函数)
- javascript 面向对象编程 function也是类
- ASP.NET MVC @Helper辅助方法和@functons自定义函数的使用方法
- IIS下PHP连接数据库提示mysql undefined function mysql_connect()
- 浅谈Javascript中Object与Function对象
- JavaScript通过function定义对象并给对象添加toString()方法实例分析
- IE 当eval遇上function的处理
- JavaScript中Object和Function的关系小结
- Javascript 使用function定义构造函数
- 浅析return false的正确使用
- 关于function类中定义变量this的简单说明
- javascript中return,return true,return false三者的用法及区别
- javascript Object与Function使用
- JavaScript中的Function函数
- 使用Function.apply()的参数数组化来提高 JavaScript程序性能的技巧
- javascript下function声明一些小结