PHP standard AES Encryption algorithm class

Source: Internet
Author: User

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

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.