解密后检查开头字符串如何是json开头,就返回全部内部,如果不是再进行截取操作。
public function decrypt($encrypted){
try {
// 使用BASE64对需要解密的字符串进行解码
$ciphertextDec = base64_decode($encrypted);
$iv = substr($this->aesKey, 0, 16);
// 解密
$decrypted = openssl_decrypt($ciphertextDec, 'aes-256-cbc', $this->aesKey, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $iv);
} catch (Exception $e) {
throw new Exception('AesEncryptUtil AES解密串非法,小于16位;');
}
try {
// 去除补位字符
$result = $this->decode($decrypted);
// 去除16位随机字符串,网络字节序和clientId
if (strlen($result) < 16) {
throw new Exception('AesEncryptUtil AES解密串非法,小于16位;');
}
if($result[0]=='{'){ # 快手是json直接返回
return $result;
}
$content = substr($result, 16, strlen($result));
$lenList = unpack("N", substr($content, 0, 4));
$xmlLen = $lenList[1];
$xmlContent = substr($content, 4, $xmlLen);
// $fromClientId = substr($content, $xmlLen + 4);
} catch (Exception $e) {
throw new Exception('AesEncryptUtil AES解密串非法,小于16位;');
}
return $xmlContent;
}
public function getMsgSignature($token, $bodyStr)
{
try {
return sha1($bodyStr.$token);
} catch (Exception $e) {
throw new Exception("生成安全签名失败");
}
}