|
|
|
|
<?php
|
|
|
|
|
namespace LaneWeChat\Core\Aes; // 定义命名空间,用于组织代码
|
|
|
|
|
|
|
|
|
|
class Prpcrypt // 定义Prpcrypt类
|
|
|
|
|
{
|
|
|
|
|
public $key; // 类成员变量,用于存储加密密钥
|
|
|
|
|
|
|
|
|
|
// 类构造函数,用于初始化密钥
|
|
|
|
|
function Prpcrypt($k)
|
|
|
|
|
{
|
|
|
|
|
$this->key = base64_decode($k . "="); // 对传入的密钥进行base64解码,并追加'='字符
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 加密函数,用于加密文本消息
|
|
|
|
|
public function encrypt($text, $appid)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$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); // 捕获异常,返回加密错误状态码
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 解密函数,用于解密加密的消息
|
|
|
|
|
public function decrypt($encrypted, $appid)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$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); // 捕获异常,返回解密错误状态码
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
$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); // 截取XML内容
|
|
|
|
|
$from_appid = substr($content, $xml_len + 4); // 获取AppID
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return array(ErrorCode::$IllegalBuffer, null); // 捕获异常,返回非法缓冲区状态码
|
|
|
|
|
}
|
|
|
|
|
if ($from_appid != $appid) // 检查AppID是否匹配
|
|
|
|
|
return array(ErrorCode::$ValidateAppidError, null);
|
|
|
|
|
return array(0, $xml_content); // 返回解密后的XML内容和状态码
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 生成随机字符串函数
|
|
|
|
|
function getRandomStr()
|
|
|
|
|
{
|
|
|
|
|
$str = "";
|
|
|
|
|
$str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"; // 定义随机字符串的字符集
|
|
|
|
|
$max = strlen($str_pol) - 1;
|
|
|
|
|
for ($i = 0; $i < 16; $i++) {
|
|
|
|
|
$str .= $str_pol[mt_rand(0, $max)]; // 生成16位随机字符串
|
|
|
|
|
}
|
|
|
|
|
return $str;
|
|
|
|
|
}
|
|
|
|
|
}
|