zoukankan      html  css  js  c++  java
  • 常用对称加密算法(DES/AES)类(PHP)

    看注释,啥也不说了,欢迎各种跨平台测试!

    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($text, chr($pad), strlen($text) - $pad) != $pad) return 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. <?php  
      2. header('Content-Type:text/html;Charset=utf-8;');  
      3.   
      4. include "xcrypt.php";  
      5.   
      6. echo '<pre>';  
      7. //////////////////////////////////////  
      8. $a = isset($_GET['a']) ? $_GET['a'] : '测试123';  
      9.   
      10. //密钥  
      11. $key = '12345678123456781234567812345678'; //256 bit  
      12. $key = '1234567812345678'; //128 bit  
      13. $key = '12345678'; //64 bit  
      14.   
      15. //设置模式和IV  
      16. $m = new Xcrypt($key, 'cbc', 'auto');  
      17.   
      18. //获取向量值  
      19. echo '向量:';  
      20. var_dump($m->getIV());  
      21.   
      22. //加密  
      23. $b = $m->encrypt($a, 'base64');  
      24. //解密  
      25. $c = $m->decrypt($b, 'base64');  
      26.   
      27. echo '加密后:';  
      28. var_dump($b);  
      29. echo '解密后:';  
      30. var_dump($c);  
      31.   
      32.   
      33. /////////////////////////////////////////  
      34. echo '</pre>';  
  • 相关阅读:
    k8s安装
    jinja +grains 取变量
    项目跨域问题
    Node.js做Web后端优势为什么这么大?
    Chrome使用技巧
    看完这篇操作系统吊打面试官
    几款国产操作系统的区别
    如果红军是一家公司,依然是最后的大赢家
    RPA AI .NET Core 与未来--学习笔记
    Oracle中MERGE INTO用法解析
  • 原文地址:https://www.cnblogs.com/kenshinobiy/p/4424195.html
Copyright © 2011-2022 走看看