在开发微信开放平台-第三方平台过程中,遇到了component_verify_ticket无法解密问题。官方文档晦涩难懂,于是自己重新写了一个解密component_verify_ticket的方法。
使用前请先安装mcrypt扩展
<?php
/**
* huyang61@qq.com
* Notes:
* DateTime: 2023/7/12 10:23
*/
namespace App\Http\Services;
/**
* Notes:
* DateTime: 2023/7/12 10:23
*/
class Jie
{
public static function decode($xml, $encodingaeskey): array
{
$xml = (array)simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$str = $xml['Encrypt'];
$data = self::aes_decode($str, $encodingaeskey);
$xml = (array)simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
return $xml;
}
public static function aes_decode($message, $encodingaeskey)
{
$key = base64_decode($encodingaeskey . '=');
$ciphertext_dec = base64_decode($message);
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
$iv = substr($key, 0, 16);
mcrypt_generic_init($module, $key, $iv);
$decrypted = mdecrypt_generic($module, $ciphertext_dec);
mcrypt_generic_deinit($module);
mcrypt_module_close($module);
$block_size = 32;
$pad = ord(substr($decrypted, -1));
if ($pad < 1 || $pad > 32) {
$pad = 0;
}
$result = substr($decrypted, 0, (strlen($decrypted) - $pad));
if (strlen($result) < 16) {
return '';
}
$content = substr($result, 16, strlen($result));
$len_list = unpack("N", substr($content, 0, 4));
$contentlen = $len_list[1];
$content = substr($content, 4, $contentlen);
return $content;
}
}