"htmlcode">

/** 
 * 常用对称加密算法类 
 * 支持密钥:64/128/256 bit(字节长度8/16/32) 
 * 支持算法:DES/AES(根据密钥长度自动匹配使用:DES:64bit AES:128/256bit) 
 * 支持模式:CBC/ECB/OFB/CFB 
 * 密文编码:base64字符串/十六进制字符串/二进制字符串流 
 * 填充方式: PKCS5Padding(DES) 
 * 
 * @author: linvo 
 * @version: 1.0.0 
 * @date: 2013/1/10 
 */  
class Xcrypt{  
  
  private $mcrypt;  
  private $key;  
  private $mode;  
  private $iv;  
  private $blocksize;  
  
  /** 
   * 构造函数 
   * 
   * @param string 密钥 
   * @param string 模式 
   * @param string 向量("off":不使用 / "auto":自动 / 其他:指定值,长度同密钥) 
   */  
  public function __construct($key, $mode = 'cbc', $iv = "off"){  
    switch (strlen($key)){  
    case 8:  
      $this->mcrypt = MCRYPT_DES;  
      break;  
    case 16:  
      $this->mcrypt = MCRYPT_RIJNDAEL_128;  
      break;  
    case 32:  
      $this->mcrypt = MCRYPT_RIJNDAEL_256;  
      break;  
    default:  
      die("Key size must be 8/16/32");  
    }  
  
    $this->key = $key;  
  
    switch (strtolower($mode)){  
    case 'ofb':  
      $this->mode = MCRYPT_MODE_OFB;  
      if ($iv == 'off') die('OFB must give a IV'); //OFB必须有向量  
      break;  
    case 'cfb':  
      $this->mode = MCRYPT_MODE_CFB;  
      if ($iv == 'off') die('CFB must give a IV'); //CFB必须有向量  
      break;  
    case 'ecb':  
      $this->mode = MCRYPT_MODE_ECB;  
      $iv = 'off'; //ECB不需要向量  
      break;  
    case 'cbc':  
    default:  
      $this->mode = MCRYPT_MODE_CBC;  
    }  
  
    switch (strtolower($iv)){  
    case "off":  
      $this->iv = null;  
      break;  
    case "auto":  
      $source = PHP_OS=='WINNT' "base64"){    
    $ret = false;  
  
    switch ($code){  
    case 'base64':  
      $str = base64_decode($str);  
      break;  
    case 'hex':  
      $str = $this->_hex2bin($str);  
      break;  
    case 'bin':  
    default:  
    }  
  
    if ($str !== false){  
      if (isset($this->iv)) {  
        $ret = mcrypt_decrypt($this->mcrypt, $this->key, $str, $this->mode, $this->iv);   
      } else {  
        @$ret = mcrypt_decrypt($this->mcrypt, $this->key, $str, $this->mode);   
      }  
      if ($this->mcrypt == MCRYPT_DES) $ret = $this->_pkcs5Unpad($ret);  
    }  
  
    return $ret;   
  }   
  
  private function _pkcs5Pad($text){  
    $this->blocksize = mcrypt_get_block_size($this->mcrypt, $this->mode);   
    $pad = $this->blocksize - (strlen($text) % $this->blocksize);  
    return $text . str_repeat(chr($pad), $pad);  
  }  
  
  private function _pkcs5Unpad($text){  
    $pad = ord($text{strlen($text) - 1});  
    if ($pad > strlen($text)) return false;  
    if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false;  
    $ret = substr($text, 0, -1 * $pad);  
    return $ret;  
  }  
  
  private function _hex2bin($hex = false){  
    $ret = $hex !== false && preg_match('/^[0-9a-fA-F]+$/i', $hex) "H*", $hex) : false;    
    return $ret;  
  }  
  
}  

使用实例

<"xcrypt.php";  
  
echo '<pre>';  
//////////////////////////////////////  
$a = isset($_GET['a']) ? $_GET['a'] : '测试123';  
  
//密钥  
$key = '12345678123456781234567812345678'; //256 bit  
$key = '1234567812345678'; //128 bit  
$key = '12345678'; //64 bit  
  
//设置模式和IV  
$m = new Xcrypt($key, 'cbc', 'auto');  
  
//获取向量值  
echo '向量:';  
var_dump($m->getIV());  
  
//加密  
$b = $m->encrypt($a, 'base64');  
//解密  
$c = $m->decrypt($b, 'base64');  
  
echo '加密后:';  
var_dump($b);  
echo '解密后:';  
var_dump($c);  
  
/////////////////////////////////////////  
echo '</pre>'; 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
PHP对称加密算法,PHP,DES对称加密算法,PHP,AES对称加密算法

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