Share a standard PHP AES cryptographic algorithm class, where Mcrypt_get_block_size (' rijndael-128 ', ' ECB '), if it is easier to make a mistake if you do not understand the principle, you can pass the Mcrypt_list_ The algorithms function looks at the identity of the cryptographic algorithm you need.
<?PHP/** * AES128 and decryption class * @author dy **/defined(' Inejbuy ') orExit(' Access invalid! ');classaes{//secret key Private $_secrect_key; Public function__construct () {$this->_secrect_key = ' MYGGNQE2JDFADSFFDSEWSDD '; } /** * Encryption method * @param string $str * @return String*/ Public functionEncrypt$str){ //AES, ECB mode encrypted data $screct _key=$this-_secrect_key; $screct _key=Base64_decode($screct _key); $str=Trim($str); $str=$this->addpkcs7padding ($str); $iv= Mcrypt_create_iv (Mcrypt_get_iv_size (MCRYPT_RIJNDAEL_128,MCRYPT_MODE_ECB),Mcrypt_rand); $encrypt _str= Mcrypt_encrypt (mcrypt_rijndael_128,$screct _key,$str, MCRYPT_MODE_ECB,$iv); return Base64_encode($encrypt _str); } /** * Decryption method * @param string $str * @return String*/ Public functionDecrypt$str){ //AES, ECB mode encrypted data $screct _key=$this-_secrect_key; $str=Base64_decode($str); $screct _key=Base64_decode($screct _key); $iv= Mcrypt_create_iv (Mcrypt_get_iv_size (MCRYPT_RIJNDAEL_128,MCRYPT_MODE_ECB),Mcrypt_rand); $encrypt _str= Mcrypt_decrypt (mcrypt_rijndael_128,$screct _key,$str, MCRYPT_MODE_ECB,$iv); $encrypt _str=Trim($encrypt _str); $encrypt _str=$this->strippksc7padding ($encrypt _str); return $encrypt _str; } /** * Fill algorithm * @param string $source * @return String*/ functionAddpkcs7padding ($source){ $source=Trim($source); $block= Mcrypt_get_block_size (' rijndael-128 ', ' ECB '); $pad=$block- (strlen($source) %$block); if($pad<=$block) { $char=CHR($pad); $source.=str_repeat($char,$pad); } return $source; } /** * Remove fill algorithm * @param string $source * @return String*/ functionStrippksc7padding ($source){ $source=Trim($source); $char=substr($source,-1); $num=Ord($char); if($num==62)return $source; $source=substr($source, 0,-$num); return $source; }}
PHP standard AES Encryption algorithm class