1: <?php
2:
3: include_once "errorCode.php";
4:
5: 6: 7: 8: 9:
10: class PKCS7Encoder
11: {
12: public static $block_size = 32;
13:
14: 15: 16: 17: 18:
19: function encode($text)
20: {
21: $block_size = PKCS7Encoder::$block_size;
22: $text_length = strlen($text);
23:
24: $amount_to_pad = PKCS7Encoder::$block_size - ($text_length % PKCS7Encoder::$block_size);
25: if ($amount_to_pad == 0) {
26: $amount_to_pad = PKCS7Encoder::block_size;
27: }
28:
29: $pad_chr = chr($amount_to_pad);
30: $tmp = "";
31: for ($index = 0; $index < $amount_to_pad; $index++) {
32: $tmp .= $pad_chr;
33: }
34: return $text . $tmp;
35: }
36:
37: 38: 39: 40: 41:
42: function decode($text)
43: {
44:
45: $pad = ord(substr($text, -1));
46: if ($pad < 1 || $pad > 32) {
47: $pad = 0;
48: }
49: return substr($text, 0, (strlen($text) - $pad));
50: }
51:
52: }
53:
54: 55: 56: 57: 58:
59: class Prpcrypt
60: {
61: public $key;
62:
63: function Prpcrypt($k)
64: {
65: $this->key = base64_decode($k . "=");
66: }
67:
68: 69: 70: 71: 72:
73: public function encrypt($text, $appid)
74: {
75:
76: try {
77:
78: $random = $this->getRandomStr();
79: $text = $random . pack("N", strlen($text)) . $text . $appid;
80:
81: $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
82: $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
83: $iv = substr($this->key, 0, 16);
84:
85: $pkc_encoder = new PKCS7Encoder;
86: $text = $pkc_encoder->encode($text);
87: mcrypt_generic_init($module, $this->key, $iv);
88:
89: $encrypted = mcrypt_generic($module, $text);
90: mcrypt_generic_deinit($module);
91: mcrypt_module_close($module);
92:
93:
94:
95: return array(ErrorCode::$OK, base64_encode($encrypted));
96: } catch (Exception $e) {
97:
98: return array(ErrorCode::$EncryptAESError, null);
99: }
100: }
101:
102: 103: 104: 105: 106:
107: public function decrypt($encrypted, $appid)
108: {
109:
110: try {
111:
112: $ciphertext_dec = base64_decode($encrypted);
113: $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
114: $iv = substr($this->key, 0, 16);
115: mcrypt_generic_init($module, $this->key, $iv);
116:
117:
118: $decrypted = mdecrypt_generic($module, $ciphertext_dec);
119: mcrypt_generic_deinit($module);
120: mcrypt_module_close($module);
121: } catch (Exception $e) {
122: return array(ErrorCode::$DecryptAESError, null);
123: }
124:
125:
126: try {
127:
128: $pkc_encoder = new PKCS7Encoder;
129: $result = $pkc_encoder->decode($decrypted);
130:
131: if (strlen($result) < 16)
132: return "";
133: $content = substr($result, 16, strlen($result));
134: $len_list = unpack("N", substr($content, 0, 4));
135: $xml_len = $len_list[1];
136: $xml_content = substr($content, 4, $xml_len);
137: $from_appid = substr($content, $xml_len + 4);
138: } catch (Exception $e) {
139:
140: return array(ErrorCode::$IllegalBuffer, null);
141: }
142: if ($from_appid != $appid)
143: return array(ErrorCode::$ValidateAppidError, null);
144: return array(0, $xml_content);
145:
146: }
147:
148:
149: 150: 151: 152:
153: function getRandomStr()
154: {
155:
156: $str = "";
157: $str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
158: $max = strlen($str_pol) - 1;
159: for ($i = 0; $i < 16; $i++) {
160: $str .= $str_pol[mt_rand(0, $max)];
161: }
162: return $str;
163: }
164:
165: }
166:
167: ?>