PHP实现AES256加密算法实例
(编辑:jimmy 日期: 2024/11/20 浏览:3 次 )
本文实例讲述了PHP实现AES256加密算法的方法,是较为常见的一种加密算法。分享给大家供大家参考。具体如下:
aes.class.php文件如下:
<"htmlcode">上一篇:php实现的发送带附件邮件类实例<"htmlcode"><"Encrypt String : $mstr<br />"; $dstr = AesCtr::decrypt($mstr, 'key', 256); echo "Decrypt String : $dstr<br />"; echo 'each not change<br>'; $mstr = AesCtr::encrypt('Hello World', 'key', 256, 1); // keep=1 echo "Encrypt String : $mstr<br />"; $dstr = AesCtr::decrypt($mstr, 'key', 256); echo "Decrypt String : $dstr<br />"; "htmlcode">/* aes 256 encrypt * @param String $ostr * @param String $securekey * @param String $type encrypt, decrypt */ function aes($ostr, $securekey, $type='encrypt'){ if($ostr==''){ return ''; } $key = $securekey; $iv = strrev($securekey); $td = mcrypt_module_open('rijndael-256', '', 'ofb', ''); mcrypt_generic_init($td, $key, $iv); $str = ''; switch($type){ case 'encrypt': $str = base64_encode(mcrypt_generic($td, $ostr)); break; case 'decrypt': $str = mdecrypt_generic($td, base64_decode($ostr)); break; } mcrypt_generic_deinit($td); return $str; } // Demo $key = "fdipzone201314showmethemoney!@#$"; $str = "show me the money"; $ostr = aes($str, $key); echo "String 1: $ostr<br />"; $dstr = aes($ostr, $key, 'decrypt'); echo "String 2: $dstr<br />";希望本文所述对大家php程序设计的学习有所帮助。
下一篇:php生成QRcode实例