• ecshop的aes加密(封装)


      从一家做shopex,ecstore的公司到一家做b2b的ecshop的公司...来了就要实战,其他的不说了,先来了解什么是php的aes加密吧?

      aes(高级加密标准),AES的区块长度固定为128 比特,密钥长度则可以是128,192或256比特;是一个可逆的加密方式,同md5不同。

      AES分为几种模式,比如ECB,CBC,CFB等等,这些模式除了ECB由于没有使用IV而不太安全,其他模式差别并没有太明显,大部分的区别在IV和KEY来计算密文的方法略有区别。

    iv的作用?

      IV称为初始向量,不同的IV加密后的字符串是不同的,加密和解密需要相同的IV,既然IV看起来和key一样,却还要多一个IV的目的,对于每个块来说,key是不变的,但是只有第一个块的IV是用户提供的,其他块IV都是自动生成。
      IV的长度为16字节。超过或者不足,可能实现的库都会进行补齐或截断。但是由于块的长度是16字节,所以一般可以认为需要的IV是16字节。

    到现在对aes有了一定的了解,就开始上代码吧。

    <?php
    
    class cryptaes{
        protected $cipher = MCRYPT_RIJNDAEL_128;
        protected $mode = MCRYPT_MODE_ECB;
        protected $pad_method = '';
        protected $secret_key = '';
        protected $iv = '';
     
        public function set_cipher($cipher)
        {
            $this->cipher = $cipher;
        }
     
        public function set_mode($mode)
        {
            $this->mode = $mode;
        }
     
        public function set_iv($iv)
        {
            $this->iv = $iv;
        }
     
        public function set_key($key)
        {
            $this->secret_key = $key;
        }
     
        public function require_pkcs5()
        {
            $this->pad_method = 'pkcs5';
        }
     
        protected function pad_or_unpad($str, $ext)
        {
            if ( is_null($this->pad_method) )
            {
                return $str;
            }
            else
            {
                $func_name = __CLASS__ . '::' . $this->pad_method . '_' . $ext . 'pad';
                if ( is_callable($func_name) )
                {
                    $size = mcrypt_get_block_size($this->cipher, $this->mode);
                    return call_user_func($func_name, $str, $size);
                }
            }
            return $str;
        }
     
        protected function pad($str)
        {
            return $this->pad_or_unpad($str, '');
        }
     
        protected function unpad($str)
        {
            return $this->pad_or_unpad($str, 'un');
        }
         //加密类
        public function encrypt($str)
        {
            print_r($str);
            $str = $this->pad($str);
            $td = mcrypt_module_open($this->cipher, '', $this->mode, '');
     
            if ( empty($this->iv) )
            {
                $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
            }
            else
            {
                $iv = $this->iv;
            }
     
            mcrypt_generic_init($td, $this->secret_key, $iv);
            $cyper_text = mcrypt_generic($td, $str);
            $rt=base64_encode($cyper_text);
            //$rt = bin2hex($cyper_text);
            mcrypt_generic_deinit($td);
            mcrypt_module_close($td);
     
            return $rt;
        }
         //解密类
        public function decrypt($str){
            $td = mcrypt_module_open($this->cipher, '', $this->mode, '');
     
            if ( empty($this->iv) )
            {
                $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
            }
            else
            {
                $iv = $this->iv;
            }
     
            mcrypt_generic_init($td, $this->secret_key, $iv);
            //$decrypted_text = mdecrypt_generic($td, self::hex2bin($str));
            $decrypted_text = mdecrypt_generic($td, base64_decode($str));
            $rt = $decrypted_text;
            mcrypt_generic_deinit($td);
            mcrypt_module_close($td);
     
            return $this->unpad($rt);
        }
     
        public static function hex2bin($hexdata) {
            $bindata = '';
            $length = strlen($hexdata);
            for ($i=0; $i < $length; $i += 2)
            {
                $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
            }
            return $bindata;
        }
     
        public static function pkcs5_pad($text, $blocksize)
        {
            $pad = $blocksize - (strlen($text) % $blocksize);
            return $text . str_repeat(chr($pad), $pad);
        }
     
        public static function pkcs5_unpad($text)
        {
            $pad = ord($text{strlen($text) - 1});
            if ($pad > strlen($text)) return false;
            if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false;
            return substr($text, 0, -1 * $pad);
        }
    }
    ?>

     aes加密解密封装类封装好了,在需要的地方加密:

    require_once(ROOT_PATH . 'includes/lib_smt_cryptaes.php'); //ecshop引入文件方式
            $aes_obj     = new cryptaes();
            $iv         = '12345678baiducom';
            $privateKey = '12345678baiducom';
    
            $data['a'] =  ‘周二’;
            $data['b'] =  ‘周三’;
            $data['c'] =  ‘周四’;
    
            $da = json_encode($data);
            $aes_obj->set_key($privateKey);
            $aes_obj->require_pkcs5();
            $aes_obj->set_iv($iv);
            $il = $aes_obj->encrypt($da);
            //写入cookie
            setcookie('il', $il,time()+360000); //加密结果$il

    这边我想传递的是一个数组,需要注意的是aes只能加密字符串。需要转换为字符串。

            $il = $_COOKIE['il'];
                require_once(ROOT_PATH . 'includes/lib_smt_cryptaes.php');
                $aes_obj     = new cryptaes();
                $keyStr     = '12345678baiducom';//密钥
                $iv         = '12345678baiducom';
    
                $aes_obj->set_key($keyStr);
                $aes_obj->require_pkcs5();
                $aes_obj->set_iv($iv);
                $j_token = $aes_obj->decrypt($il);
                $j_token = json_decode($j_token,true);    
           print_r($j_token);//解密结果

     这样就完成了aes的加密,及传输。

    欢迎大家提出问题,大家一起交流,一起成长。

    欢迎大家关注我的 订阅号:博客乐园

  • 相关阅读:
    程序员的成长
    存储系统的基本数据结构之一: 跳表 (SkipList)
    【机器学习-斯坦福】学习笔记3
    TCP/IP入门(3) --传输层
    2015华为暑期实习(北京)面试经验
    C++面试中关于sizeof问题总结
    KMP详解
    hihoCoder #1014 : Trie树
    Trie树的创建、插入、查询的实现
    Trie树(c++实现)
  • 原文地址:https://www.cnblogs.com/6kou/p/aes.html
Copyright © 2020-2023  润新知