php aes cbc

WBOY
发布: 2016-06-23 13:35:59
原创
1184 人浏览过

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 < strlen($hex)-1; $i+=2)        {            $string .= chr(hexdec($hex[$i].$hex[$i+1]));        }        return $string;    }}
登录后复制


相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!