DDR爱好者之家 Design By 杰米

本文实例讲述了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程序设计的学习有所帮助。

DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米