standard aes 138 ecb crypt
Showing
1 changed file
with
58 additions
and
53 deletions
1 | <?php | 1 | <?php |
2 | /** | 2 | /** |
3 | - * 加密解密 | 3 | + * AES, 128 ECB模式加密数据 |
4 | */ | 4 | */ |
5 | 5 | ||
6 | namespace WebPlugin; | 6 | namespace WebPlugin; |
7 | 7 | ||
8 | class Encryption { | 8 | class Encryption { |
9 | 9 | ||
10 | - // 密钥 | ||
11 | - private static $key = '123'; | 10 | + //密钥 |
11 | + private static $_secrect_key = 'yoho9646abcdefgh'; | ||
12 | 12 | ||
13 | - // 加密 | ||
14 | - public static function encrypt ($data) | ||
15 | - { | ||
16 | - $char = ''; | ||
17 | - $str = ''; | ||
18 | - $key = md5(self::$key); | ||
19 | - $x = 0; | ||
20 | - $len = strlen($data); | ||
21 | - $l = strlen($key); | ||
22 | - for ($i = 0; $i < $len; $i++) { | ||
23 | - if ($x == $l) | ||
24 | - { | ||
25 | - $x = 0; | ||
26 | - } | ||
27 | - $char .= $key{$x}; | ||
28 | - $x++; | ||
29 | - } | ||
30 | - | ||
31 | - for ($i = 0; $i < $len; $i++) { | ||
32 | - $str .= chr(ord($data{$i}) + (ord($char{$i})) % 256); | ||
33 | - } | ||
34 | - return base64_encode($str); | 13 | + /** |
14 | + * 加密方法 | ||
15 | + * @param string $str | ||
16 | + * @return string | ||
17 | + */ | ||
18 | + public static function encrypt($str){ | ||
19 | + //AES, 128 ECB模式加密数据 | ||
20 | + $screct_key = self::$_secrect_key; | ||
21 | + $str = trim($str); | ||
22 | + $str = self::addPKCS7Padding($str); | ||
23 | + $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_ECB),MCRYPT_RAND); | ||
24 | + $encrypt_str = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $screct_key, $str, MCRYPT_MODE_ECB, $iv); | ||
25 | + return base64_encode($encrypt_str); | ||
35 | } | 26 | } |
36 | 27 | ||
37 | - //解密 | ||
38 | - public static function decrypt($data) | ||
39 | - { | ||
40 | - $char = ''; | ||
41 | - $str = ''; | 28 | + /** |
29 | + * 解密方法 | ||
30 | + * @param string $str | ||
31 | + * @return string | ||
32 | + */ | ||
33 | + public static function decrypt($str){ | ||
34 | + //AES, 128 ECB模式加密数据 | ||
35 | + $screct_key = self::$_secrect_key; | ||
36 | + $str = base64_decode($str); | ||
37 | + $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_ECB),MCRYPT_RAND); | ||
38 | + $encrypt_str = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $screct_key, $str, MCRYPT_MODE_ECB, $iv); | ||
39 | + $encrypt_str = trim($encrypt_str); | ||
40 | + $encrypt_str = self::stripPKSC7Padding($encrypt_str); | ||
41 | + return $encrypt_str; | ||
42 | 42 | ||
43 | - $key = md5(self::$key); | ||
44 | - $x = 0; | ||
45 | - $data = base64_decode($data); | ||
46 | - $len = strlen($data); | ||
47 | - $l = strlen($key); | ||
48 | - for ($i = 0; $i < $len; $i++) { | ||
49 | - if ($x == $l) | ||
50 | - { | ||
51 | - $x = +0; | ||
52 | - } | ||
53 | - $char .= substr($key, $x, 1); | ||
54 | - $x++; | ||
55 | - } | ||
56 | - for ($i = 0; $i < $len; $i++) { | ||
57 | - if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1))) | ||
58 | - { | ||
59 | - $str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1))); | ||
60 | - } | ||
61 | - else | ||
62 | - { | ||
63 | - $str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1))); | ||
64 | - } | 43 | + } |
44 | + | ||
45 | + /** | ||
46 | + * 填充算法 | ||
47 | + * @param string $source | ||
48 | + * @return string | ||
49 | + */ | ||
50 | + private static function addPKCS7Padding($source){ | ||
51 | + $source = trim($source); | ||
52 | + $block = mcrypt_get_block_size('rijndael-128', 'ecb'); | ||
53 | + $pad = $block - (strlen($source) % $block); | ||
54 | + if ($pad <= $block) { | ||
55 | + $char = chr($pad); | ||
56 | + $source .= str_repeat($char, $pad); | ||
65 | } | 57 | } |
66 | - return $str; | 58 | + return $source; |
59 | + } | ||
60 | + /** | ||
61 | + * 移去填充算法 | ||
62 | + * @param string $source | ||
63 | + * @return string | ||
64 | + */ | ||
65 | + private static function stripPKSC7Padding($source){ | ||
66 | + $source = trim($source); | ||
67 | + $char = substr($source, -1); | ||
68 | + $num = ord($char); | ||
69 | + if($num==62)return $source; | ||
70 | + $source = substr($source,0,-$num); | ||
71 | + return $source; | ||
67 | } | 72 | } |
68 | } | 73 | } |
-
Please register or login to post a comment