Home  >  Article  >  Backend Development  >  Example tutorial of ecshop's aes encryption (encapsulation)

Example tutorial of ecshop's aes encryption (encapsulation)

零下一度
零下一度Original
2017-06-28 10:38:511621browse

From a company that does shopex and ecstore to a company that does b2b ecshop... It’s time to put it into practice. Let’s not talk about other things. Let’s first understand what PHP’s AES encryption is?

aes (Advanced Encryption Standard), the block length of AES is fixed at 128 bits, and the key length can be 128, 192 or 256 bits; it is a reversible encryption method, different from md5.

AES is divided into several modes, such as ECB, CBC, CFB, etc. Except for ECB, which is not very safe because it does not use IV, the differences between other modes are not too obvious. Most of the differences are between IV and The method of KEY to calculate ciphertext is slightly different.

What is the role of iv?

IV is called the initial vector. The encrypted strings of different IVs are different. Encryption and decryption require the same IV. Since the IV looks the same as the key, there is one more IV. For the purpose For each block, the key remains unchanged, but only the IV of the first block is provided by the user, and other block IVs are automatically generated.
The length of IV is 16 bytes. If it exceeds or is insufficient, the libraries that may be implemented will complete or truncate it. But since the length of the block is 16 bytes, it can generally be considered that the required IV is 16 bytes.

Now that I have a certain understanding of aes, let’s start coding.

<?phpclass cryptaes{protected $cipher = MCRYPT_RIJNDAEL_128;protected $mode = MCRYPT_MODE_ECB;protected $pad_method = &#39;&#39;;protected $secret_key = &#39;&#39;;protected $iv = &#39;&#39;; 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);
    }
}?>

The aes encryption and decryption packaging class is encapsulated and encrypted where needed:

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);//写入cookiesetcookie('il', $il,time()+360000); //加密结果$il

What I want to pass here is an array , it should be noted that aes can only encrypt strings. Needs to be converted to string.

         = ['il'(ROOT_PATH . 'includes/lib_smt_cryptaes.php'     =      = '12345678baiducom';         = '12345678baiducom'->set_key(->->set_iv( = ->decrypt( = json_decode(,);    
       print_r($j_token);//解密结果

This completes the encryption and transmission of aes.

We welcome everyone to ask questions, communicate and grow together.

The above is the detailed content of Example tutorial of ecshop's aes encryption (encapsulation). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn