PHP AES CBC

Source: Internet
Author: User

class aes_mcrypt {    private  $hex _iv =  ' 00000000000000000000000000000000 '; # converted java byte code in to  Hex and placed it here    function __construct ($key)  {          $this->key = hash (' sha256 ', $key,  true);     }    function encrypt ($STR)  {          $td  = mcrypt_module_open (mcrypt_rijndael_128,  ', mcrypt_mode_cbc,          mcrypt_generic_init ($td,  $this->key,  $this- >hextostr ($this->hex_iv));         //$block  = mcrypt_ Get_block_size (MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);         $ Block = mcrypt_enc_get_block_size ($TD);          $str  =  $this->addpadding ($str, $block);          $encrypted  = mcrypt_generic ($td,  $str);         mcrypt_generic_deinit ($TD);         Mcrypt_module_close ($TD);         return base64_encode ($encrypted) ;     }    function decrypt ($code)  {          $td  = mcrypt_module_open (mcrypt_rijndael_128,  ",  MCRYPT_MODE_CBC,           mcrypt_generic_init ($td,  $this->key, $ This->hextostr ($this->hex_iv));         $str  = mdecrypt _generic ($TD,  base64_decode ($code));         mcrypt_generic_deinit ($TD);   &nBsp;     mcrypt_module_close ($TD);         return  $this->strippadding ($STR);    }    /*       for pkcs7 padding     */    private  function addpadding ($string,  $blocksize  = 16)  {          $len  = strlen ($string);         $pad  =   $blocksize  -  ($len  %  $blocksize);         $ String .= str_repeat (Chr ($pad),  $pad);         return   $string;     }    private function strippadding ($string )  {         $slast  = ord (substr ($string,  -1));          $SLASTC  = chr ($slast);         $pcheck  = substr ($string,  -$slast);        if  (preg_match ("/$SLASTC {"  .  $slast  .  "}/",  $string))  {             $string  = substr ($ String, 0, strlen ($string)  -  $slast);             return  $string;         } else {             return false;         }    }    public function  Hextostr ($hex)     {         $string = ';         for  ($i =0;  $i  <&NBSp;strlen ($hex) -1;  $i +=2)         {              $string  .= chr (Hexdec ($hex [$i]. $hex [$i +1]);         }        return  $string;     }}


PHP AES CBC

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.