From b88beea0672f122eb056a79299c17cef965b254b Mon Sep 17 00:00:00 2001 From: fanbo <3412853751@qq.com> Date: Mon, 30 Dec 2024 08:16:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9prpcrypt.lib.php=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fanbo <3412853751@qq.com> --- core/aes/prpcrypt.lib.php | 80 ++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 38 deletions(-) diff --git a/core/aes/prpcrypt.lib.php b/core/aes/prpcrypt.lib.php index f7322fd..9fe825f 100644 --- a/core/aes/prpcrypt.lib.php +++ b/core/aes/prpcrypt.lib.php @@ -1,73 +1,77 @@ key = base64_decode($k . "="); + $this->key = base64_decode($k . "="); // 对传入的密钥进行base64解码,并追加'='字符 } + // 加密函数,用于加密文本消息 public function encrypt($text, $appid) { try { - $random = $this->getRandomStr(); - $text = $random . pack("N", strlen($text)) . $text . $appid; - $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); - $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); - $iv = substr($this->key, 0, 16); - $pkc_encoder = new PKCS7Encoder; - $text = $pkc_encoder->encode($text); - mcrypt_generic_init($module, $this->key, $iv); - $encrypted = mcrypt_generic($module, $text); - mcrypt_generic_deinit($module); - mcrypt_module_close($module); - return array(ErrorCode::$OK, base64_encode($encrypted)); + $random = $this->getRandomStr(); // 生成16位随机字符串 + $text = $random . pack("N", strlen($text)) . $text . $appid; // 将随机字符串、文本长度、文本内容和AppID拼接 + $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); // 获取加密块大小 + $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); // 打开加密模块 + $iv = substr($this->key, 0, 16); // 从密钥中截取前16位作为初始化向量 + $pkc_encoder = new PKCS7Encoder; // 实例化PKCS7Encoder类,用于数据填充 + $text = $pkc_encoder->encode($text); // 对文本进行PKCS7编码 + mcrypt_generic_init($module, $this->key, $iv); // 初始化加密模块 + $encrypted = mcrypt_generic($module, $text); // 进行加密操作 + mcrypt_generic_deinit($module); // 解密模块 + mcrypt_module_close($module); // 关闭加密模块 + return array(ErrorCode::$OK, base64_encode($encrypted)); // 返回加密结果和状态码 } catch (\Exception $e) { - return array(ErrorCode::$EncryptAESError, null); + return array(ErrorCode::$EncryptAESError, null); // 捕获异常,返回加密错误状态码 } } + // 解密函数,用于解密加密的消息 public function decrypt($encrypted, $appid) { try { - $ciphertext_dec = base64_decode($encrypted); - $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); - $iv = substr($this->key, 0, 16); - mcrypt_generic_init($module, $this->key, $iv); - $decrypted = mdecrypt_generic($module, $ciphertext_dec); - mcrypt_generic_deinit($module); - mcrypt_module_close($module); + $ciphertext_dec = base64_decode($encrypted); // 对加密文本进行base64解码 + $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); // 打开解密模块 + $iv = substr($this->key, 0, 16); // 从密钥中截取前16位作为初始化向量 + mcrypt_generic_init($module, $this->key, $iv); // 初始化解密模块 + $decrypted = mdecrypt_generic($module, $ciphertext_dec); // 进行解密操作 + mcrypt_generic_deinit($module); // 解密模块 + mcrypt_module_close($module); // 关闭解密模块 } catch (\Exception $e) { - return array(ErrorCode::$DecryptAESError, null); + return array(ErrorCode::$DecryptAESError, null); // 捕获异常,返回解密错误状态码 } try { - $pkc_encoder = new PKCS7Encoder; - $result = $pkc_encoder->decode($decrypted); - if (strlen($result) < 16) + $pkc_encoder = new PKCS7Encoder; // 实例化PKCS7Encoder类 + $result = $pkc_encoder->decode($decrypted); // 对解密后的数据进行PKCS7解码 + if (strlen($result) < 16) // 检查解码后的数据长度 return ""; - $content = substr($result, 16, strlen($result)); - $len_list = unpack("N", substr($content, 0, 4)); - $xml_len = $len_list[1]; - $xml_content = substr($content, 4, $xml_len); - $from_appid = substr($content, $xml_len + 4); + $content = substr($result, 16, strlen($result)); // 截取除去随机字符串和长度信息后的内容 + $len_list = unpack("N", substr($content, 0, 4)); // 解析文本长度信息 + $xml_len = $len_list[1]; // 获取文本长度 + $xml_content = substr($content, 4, $xml_len); // 截取XML内容 + $from_appid = substr($content, $xml_len + 4); // 获取AppID } catch (\Exception $e) { - return array(ErrorCode::$IllegalBuffer, null); + return array(ErrorCode::$IllegalBuffer, null); // 捕获异常,返回非法缓冲区状态码 } - if ($from_appid != $appid) + if ($from_appid != $appid) // 检查AppID是否匹配 return array(ErrorCode::$ValidateAppidError, null); - return array(0, $xml_content); + return array(0, $xml_content); // 返回解密后的XML内容和状态码 } + // 生成随机字符串函数 function getRandomStr() { $str = ""; - $str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"; + $str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"; // 定义随机字符串的字符集 $max = strlen($str_pol) - 1; for ($i = 0; $i < 16; $i++) { - $str .= $str_pol[mt_rand(0, $max)]; + $str .= $str_pol[mt_rand(0, $max)]; // 生成16位随机字符串 } return $str; }