php實現AES/CBC/PKCS5Padding加密解密

來源:互聯網
上載者:User

標籤:

<?phpclass MagicCrypt {private $iv = "0102030405060708";//密鑰位移量IVprivate $encryptKey = "58e15e87488a47c9";//AESkey//加密public function encrypt($encryptStr) {$localIV = $this->iv;$encryptKey = $this->encryptKey;//Open module$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, ‘‘, MCRYPT_MODE_CBC, $localIV);//print "module = $module <br/>" ;mcrypt_generic_init($module, $encryptKey, $localIV);//Padding$block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);$pad = $block - (strlen($encryptStr) % $block); //Compute how many characters need to pad$encryptStr .= str_repeat(chr($pad), $pad); // After pad, the str length must be equal to block or its integer multiples//encrypt$encrypted = mcrypt_generic($module, $encryptStr);//Closemcrypt_generic_deinit($module);mcrypt_module_close($module);return base64_encode($encrypted);}//解密public function decrypt($encryptStr) {$localIV = $this->iv;$encryptKey = $this->encryptKey;//Open module$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, ‘‘, MCRYPT_MODE_CBC, $localIV);//print "module = $module <br/>" ;mcrypt_generic_init($module, $encryptKey, $localIV);$encryptedData = base64_decode($encryptStr);$encryptedData = mdecrypt_generic($module, $encryptedData);return $encryptedData;}}$encryptString = ‘gz1DR+BsCzQe55HFdq1IiQ==‘;$encryptObj = new MagicCrypt();$result = $encryptObj->encrypt($encryptString);//加密結果$decryptString = $decryptString = $encryptObj->decrypt($result);//解密結果echo $result . "<br/>";echo $decryptString . "<br/>";?>

  

php實現AES/CBC/PKCS5Padding加密解密

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.