SG11在线加密系统使用教程(PHP 开发指南)
SG11 是一款广泛用于 PHP 源码保护的商业混淆与加密工具,其核心为 Zend 引擎级扩展(sg11.so 或 php_sg11.dll),支持 PHP 5.3–8.2。需注意:SG11 本身不提供“在线加密服务”——所谓“SG11在线加密系统”实为第三方封装的 Web 接口(如基于 sg11-cli 的 REST API),本文以典型集成方式为例,介绍如何在 PHP 项目中安全调用 SG11 加密服务。
一、前提准备
- 已购买并激活 SG11 商业授权(获取 License Key 与 CLI 工具)
- 服务器安装 SG11 扩展(参考官方文档启用
extension=sg11.so) - 部署好可信的 SG11 加密 API 服务(如:https://api.yourdomain.com/sg11/encrypt)
二、PHP 调用示例(cURL 封装)
<?php
/**
* SG11 在线加密客户端(v1.0)
* 注意:生产环境请使用 HTTPS + Token 鉴权
*/
class SG11Encryptor {
private $apiUrl = 'https://api.yourdomain.com/sg11/encrypt';
private $apiKey = 'your_api_key_here'; // 替换为实际 API 密钥
public function encryptFile(string $sourcePath, string $targetPath): bool {
if (!file_exists($sourcePath)) {
throw new InvalidArgumentException("源文件不存在: {$sourcePath}");
}
$content = file_get_contents($sourcePath);
$payload = [
'code' => base64_encode($content),
'license' => $this->apiKey,
'options' => ['obfuscate' => true, 'strip_comments' => true]
];
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $this->apiUrl,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode !== 200 || !$response) {
throw new RuntimeException("加密请求失败 (HTTP {$httpCode})");
}
$result = json_decode($response, true);
if (!isset($result['success']) || !$result['success']) {
throw new RuntimeException("加密失败: " . ($result['error'] ?? '未知错误'));
}
return (bool)file_put_contents($targetPath, base64_decode($result['encrypted_code']));
}
}
// 使用示例
try {
$encryptor = new SG11Encryptor();
$encryptor->encryptFile('app/index.php', 'dist/index.php.sg11');
echo "✅ 加密成功!已保存至 dist/index.php.sg11\n";
} catch (Exception $e) {
echo "❌ 加密失败:{$e->getMessage()}\n";
}
?>
💡 提示:真实生产环境中,请务必:
- 对 API 请求启用双向 TLS 认证
- 限制 IP 白名单与请求频率
- 敏感代码绝不通过网络传输——推荐本地 CLI 加密(
sg11 --encode index.php)
三、验证加密结果
加密后文件需在启用 SG11 扩展的环境中运行:
<?php
// dist/index.php.sg11(无需修改,直接包含)
require_once 'dist/index.php.sg11'; // ✅ 正常执行
?>
⚠️ 重要警告:SG11 加密不可逆,且不兼容所有 PHP 特性(如动态函数名、eval 内容、部分反射操作)。务必在加密前完成完整测试,并保留原始源码备份!
掌握 SG11 的正确集成方式,可有效提升 PHP 应用分发安全性。但请始终牢记:代码保护 ≠ 安全保障,真正的安全源于架构设计、权限控制与持续运维。
```