PHP实现AES加密解密

两盒软妹~` 提交于 2019-12-02 21:30:38
1、mcrypt_encrypt AES加密,解密
 1 class Lib_desEnctyp
 2 {
 3     private $key = "";
 4     private $iv = "";
 5  
 6     /**
 7     * 构造,传递二个已经进行base64_encode的KEY与IV
 8     *
 9     * @param string $key
10     * @param string $iv
11     */
12     function __construct ($key, $iv)
13     {
14         if (empty($key) || empty($iv)) {
15             echo 'key and iv is not valid';
16             exit();
17         }
18         $this->key = $key;
19         $this->iv = $iv;
20     }
21  
22     /**
23     *加密
24     * @param <type> $value
25     * @return <type>
26     */
27     public function encrypt ($value)
28     {
29         $td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, '');
30         $iv = base64_decode($this->iv);
31         $value = $this->PaddingPKCS7($value);
32         $key = base64_decode($this->key);
33         mcrypt_generic_init($td, $key, $iv);
34         $ret = base64_encode(mcrypt_generic($td, $value));
35         mcrypt_generic_deinit($td);
36         mcrypt_module_close($td);
37         return $ret;
38     }
39  
40     /**
41     *解密
42     * @param <type> $value
43     * @return <type>
44     */
45     public function decrypt ($value)
46     {
47         $td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, '');
48         $iv = base64_decode($this->iv);
49         $key = base64_decode($this->key);
50         mcrypt_generic_init($td, $key, $iv);
51         $ret = trim(mdecrypt_generic($td, base64_decode($value)));
52         $ret = $this->UnPaddingPKCS7($ret);
53         mcrypt_generic_deinit($td);
54         mcrypt_module_close($td);
55         return $ret;
56     }
57  
58     private function PaddingPKCS7 ($data)
59     {
60         $block_size = mcrypt_get_block_size('tripledes', 'cbc');
61         $padding_char = $block_size - (strlen($data) % $block_size);
62         $data .= str_repeat(chr($padding_char), $padding_char);
63         return $data;
64     }
65  
66     private function UnPaddingPKCS7($text)
67     {
68         $pad = ord($text{strlen($text) - 1});
69         if ($pad > strlen($text)) {
70             return false;
71         }
72         if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {
73             return false;
74         }
75         return substr($text, 0, - 1 * $pad);
76     }
77 }

 

2、openssl 加密,解密 [方式1]
 1 /**
 2  * DES加密类
 3  * User: gaowei
 4  * Date: 2017/12/12
 5  * Time: 19:23
 6  */
 7 class DesEncrypt {
 8     private $key = "";
 9     private $iv = "";
10  
11     /**
12      * 构造,传递二个已经进行base64_encode的KEY与IV
13      *
14      * @param string $key
15      * @param string $iv
16      */
17     function __construct ($key, $iv)
18     {
19         if (empty($key) || empty($iv)) {
20             echo 'key and iv is not valid';
21             exit();
22         }
23         $this->key = $key;
24         $this->iv = $iv;//8
25         //$this->iv = $iv.'00000000000';//16
26  
27     }
28  
29     /**
30      * @title 加密
31      * @author gaowei
32      * @date 2017/12/18
33      * @param string $value 要传的参数
34      * @ //OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING //AES-128-ECB|AES-256-CBC|BF-CBC
35      * @return json
36      * */
37     public function encrypt ($value) {
38  
39         //参考地址:https://stackoverflow.com/questions/41181905/php-mcrypt-encrypt-to-openssl-encrypt-and-openssl-zero-padding-problems#
40         $value = $this->PaddingPKCS7($value);
41         $key = base64_decode($this->key);
42         $iv  = base64_decode($this->iv);
43         //AES-128-ECB|不能用 AES-256-CBC|16 AES-128-CBC|16 BF-CBC|8 aes-128-gcm|需要加$tag  DES-EDE3-CBC|8
44         $cipher = "DES-EDE3-CBC";
45         if (in_array($cipher, openssl_get_cipher_methods())) {
46             //$ivlen = openssl_cipher_iv_length($cipher);
47            // $iv = openssl_random_pseudo_bytes($ivlen);
48             $result = openssl_encrypt($value, $cipher, $key, OPENSSL_SSLV23_PADDING, $iv);
49             //$result = base64_encode($result); //为3的时间要用
50             //store $cipher, $iv, and $tag for decryption later
51            /* $original_plaintext = openssl_decrypt($result, $cipher, $key, OPENSSL_SSLV23_PADDING, $iv);
52             echo $original_plaintext."\n";*/
53         }
54         return $result;
55  
56     }
57     /**
58      * @title 解密
59      * @author gaowei
60      * @date 2017/12/18
61      * @param string $value 要传的参数
62      * @return json
63      * */
64     public function decrypt ($value) {
65         $key       = base64_decode($this->key);
66         $iv        = base64_decode($this->iv);
67         $decrypted = openssl_decrypt($value, 'DES-EDE3-CBC', $key, OPENSSL_SSLV23_PADDING, $iv);
68         $ret = $this->UnPaddingPKCS7($decrypted);
69         return $ret;
70     }
71  
72     private function PaddingPKCS7 ($data) {
73         //$block_size = mcrypt_get_block_size('tripledes', 'cbc');//获取长度
74         //$block_size = openssl_cipher_iv_length('tripledes', 'cbc');//获取长度
75         $block_size = 8;
76         $padding_char = $block_size - (strlen($data) % $block_size);
77         $data .= str_repeat(chr($padding_char), $padding_char);
78         return $data;
79     }
80     private function UnPaddingPKCS7($text) {
81         $pad = ord($text{strlen($text) - 1});
82         if ($pad > strlen($text)) {
83             return false;
84         }
85         if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {
86             return false;
87         }
88         return substr($text, 0, - 1 * $pad);
89     }
90 }

 

3、openssl 加密,解密 [方式2]
 1 /**
 2  * @desc:php aes加密解密类 
 3  * @author gl
 4  * @date 2019/08/31
 5  */
 6 class CI_Aes{
 7     /**
 8      * CI_Aes cipher
 9      * @var string
10      */
11     protected $cipher = 'aes-128-ecb';
12     /**
13      * CI_Aes key
14      *
15      * @var string
16      */
17     protected $key;
18     /**
19      * CI_Aes constructor
20      * @param string $key Configuration parameter
21      */
22  
23     public function __construct($key=null){
24         $this->key = $key;
25     }
26  
27     /**
28      * Initialize
29      *
30      * @param array $params Configuration parameters
31      * @return CI_Encryption
32      */
33     public function initialize($params)
34     {
35         if (!empty($params) && is_array($params)) {
36             foreach ($params as $key => $val) {
37                 $this->$key = $val;
38             }
39         }
40     }
41     /**
42      * Encrypt
43      *
44      * @param string $data Input data
45      * @return string
46      */
47     public function encrypt($data) {
48         $endata =  openssl_encrypt($data, $this->cipher, $this->key, OPENSSL_RAW_DATA);
49         return  bin2hex($endata);
50     }
51  
52     /**
53      * Decrypt
54      *
55      * @param string $data Encrypted data
56      * @return string
57      */
58     public function decrypt($data) {
59         $encrypted = hex2bin($data);
60         return openssl_decrypt($encrypted, $this->cipher, $this->key, OPENSSL_RAW_DATA);
61     }
62  
63 }

 

4、其他 加密,解密
 1 //加密函数
 2 function lock_url($txt,$key='www.jb51.net')
 3 {
 4   $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
 5   $nh = rand(0,64);
 6   $ch = $chars[$nh];
 7   $mdKey = md5($key.$ch);
 8   $mdKey = substr($mdKey,$nh%8, $nh%8+7);
 9   $txt = base64_encode($txt);
10   $tmp = '';
11   $i=0;$j=0;$k = 0;
12   for ($i=0; $i<strlen($txt); $i++) {
13     $k = $k == strlen($mdKey) ? 0 : $k;
14     $j = ($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;
15     $tmp .= $chars[$j];
16   }
17   return urlencode($ch.$tmp);
18 }
19 //解密函数
20 function unlock_url($txt,$key='www.jb51.net')
21 {
22   $txt = urldecode($txt);
23   $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
24   $ch = $txt[0];
25   $nh = strpos($chars,$ch);
26   $mdKey = md5($key.$ch);
27   $mdKey = substr($mdKey,$nh%8, $nh%8+7);
28   $txt = substr($txt,1);
29   $tmp = '';
30   $i=0;$j=0; $k = 0;
31   for ($i=0; $i<strlen($txt); $i++) {
32     $k = $k == strlen($mdKey) ? 0 : $k;
33     $j = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]);
34     while ($j<0) $j+=64;
35     $tmp .= $chars[$j];
36   }
37   return base64_decode($tmp);
38 }

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!