当前位置:首页 > PHP教程 > php高级应用 > 列表

PHP如何实现AES加密、解密?方法介绍(代码示例)

发布:smiling 来源: PHP粉丝网  添加日期:2022-07-13 11:49:03 浏览: 评论:0 

1、mcrypt_encrypt AES加密,解密

  1. class Lib_desEnctyp 
  2.  
  3.  
  4.     private $key = ""
  5.  
  6.     private $iv = ""
  7.  
  8.  
  9.  
  10.     /** 
  11.  
  12.     * 构造,传递二个已经进行base64_encode的KEY与IV 
  13.  
  14.     * 
  15.  
  16.     * @param string $key 
  17.  
  18.     * @param string $iv 
  19.  
  20.     */ 
  21.  
  22.     function __construct ($key$iv
  23.  
  24.     { 
  25.  
  26.         if (emptyempty($key) || emptyempty($iv)) { 
  27.  
  28.             echo 'key and iv is not valid'
  29.  
  30.             exit(); 
  31.  
  32.         } 
  33.  
  34.         $this->key = $key
  35.  
  36.         $this->iv = $iv
  37.  
  38.     } 
  39.  
  40.  
  41.  
  42.     /** 
  43.  
  44.     *加密 
  45.  
  46.     * @param <type> $value 
  47.  
  48.     * @return <type> 
  49.  
  50.     */ 
  51.  
  52.     public function encrypt ($value
  53.  
  54.     { 
  55.  
  56.         $td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, ''); 
  57.  
  58.         $iv = base64_decode($this->iv); 
  59.  
  60.         $value = $this->PaddingPKCS7($value); 
  61.  
  62.         $key = base64_decode($this->key); 
  63.  
  64.         mcrypt_generic_init($td$key$iv); 
  65.  
  66.         $ret = base64_encode(mcrypt_generic($td$value)); 
  67.  
  68.         mcrypt_generic_deinit($td); 
  69.  
  70.         mcrypt_module_close($td); 
  71.  
  72.         return $ret
  73.  
  74.     } 
  75.  
  76.  
  77.  
  78.     /** 
  79.  
  80.     *解密 
  81.  
  82.     * @param <type> $value 
  83.  
  84.     * @return <type> 
  85.  
  86.     */ 
  87.  
  88.     public function decrypt ($value
  89.  
  90.     { 
  91.  
  92.         $td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, ''); 
  93.  
  94.         $iv = base64_decode($this->iv); 
  95.  
  96.         $key = base64_decode($this->key); 
  97.  
  98.         mcrypt_generic_init($td$key$iv); 
  99.  
  100.         $ret = trim(mdecrypt_generic($tdbase64_decode($value))); 
  101.  
  102.         $ret = $this->UnPaddingPKCS7($ret); 
  103.  
  104.         mcrypt_generic_deinit($td); 
  105.  
  106.         mcrypt_module_close($td); 
  107.  
  108.         return $ret
  109.  
  110.     } 
  111.  
  112.  
  113.  
  114.     private function PaddingPKCS7 ($data
  115.  
  116.     { 
  117.  
  118.         $block_size = mcrypt_get_block_size('tripledes''cbc'); 
  119.  
  120.         $padding_char = $block_size - (strlen($data) % $block_size); 
  121.  
  122.         $data .= str_repeat(chr($padding_char), $padding_char); 
  123.  
  124.         return $data
  125.  
  126.     } 
  127.  
  128.  
  129.  
  130.     private function UnPaddingPKCS7($text
  131.  
  132.     { 
  133.  
  134.         $pad = ord($text{strlen($text) - 1}); 
  135.  
  136.         if ($pad > strlen($text)) { 
  137.  
  138.             return false; 
  139.  
  140.         } 
  141.  
  142.         if (strspn($textchr($pad), strlen($text) - $pad) != $pad) { 
  143.  
  144.             return false; 
  145.  
  146.         } 
  147.  
  148.         return substr($text, 0, - 1 * $pad); 
  149.  
  150.     } 
  151.  

2、openssl 加密,解密 [方式1]

  1. /** 
  2.  
  3.  * DES加密类 
  4.  
  5.  * User: gaowei 
  6.  
  7.  * Date: 2017/12/12 
  8.  
  9.  * Time: 19:23 
  10.  
  11.  */ 
  12.  
  13. class DesEncrypt { 
  14.  
  15.     private $key = ""
  16.  
  17.     private $iv = ""
  18.  
  19.  
  20.  
  21.     /** 
  22.  
  23.      * 构造,传递二个已经进行base64_encode的KEY与IV 
  24.  
  25.      * 
  26.  
  27.      * @param string $key 
  28.  
  29.      * @param string $iv 
  30.  
  31.      */ 
  32.  
  33.     function __construct ($key$iv
  34.  
  35.     { 
  36.  
  37.         if (emptyempty($key) || emptyempty($iv)) { 
  38.  
  39.             echo 'key and iv is not valid'
  40.  
  41.             exit(); 
  42.  
  43.         } 
  44.  
  45.         $this->key = $key
  46.  
  47.         $this->iv = $iv;//8 
  48.  
  49.         //$this->iv = $iv.'00000000000';//16 
  50.  
  51.  
  52.  
  53.     } 
  54.  
  55.  
  56.  
  57.     /** 
  58.  
  59.      * @title 加密 
  60.  
  61.      * @author gaowei 
  62.  
  63.      * @date 2017/12/18 
  64.  
  65.      * @param string $value 要传的参数 
  66.  
  67.      * @ //OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING //AES-128-ECB|AES-256-CBC|BF-CBC 
  68.  
  69.      * @return json 
  70.  
  71.      * */ 
  72.  
  73.     public function encrypt ($value) { 
  74.  
  75.  
  76.  
  77.         //参考地址:https://stackoverflow.com/questions/41181905/php-mcrypt-encrypt-to-openssl-encrypt-and-openssl-zero-padding-problems# 
  78.  
  79.         $value = $this->PaddingPKCS7($value); 
  80.  
  81.         $key = base64_decode($this->key); 
  82.  
  83.         $iv  = base64_decode($this->iv); 
  84.  
  85.         //AES-128-ECB|不能用 AES-256-CBC|16 AES-128-CBC|16 BF-CBC|8 aes-128-gcm|需要加$tag  DES-EDE3-CBC|8 
  86.  
  87.         $cipher = "DES-EDE3-CBC"
  88.  
  89.         if (in_array($cipher, openssl_get_cipher_methods())) { 
  90.  
  91.             //$ivlen = openssl_cipher_iv_length($cipher); 
  92.  
  93.            // $iv = openssl_random_pseudo_bytes($ivlen); 
  94.  
  95.             $result = openssl_encrypt($value$cipher$key, OPENSSL_SSLV23_PADDING, $iv); 
  96.  
  97.             //$result = base64_encode($result); //为3的时间要用 
  98.  
  99.             //store $cipher, $iv, and $tag for decryption later 
  100.  
  101.            /* $original_plaintext = openssl_decrypt($result, $cipher, $key, OPENSSL_SSLV23_PADDING, $iv); 
  102.  
  103.             echo $original_plaintext."\n";*/ 
  104.  
  105.         } 
  106.  
  107.         return $result
  108.  
  109.  
  110.  
  111.     } 
  112.  
  113.     /** 
  114.  
  115.      * @title 解密 
  116.  
  117.      * @author gaowei 
  118.  
  119.      * @date 2017/12/18 
  120.  
  121.      * @param string $value 要传的参数 
  122.  
  123.      * @return json 
  124.  
  125.      * */ 
  126.  
  127.     public function decrypt ($value) { 
  128.  
  129.         $key       = base64_decode($this->key); 
  130.  
  131.         $iv        = base64_decode($this->iv); 
  132.  
  133.         $decrypted = openssl_decrypt($value'DES-EDE3-CBC'$key, OPENSSL_SSLV23_PADDING, $iv); 
  134.  
  135.         $ret = $this->UnPaddingPKCS7($decrypted); 
  136.  
  137.         return $ret
  138.  
  139.     } 
  140.  
  141.  
  142.  
  143.     private function PaddingPKCS7 ($data) { 
  144.  
  145.         //$block_size = mcrypt_get_block_size('tripledes', 'cbc');//获取长度 
  146.  
  147.         //$block_size = openssl_cipher_iv_length('tripledes', 'cbc');//获取长度 
  148.  
  149.         $block_size = 8; 
  150.  
  151.         $padding_char = $block_size - (strlen($data) % $block_size); 
  152.  
  153.         $data .= str_repeat(chr($padding_char), $padding_char); 
  154.  
  155.         return $data
  156.  
  157.     } 
  158.  
  159.     private function UnPaddingPKCS7($text) { 
  160.  
  161.         $pad = ord($text{strlen($text) - 1}); 
  162.  
  163.         if ($pad > strlen($text)) { 
  164.  
  165.             return false; 
  166.  
  167.         } 
  168.  
  169.         if (strspn($textchr($pad), strlen($text) - $pad) != $pad) { 
  170.  
  171.             return false; 
  172.  
  173.         } 
  174.  
  175.         return substr($text, 0, - 1 * $pad); 
  176.  
  177.     } 
  178.  

3、openssl 加密,解密 [方式2]

  1. /** 
  2.  
  3.  * @desc:php aes加密解密类 
  4.  
  5.  * @author gl 
  6.  
  7.  * @date 2019/08/31 
  8.  
  9.  */ 
  10.  
  11. class CI_Aes{ 
  12.  
  13.     /** 
  14.  
  15.      * CI_Aes cipher 
  16.  
  17.      * @var string 
  18.  
  19.      */ 
  20.  
  21.     protected $cipher = 'aes-128-ecb'
  22.  
  23.     /** 
  24.  
  25.      * CI_Aes key 
  26.  
  27.      * 
  28.  
  29.      * @var string 
  30.  
  31.      */ 
  32.  
  33.     protected $key
  34.  
  35.     /** 
  36.  
  37.      * CI_Aes constructor 
  38.  
  39.      * @param string $key Configuration parameter 
  40.  
  41.      */ 
  42.  
  43.  
  44.  
  45.     public function __construct($key=null){ 
  46.  
  47.         $this->key = $key
  48.  
  49.     } 
  50.  
  51.  
  52.  
  53.     /** 
  54.  
  55.      * Initialize 
  56.  
  57.      * 
  58.  
  59.      * @param array $params Configuration parameters 
  60.  
  61.      * @return CI_Encryption 
  62.  
  63.      */ 
  64.  
  65.     public function initialize($params
  66.  
  67.     { 
  68.  
  69.         if (!emptyempty($params) && is_array($params)) { 
  70.  
  71.             foreach ($params as $key => $val) { 
  72.  
  73.                 $this->$key = $val
  74.  
  75.             } 
  76.  
  77.         } 
  78.  
  79.     } 
  80.  
  81.     /** 
  82.  
  83.      * Encrypt 
  84.  
  85.      * 
  86.  
  87.      * @param string $data Input data 
  88.  
  89.      * @return string 
  90.  
  91.      */ 
  92.  
  93.     public function encrypt($data) { 
  94.  
  95.         $endata =  openssl_encrypt($data$this->cipher, $this->key, OPENSSL_RAW_DATA); 
  96.  
  97.         return  bin2hex($endata); 
  98.  
  99.     } 
  100.  
  101.  
  102.  
  103.     /** 
  104.  
  105.      * Decrypt 
  106.  
  107.      * 
  108.  
  109.      * @param string $data Encrypted data 
  110.  
  111.      * @return string 
  112.  
  113.      */ 
  114.  
  115.     public function decrypt($data) { 
  116.  
  117.         $encrypted = hex2bin($data); 
  118.  
  119.         return openssl_decrypt($encrypted$this->cipher, $this->key, OPENSSL_RAW_DATA); 
  120.  
  121.     } 
  122.  
  123.  
  124.  

4、其他 加密,解密

  1. //加密函数 
  2.  
  3. function lock_url($txt,$key='www.jb51.net'
  4.  
  5.  
  6.   $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+"
  7.  
  8.   $nh = rand(0,64); 
  9.  
  10.   $ch = $chars[$nh]; 
  11.  
  12.   $mdKey = md5($key.$ch); 
  13.  
  14.   $mdKey = substr($mdKey,$nh%8, $nh%8+7); 
  15.  
  16.   $txt = base64_encode($txt); 
  17.  
  18.   $tmp = ''
  19.  
  20.   $i=0;$j=0;$k = 0; 
  21.  
  22.   for ($i=0; $i<strlen($txt); $i++) { 
  23.  
  24.     $k = $k == strlen($mdKey) ? 0 : $k
  25.  
  26.     $j = ($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64; 
  27.  
  28.     $tmp .= $chars[$j]; 
  29.  
  30.   } 
  31.  
  32.   return urlencode($ch.$tmp); 
  33.  
  34.  
  35. //解密函数 
  36.  
  37. function unlock_url($txt,$key='www.jb51.net'
  38.  
  39.  
  40.   $txt = urldecode($txt); 
  41.  
  42.   $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+"
  43.  
  44.   $ch = $txt[0]; 
  45.  
  46.   $nh = strpos($chars,$ch); 
  47.  
  48.   $mdKey = md5($key.$ch); 
  49.  
  50.   $mdKey = substr($mdKey,$nh%8, $nh%8+7); 
  51.  
  52.   $txt = substr($txt,1); 
  53.  
  54.   $tmp = ''
  55.  
  56.   $i=0;$j=0; $k = 0; 
  57.  
  58.   for ($i=0; $i<strlen($txt); $i++) { 
  59.  
  60.     $k = $k == strlen($mdKey) ? 0 : $k
  61.  
  62.     $j = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]); 
  63.  
  64.     while ($j<0) $j+=64; 
  65.  
  66.     $tmp .= $chars[$j]; 
  67.  
  68.   } 
  69.  
  70.   return base64_decode($tmp); 
  71.  
  72. }

Tags: AES加密 AES解密

分享到: