# C# 离线防破解 + 序列号授权方案

## 文件清单

| 文件 | 说明 |
|------|------|
| `LicenseLib/LicenseHelper.cs` | 核心授权库（机器码、序列号校验、防调试） |
| `LicenseGen/Program.cs` | 序列号生成工具（你在自己电脑上生成注册码用） |
| `如何使用.md` | 集成步骤 |

---

## 快速集成（在你的项目里）

### 1. 把 LicenseHelper.cs 加入你的项目

复制 `LicenseLib/LicenseHelper.cs` 到你的解决方案中。

### 2. 初始化（程序启动时）

```csharp
// 启动时检查调试器
LicenseHelper.CheckDebugger();

// 获取本机机器码（用户发给你时用）
string machineCode = LicenseHelper.GetLocalMachineCode();
```

### 3. 序列号校验（用户输入注册码时）

```csharp
string userInput = textBox_RegCode.Text.Trim();
string machineCode = LicenseHelper.GetLocalMachineCode();

if (LicenseHelper.CheckRegister(userInput, machineCode))
{
    // ✅ 验证通过，保存标志
    LicenseHelper.SaveLicensedFlag();
    // 解锁功能
}
else
{
    // ❌ 序列号无效
    MessageBox.Show("注册码无效，请检查后重试");
}
```

### 4. 判断是否已授权

```csharp
if (LicenseHelper.IsLicensed())
{
    // 已授权，加载功能
}
else
{
    // 未授权，显示注册窗口
}
```

---

## 序列号生成（在你电脑上）

用 `LicenseGen` 项目编译出的工具，用户给你机器码，你输入后生成注册码：

```
用户机器码: 1A2B3C4D5E6F7G8H...
生成的注册码: 4F6A-8B2C-1D9E-3F7A
```

---

## 加固打包流程（发布前）

### 第一步：源码加固
- ✅ 已包含：私钥异或加密存储、防调试、防多开
- 建议：关键业务代码也加一些花逻辑、冗余判断

### 第二步：ConfuserEx 混淆（免费）
1. 下载：https://github.com/yck1509/ConfuserEx
2. 使用 `ConfuserEx.crproj` 配置
3. 保护全部勾选：Rename / Control Flow / Anti Debug / Anti Tamper / Constants / Reference Proxy
4. 强度选 Strong

### 第三步：VMProtect 虚拟机壳（可选，最强）
1. 购买 VMProtect 或试用版
2. 只虚拟化 `CheckRegister` 等核心校验函数
3. 开启：Anti-Debug / Anti-Dump / Mutation (代码变异)
4. 编译输出 xxx.vmp.exe

### 第四步：可选加签名
用 Authenticode 证书给 exe 签名，防止程序集被替换。
