欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php aes cbc

程序员文章站 2022-04-08 22:21:29
...
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);        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 
相关标签: php aes cbc