wanglizhong
2025-05-05 2876dd05c0528ec665791a0844f643f566f7c31b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
 
/**
 * Class AES
 * 用于AES加解密数据
 * by:云客 【云游天下,做客四方】,微信号:php-world,欢迎转载,但须注明出处,讨论请加qq群203286137
 * time:2018-04-27
 */
class AES
{
    protected $cipher = MCRYPT_RIJNDAEL_256; //AES加密算法
    protected $mode = MCRYPT_MODE_CBC; //采用cbc加密模式
    protected $key; //密钥
    protected $iv; //cbc模式加密向量,如为空将采用密钥代替
 
    /**
     * AES constructor.
     *
     * @param      $key 密钥
     * @param null $iv 向量 可选 如为空将采用密钥代替
     *
     * @throws Exception
     */
    public function __construct($key, $iv = NULL)
    {
        if (!extension_loaded("mcrypt")) {
            throw new \Exception("mcrypt extension do not exist. it was DEPRECATED in PHP 7.1.0, and REMOVED in PHP 7.2.0. use OpenSSL instead");
        }
        $this->key = $key;
        $this->iv = $iv;
    }
 
    /**
     * 加密数据
     * @param $data
     *
     * @return string
     */
    public function encrypt($data)
    {
        $td = mcrypt_module_open($this->cipher, '', $this->mode, '');
        $key = hash("sha256", $this->key, true);
        $iv = isset($this->iv) ? hash("sha256", $this->iv, true) : $key;
        $data = $this->padding($data);
        mcrypt_generic_init($td, $key, $iv);
        $encryptedData = base64_encode(mcrypt_generic($td, $data));
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        return $encryptedData;
    }
 
    /**
     * 解密数据
     * @param $data
     *
     * @return bool|string
     */
    public function decrypt($data)
    {
        $td = mcrypt_module_open($this->cipher, '', $this->mode, '');
        $key = hash("sha256", $this->key, true);
        $iv = isset($this->iv) ? hash("sha256", $this->iv, true) : $key;
        mcrypt_generic_init($td, $key, $iv);
        $decrypted_data = mdecrypt_generic($td, base64_decode($data));
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        return $this->unPadding($decrypted_data);
    }
 
    /**
     * 填充数据到分组大小的整数倍
     * @param null $data
     *
     * @return string
     */
    protected function padding($data = null)
    {
        $blockSize = 32; //MCRYPT_RIJNDAEL_256算法的分组大小是32字节
        $pad = $blockSize - (strlen($data) % $blockSize);
        return $data . str_repeat(chr($pad), $pad);
    }
 
    /**
     * 去掉填充的数据
     * @param null $data
     *
     * @return bool|string
     */
    protected function unPadding($data = null)
    {
        $pad = ord($data[strlen($data) - 1]);
        if ($pad > strlen($data)) {
            return false;
        }
 
        if (strspn($data, chr($pad), strlen($data) - $pad) != $pad) {
            return false;
        }
 
        return substr($data, 0, -1 * $pad);
    }
 
    /**
     * @return mixed
     */
    public function getSecretKey()
    {
        return $this->key;
    }
 
    /**
     * @param mixed $key
     */
    public function setSecretKey($key)
    {
        $this->key = $key;
    }
    /**
     * @return null
     */
    public function getIv()
    {
        return $this->iv;
    }
 
    /**
     * @param null $iv
     */
    public function setIv($iv)
    {
        $this->iv = $iv;
    }
 
}
?>