首頁 > 後端開發 > php教程 > php aes cbc

php aes cbc

WBOY
發布: 2016-06-23 13:35:59
原創
1216 人瀏覽過

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
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板