本文实例讲述了PHP实现AES256加密算法的方法,是较为常见的一种加密算法。分享给大家供大家参考。具体如下:

aes.class.php文件如下:

<"htmlcode">
<"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,AES256,加密,算法

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com

评论“PHP实现AES256加密算法实例”

暂无“PHP实现AES256加密算法实例”评论...