AES CBC

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

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
class AES_CBC_NoPadding
{
    private $iv;
    private $key;
    private $blocksize;
     
    public function __construct($key, $iv)
    {
        $this->blocksize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
        $this->key = substr(md5($key), 0, $this->blocksize);
        $this->iv = substr(md5($iv), 0, $this->blocksize);
    }
     
    public function encrypt($text)
    {
        $pad = $this->blocksize - strlen($text)%$this->blocksize;
        $text = str_pad($text, strlen($text) + $pad, "\0");
        return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->key, $text, MCRYPT_MODE_CBC, $this->iv));
    }
     
    public function decrypt($text)
    {
        $text = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $this->key, base64_decode($text), MCRYPT_MODE_CBC, $this->iv);
        $len = strlen($text);
        for($len--; $len >= 0; $len--)
        {
            if($text[$len] !== "\0")
            {
                $len++;
                break;
            }
        }
        return substr($text, 0, $len);
    }
     
}
 
class AES_CBC_PKCS7Padding
{
    private $iv;
    private $key;
    private $blocksize;
     
    public function __construct($key, $iv)
    {
        $this->blocksize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
        $this->key = substr(md5($key), 0, $this->blocksize);
        $this->iv = substr(md5($iv), 0, $this->blocksize);
    }
     
    public function encrypt($text)
    {
        $pad = $this->blocksize - strlen($text)%$this->blocksize;
        $text = str_pad($text, strlen($text) + $pad, chr($pad));
        return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->key, $text, MCRYPT_MODE_CBC, $this->iv));
    }
     
    public function decrypt($text)
    {
        $text = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $this->key, base64_decode($text), MCRYPT_MODE_CBC, $this->iv);
        $pad = ord(substr($text, -1));
        if($pad < 1 || $pad > 32) {
            $pad = 0;
        }
        return substr($text, 0, strlen($text) - $pad);
    }
     
}