You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
git-test/core/aes/wxbizmsgcrypt.lib.php

50 lines
1.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace LaneWeChat\Core\Aes; // 定义命名空间为 LaneWeChat\Core\Aes用于组织与微信消息加密相关的代码
/**
* 微信消息加密类
*
* 该类用于对微信公众号的消息进行加密和解密操作,确保消息的安全性。
*/
class WXBizMsgCrypt {
private $token; // 微信Token用于验证消息来源
private $encodingAesKey; // 微信EncodingAESKey用于消息加密
private $appId; // 微信AppId用于标识公众号
/**
* 类构造函数
*
* 用于初始化Token、EncodingAESKey和AppId。
*
* @param string $token 微信Token
* @param string $encodingAesKey 微信EncodingAESKey
* @param string $appId 微信AppId
*/
public function __construct($token, $encodingAesKey, $appId) {
$this->token = $token; // 初始化微信Token
$this->encodingAesKey = $encodingAesKey; // 初始化微信EncodingAESKey
$this->appId = $appId; // 初始化微信AppId
}
/**
* 加密消息方法
*
* 对回复消息进行加密,确保消息在传输过程中的安全性。
*
* @param string $replyMsg 要加密的回复消息
* @param int $timeStamp 时间戳
* @param string $nonce 随机数
* @param string $encryptMsg 加密后的消息
* @return int 返回加密结果的状态码
*/
public function encryptMsg($replyMsg, $timeStamp, $nonce, &$encryptMsg) {
$pc = new Prpcrypt($this->encodingAesKey); // 实例化Prpcrypt类用于消息加密
$array = $pc->encrypt($replyMsg, $this->appId); // 调用encrypt方法进行消息加密
$ret = $array[0]; // 获取加密结果的状态码
if ($ret != 0) {
return $ret; // 如果加密失败,返回错误码
}
// 省略了后续代码,但通常这里会将加密后的消息、时间戳、随机数等信息
// 格式化为XML格式并赋值给$encryptMsg变量
}
}