微信公众号PHP-SDK ( php8.0 版)
2022/2/25 1:51:22
本文主要是介绍微信公众号PHP-SDK ( php8.0 版),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
PHP7 版本后,官方舍弃 mcrypt_module 系列的方法了。
比如 mcrypt_module_open, mcrypt_module_open , mcrypt_generic_deinit ,mcrypt_generic 等。
改用 openssl_encrypt 和 openssl_decrypt 。
所以,在公众号的SKD中,修改 pkcs7Encoder.php文件中 pkcs7Encoder 类即可。
class Prpcrypt { public $key; /**向量 */ public static $IV = "12asd67890123412";//16位 public static $AES = 'AES-256-CBC'; function __construct($k) { $this->key = base64_decode($k . "="); } public function encrypt($text, $appid) { try { //获得16位随机字符串,填充到明文之前 $random = $this->getRandomStr(); $text = $random . pack("N", strlen($text)) . $text . $appid; $pkc_encoder = new PKCS7Encoder; $text = $pkc_encoder->encode($text); $encrypted = openssl_encrypt($text,self::$AES,$this->key,OPENSSL_RAW_DATA,self::$IV); return array(ErrorCode::$OK, base64_encode($encrypted)); } catch (Exception $e) { //print $e; return array(ErrorCode::$EncryptAESError, null); } } public function decrypt($encrypted, $appid) { $decrypted = openssl_decrypt(base64_decode($encrypted),self::$AES,$this->key,OPENSSL_RAW_DATA,self::$IV); try { //去除补位字符 $pkc_encoder = new PKCS7Encoder; $result = $pkc_encoder->decode($decrypted); //去除16位随机字符串,网络字节序和AppId 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); } catch (Exception $e) { //print $e; return array(ErrorCode::$IllegalBuffer, null); } if ($from_appid != $appid) return array(ErrorCode::$ValidateAppidError, null); return array(0, $xml_content); } function getRandomStr() { $str = ""; $str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"; $max = strlen($str_pol) - 1; for ($i = 0; $i < 16; $i++) { $str .= $str_pol[mt_rand(0, $max)]; } return $str; } }
这篇关于微信公众号PHP-SDK ( php8.0 版)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-03-29env: php: no such file or directory
- 2024-03-01php foreach break
- 2024-02-26Exception参数 php-icode9专业技术文章分享
- 2023-12-30PHP文件批量上传-icode9专业技术文章分享
- 2023-12-30thinkphp6 withJoin-icode9专业技术文章分享
- 2023-12-27MagicArray:像php一样,让Go业务代码不再卷!
- 2023-11-18centos7编译安装PHP教程。
- 2023-11-18centos7编译安装phpMyAdmin教程。
- 2023-10-13唱衰这么多年,PHP 仍然还是你大爷!
- 2023-07-25PHP8,性能更好,语法更好,类型安全更完善