PHP AES CBC

Source: Internet
Author: User
Class Aes_mcrypt {private $hex _iv = ' 00000000000000000000000000000000 '; # converted JAVA byte code in to hex and place    D 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);        Mcrypt_module_close ($TD);    return $this->strippadding ($STR); }/* for PKCS7 padding */Private Function addpadding ($string, $blocksize = +) {$len = strlen ($str        ing);        $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 < strlen ($hex)-1; $i +=2) {$string. = Chr (Hexdec ($hex [$i]. $hex [$i +1]);    } return $string; }}
  • 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.