setKey($key); $cipher->disablePadding(); $result = $cipher->decrypt($data); return self::removePKCS5Padding($result); } // 都不可用,返回false return false; } /** * 移除PKCS5填充 * * @param string $text 填充的文本 * @return string|false 移除填充后的文本 */ private static function removePKCS5Padding($text) { if (empty($text) || !is_string($text)) { return false; } $textLength = strlen($text); if ($textLength === 0) { return false; } $pad = ord($text[$textLength - 1]); // 验证填充值的合理性 if ($pad > $textLength || $pad > 8 || $pad < 1) { return false; } // 验证所有填充字节是否一致 for ($i = 0; $i < $pad; $i++) { if (ord($text[$textLength - 1 - $i]) !== $pad) { return false; } } return substr($text, 0, -1 * $pad); } }