Chinaunix首页 | 论坛 | 博客
  • 博客访问: 893362
  • 博文数量: 91
  • 博客积分: 803
  • 博客等级: 准尉
  • 技术积分: 1051
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-24 13:42
文章分类

全部博文(91)

文章存档

2021年(1)

2020年(4)

2019年(4)

2018年(9)

2017年(11)

2016年(11)

2015年(6)

2014年(3)

2013年(28)

2012年(14)

分类: PHP

2014-12-09 15:36:36

  1. /** 
  2.  * 常用对称加密算法类 
  3.  * 支持密钥:64/128/256 bit(字节长度8/16/32) 
  4.  * 支持算法:DES/AES(根据密钥长度自动匹配使用:DES:64bit AES:128/256bit) 
  5.  * 支持模式:CBC/ECB/OFB/CFB 
  6.  * 密文编码:base64字符串/十六进制字符串/二进制字符串流 
  7.  * 填充方式: PKCS5Padding(DES) 
  8.  * 
  9.  * @author: linvo 
  10.  * @version: 1.0.0 
  11.  * @date: 2013/1/10 
  12.  */  
  13. class Xcrypt{  
  14.   
  15.     private $mcrypt;  
  16.     private $key;  
  17.     private $mode;  
  18.     private $iv;  
  19.     private $blocksize;  
  20.   
  21.     /** 
  22.      * 构造函数 
  23.      * 
  24.      * @param string 密钥 
  25.      * @param string 模式 
  26.      * @param string 向量("off":不使用 / "auto":自动 / 其他:指定值,长度同密钥) 
  27.      */  
  28.     public function __construct($key$mode = 'cbc'$iv = "off"){  
  29.         switch (strlen($key)){  
  30.         case 8:  
  31.             $this->mcrypt = MCRYPT_DES;  
  32.             break;  
  33.         case 16:  
  34.             $this->mcrypt = MCRYPT_RIJNDAEL_128;  
  35.             break;  
  36.         case 32:  
  37.             $this->mcrypt = MCRYPT_RIJNDAEL_256;  
  38.             break;  
  39.         default:  
  40.             die("Key size must be 8/16/32");  
  41.         }  
  42.   
  43.         $this->key = $key;  
  44.   
  45.         switch (strtolower($mode)){  
  46.         case 'ofb':  
  47.             $this->mode = MCRYPT_MODE_OFB;  
  48.             if ($iv == 'off'die('OFB must give a IV'); //OFB必须有向量  
  49.             break;  
  50.         case 'cfb':  
  51.             $this->mode = MCRYPT_MODE_CFB;  
  52.             if ($iv == 'off'die('CFB must give a IV'); //CFB必须有向量  
  53.             break;  
  54.         case 'ecb':  
  55.             $this->mode = MCRYPT_MODE_ECB;  
  56.             $iv = 'off'//ECB不需要向量  
  57.             break;  
  58.         case 'cbc':  
  59.         default:  
  60.             $this->mode = MCRYPT_MODE_CBC;  
  61.         }  
  62.   
  63.         switch (strtolower($iv)){  
  64.         case "off":  
  65.             $this->iv = null;  
  66.             break;  
  67.         case "auto":  
  68.             $source = PHP_OS=='WINNT' ? MCRYPT_RAND : MCRYPT_DEV_RANDOM;  
  69.             $this->iv = mcrypt_create_iv(mcrypt_get_block_size($this->mcrypt, $this->mode), $source);  
  70.             break;  
  71.         default:  
  72.             $this->iv = $iv;  
  73.         }  
  74.   
  75.      
  76.     }  
  77.   
  78.   
  79.     /** 
  80.      * 获取向量值 
  81.      * @param string 向量值编码(base64/hex/bin) 
  82.      * @return string 向量值 
  83.      */  
  84.     public function getIV($code = 'base64'){  
  85.         switch ($code){  
  86.         case 'base64':  
  87.             $ret = base64_encode($this->iv);  
  88.             break;  
  89.         case 'hex':  
  90.             $ret = bin2hex($this->iv);  
  91.             break;  
  92.         case 'bin':  
  93.         default:  
  94.             $ret = $this->iv;  
  95.         }  
  96.         return $ret;  
  97.     }  
  98.   
  99.   
  100.     /** 
  101.      * 加密 
  102.      * @param string 明文 
  103.      * @param string 密文编码(base64/hex/bin) 
  104.      * @return string 密文 
  105.      */  
  106.     public function encrypt($str$code = 'base64'){  
  107.         if ($this->mcrypt == MCRYPT_DES) $str = $this->_pkcs5Pad($str);  
  108.   
  109.         if (isset($this->iv)) {  
  110.             $result = mcrypt_encrypt($this->mcrypt, $this->key, $str$this->mode, $this->iv);    
  111.         } else {  
  112.             @$result = mcrypt_encrypt($this->mcrypt, $this->key, $str$this->mode);    
  113.         }  
  114.   
  115.         switch ($code){  
  116.         case 'base64':  
  117.             $ret = base64_encode($result);  
  118.             break;  
  119.         case 'hex':  
  120.             $ret = bin2hex($result);  
  121.             break;  
  122.         case 'bin':  
  123.         default:  
  124.             $ret = $result;  
  125.         }  
  126.    
  127.         return $ret;  
  128.    
  129.     }  
  130.   
  131.     /** 
  132.      * 解密  
  133.      * @param string 密文 
  134.      * @param string 密文编码(base64/hex/bin) 
  135.      * @return string 明文 
  136.      */  
  137.     public function decrypt($str$code = "base64"){      
  138.         $ret = false;  
  139.   
  140.         switch ($code){  
  141.         case 'base64':  
  142.             $str = base64_decode($str);  
  143.             break;  
  144.         case 'hex':  
  145.             $str = $this->_hex2bin($str);  
  146.             break;  
  147.         case 'bin':  
  148.         default:  
  149.         }  
  150.   
  151.         if ($str !== false){  
  152.             if (isset($this->iv)) {  
  153.                 $ret = mcrypt_decrypt($this->mcrypt, $this->key, $str$this->mode, $this->iv);    
  154.             } else {  
  155.                 @$ret = mcrypt_decrypt($this->mcrypt, $this->key, $str$this->mode);    
  156.             }  
  157.             if ($this->mcrypt == MCRYPT_DES) $ret = $this->_pkcs5Unpad($ret);  
  158.             $ret = trim($ret);  
  159.         }  
  160.   
  161.         return $ret;   
  162.     }   
  163.   
  164.   
  165.   
  166.     private function _pkcs5Pad($text){  
  167.         $this->blocksize = mcrypt_get_block_size($this->mcrypt, $this->mode);    
  168.         $pad = $this->blocksize - (strlen($text) % $this->blocksize);  
  169.         return $text . str_repeat(chr($pad), $pad);  
  170.     }  
  171.   
  172.     private function _pkcs5Unpad($text){  
  173.         $pad = ord($text{strlen($text) - 1});  
  174.         if ($pad > strlen($text)) return false;  
  175.         if (strspn($textchr($pad), strlen($text) - $pad) != $padreturn false;  
  176.         $ret = substr($text, 0, -1 * $pad);  
  177.         return $ret;  
  178.     }  
  179.   
  180.     private function _hex2bin($hex = false){  
  181.         $ret = $hex !== false && preg_match('/^[0-9a-fA-F]+$/i'$hex) ? pack("H*"$hex) : false;      
  182.         return $ret;  
  183.     }  
  184.   
  185.   
  186.   
  187. }  

给个小demo吧
  1. header('Content-Type:text/html;Charset=utf-8;');  
  2.   
  3. include "xcrypt.php";  
  4.   
  5. echo '
    ';  
    		
  6. //////////////////////////////////////  
  7. $a = isset($_GET['a']) ? $_GET['a'] : '测试123';  
  8.   
  9. //密钥  
  10. $key = '12345678123456781234567812345678'//256 bit  
  11. $key = '1234567812345678'//128 bit  
  12. $key = '12345678'//64 bit  
  13.   
  14. //设置模式和IV  
  15. $m = new Xcrypt($key'cbc''auto');  
  16.   
  17. //获取向量值  
  18. echo '向量:';  
  19. var_dump($m->getIV());  
  20.   
  21. //加密  
  22. $b = $m->encrypt($a'base64');  
  23. //解密  
  24. $c = $m->decrypt($b'base64');  
  25.   
  26. echo '加密后:';  
  27. var_dump($b);  
  28. echo '解密后:';  
  29. var_dump($c);  
  30.   
  31.   
  32. /////////////////////////////////////////  
  33. echo ''
阅读(3583) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~