Ecshop example of AES encryption (encapsulation) tutorial

Source: Internet
Author: User
From a company that makes Shopex,ecstore to a company that does business-to-business ecshop ... Come to the actual combat, the other do not say, first to understand what is the PHP AES encryption it?

AES (Advanced Encryption Standard), the block length of AES fixed to 128 bits, the key length can be 128,192 or 256 bit, is a reversible encryption method, different from MD5.

AES is divided into several modes, such as ECB,CBC,CFB, and so on, except that the ECB is not too safe to use IV, and the other modes are not too obvious, and most of the differences are slightly different in the method of calculating ciphertext with the IV and key.

The role of IV?

IV is called the initial vector, the different IV encryption string is different, the encryption and decryption need the same IV, since IV looks like key, but also the purpose of an IV, for each block, key is constant, but only the first block of the IV is provided by the user, the other block IV is automatically generated.
The length of IV is 16 bytes. Exceeded or insufficient, the library that may be implemented will be padded or truncated. However, since the block length is 16 bytes, it is generally considered that the required IV is 16 bytes.

Now that you have a certain understanding of AES, start the code.

<?phpclass cryptaes{protected $cipher = mcrypt_rijndael_128;protected $mode = mcrypt_mode_ecb;protected $pad _method = ';p rotected $secret _key = ';p rotected $iv = ';    Public Function Set_cipher ($cipher) {$this->cipher = $cipher;    The Public Function Set_mode ($mode) {$this->mode = $mode;    The Public Function Set_iv ($iv) {$this->iv = $iv;    The 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 CA            Ll_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 '); }//Cryptographic class Public function encrypt ($STR) {Print_r ($str), $str = $this->pad ($str); $td = Mcrypt_module_open ($this->c Ipher, ', $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 ($c        Yper_text);//$rt = Bin2Hex ($cyper _text); Mcrypt_generic_deinit ($TD); Mcrypt_module_close ($TD);    return $rt; }//Decryption class Public function decrypt ($str) {$td = Mcrypt_module_open ($this->cipher, "", $this->mode, "); if (Empty ($t        HIS-&GT;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; The public static function Pkcs5_pad ($text, $blocksize) {$pad = $blocksize-(strlen ($text)% $blocksize), and return $text.    Str_repeat (Chr ($pad), $pad); public static function Pkcs5_unpad ($text) {$pad = Ord ($text {strlen ($text)-1}), if ($pad > strlen ($text)) return F    Alse;if (STRSPN ($text, Chr ($pad), strlen ($text)-$pad)! = $pad) return False;return substr ($text, 0,-1 * $pad); }}?>

The AES cryptographic decryption encapsulation class is packaged and encrypted where it is needed:

Require_once (Root_path. ' includes/lib_smt_cryptaes.php '); Ecshop Introduction File mode $aes_obj     = new Cryptaes (); $iv         = ' 12345678baiducom '; $privateKey = ' 12345678baiducom '; $data [' A ' ] =  ' Tuesday '; $data [' b '] =  ' Wednesday '; $data [' c '] =  ' Thursday '; $da = Json_encode ($data); $aes _obj->set_key ($privateKey) , $aes _obj->require_pkcs5 (), $aes _obj->set_iv ($iv), $il = $aes _obj->encrypt ($DA);//write Cookiesetcookie (' il '), $il, Time () +360000); Encryption result $il

Here I want to pass an array, note that AES can only encrypt strings. needs to be converted to a string.

         = [' Il ' (root_path. ' includes/lib_smt_cryptaes.php '     = =      ' 12345678baiducom ';         = ' 12345678baiducom '->set_key (->->set_iv (=->decrypt (= Json_decode (,);    Print_r ($j _token);//decryption result

This completes the encryption and transmission of AES.

Welcome to ask questions, we communicate together, grow together.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.