1. 程式人生 > >ecshop的aes加密(封裝)

ecshop的aes加密(封裝)

als 現在 sta use n) repeat require name close

從一家做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的加密,及傳輸。

歡迎大家提出問題,大家一起交流,一起成長。

ecshop的aes加密(封裝)