码迷,mamicode.com
首页 > Web开发 > 详细

php aes加密

时间:2017-05-03 13:14:27      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:个数   substr   init   bind   mcr   div   cti   ip地址   lld   

    private $iv = ‘lua123456789qwer‘; #随便填写16个数
    private $key = ‘123456789asdfghjkl$$$$$‘; #随便写多少


    //加密
    function encryptlua($str) {

      //$key = $this->hex2bin($key);    
      $iv = $this->iv;

      $td = mcrypt_module_open(‘rijndael-128‘, ‘‘, ‘cbc‘, $iv);

      mcrypt_generic_init($td, $this->key, $iv);
      $encrypted = mcrypt_generic($td, $str);

      mcrypt_generic_deinit($td);
      mcrypt_module_close($td);

      return bin2hex($encrypted);
    }

    //解密
    function decryptlua($str) {
      //$key = $this->hex2bin($key);
      $str = $this->hex2bin($str);
      $iv = $this->iv;

      $td = mcrypt_module_open(‘rijndael-128‘, ‘‘, ‘cbc‘, $iv);

      mcrypt_generic_init($td, $this->key, $iv);
      $decrypted = mdecrypt_generic($td, $str);

      mcrypt_generic_deinit($td);
      mcrypt_module_close($td);

      return utf8_encode(trim($decrypted));
    }

    protected function hex2bin($hexdata) {
      $bindata = ‘‘;

      for ($i = 0; $i < strlen($hexdata); $i += 2) {
            $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
      }

      return $bindata;
    }

demo

 

public function actionIndex()
    {
        $c=json_encode(‘{"state":"failed","message":"当前IP地址[119.90.13.163]:24小时次数[2],30天次数[2]","data":{"ip":"119.90.13.163","area":"中国北京北京","oneday":2,"allday":2}}‘);
        $a=$this->encryptlua($c);
        $b=json_decode($this->decryptlua($a));
        echo $a;
        echo ‘<br>‘;
        echo $b;

 

php aes加密

标签:个数   substr   init   bind   mcr   div   cti   ip地址   lld   

原文地址:http://www.cnblogs.com/xiong63/p/6801069.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!