第10章 资源保护

第10章:资源保护

10.1 资源保护概述

应用程序的嵌入资源(图片、配置文件、数据文件等)也可能包含敏感信息,需要适当保护。

10.1.1 需要保护的资源类型

1. 配置文件
   - *.config
   - *.xml
   - *.json

2. 数据文件
   - *.dat
   - *.bin
   - 数据库文件

3. 许可证文件
   - *.license
   - *.key

4. 图片资源
   - 水印图片
   - Logo
   - 品牌资源

5. 文档资源
   - 帮助文档
   - 说明文件

10.2 配置资源保护

基本配置:

Settings → Protection → Resource Protection

☑ Enable Resource Protection

Protection Method:
○ Compress Only
○ Encrypt Only
● Encrypt and Compress

Compression Level:
○ Fast
● Optimal
○ Maximum

10.3 选择性保护

使用模式匹配:

<ResourceProtection enabled="true">
  <Include>
    <!-- 保护所有配置文件 -->
    <Pattern>*.config</Pattern>
    <Pattern>*.xml</Pattern>
    
    <!-- 保护特定资源 -->
    <Resource name="license.dat" />
    <Resource name="watermark.png" />
    
    <!-- 保护整个资源文件夹 -->
    <Folder path="Configs/" />
  </Include>
  
  <Exclude>
    <!-- 排除需要外部访问的资源 -->
    <Pattern>*.ico</Pattern>
    <Resource name="app.manifest" />
  </Exclude>
</ResourceProtection>

10.4 访问受保护资源

代码完全透明:

// 原始代码
var assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream("MyApp.config.xml"))
using (StreamReader reader = new StreamReader(stream))
{
    string content = reader.ReadToEnd();
    // 处理配置
}

// 保护后代码无需修改
// .NET Reactor 自动处理解密

10.5 嵌入和合并

10.5.1 嵌入依赖DLL

Settings → Resource Protection → Embed Dependencies

☑ Embed Dependent Assemblies

Dependencies to Embed:
☑ Newtonsoft.Json.dll
☑ System.Data.SQLite.dll
☑ Custom.Library.dll

Options:
☑ Compress Embedded DLLs
☑ Encrypt Embedded DLLs
☑ Load on Demand (按需加载)

10.5.2 自动解析依赖

// .NET Reactor 自动注入
static Constructor()
{
    AppDomain.CurrentDomain.AssemblyResolve += 
        (sender, args) =>
    {
        // 自动从资源加载嵌入的 DLL
        return LoadEmbeddedAssembly(args.Name);
    };
}

10.6 卫星程序集

10.6.1 保护本地化资源

Satellite Assemblies:
☑ Protect Satellite Assemblies

Cultures:
☑ zh-CN
☑ ja-JP
☑ de-DE
☑ fr-FR

Options:
☑ Embed into Main Assembly
○ Keep Separate (加密)

10.6.2 动态加载

// 访问本地化资源
ResourceManager rm = new ResourceManager(
    "MyApp.Resources.Strings", 
    Assembly.GetExecutingAssembly());

string welcome = rm.GetString("Welcome", 
    CultureInfo.CurrentUICulture);

10.7 自定义资源加载器

10.7.1 实现自定义加载

public class CustomResourceLoader
{
    public static Stream LoadResource(string name)
    {
        // 从受保护的资源加载
        var assembly = Assembly.GetExecutingAssembly();
        Stream stream = assembly.GetManifestResourceStream(name);
        
        // 额外的处理(如果需要)
        return ProcessStream(stream);
    }
    
    public static byte[] LoadBinaryResource(string name)
    {
        using (Stream stream = LoadResource(name))
        using (MemoryStream ms = new MemoryStream())
        {
            stream.CopyTo(ms);
            return ms.ToArray();
        }
    }
}

10.7.2 缓存机制

public class ResourceCache
{
    private static Dictionary<string, byte[]> _cache = 
        new Dictionary<string, byte[]>();
    
    public static byte[] GetResource(string name)
    {
        if (!_cache.ContainsKey(name))
        {
            _cache[name] = CustomResourceLoader.LoadBinaryResource(name);
        }
        return _cache[name];
    }
}

10.8 性能优化

10.8.1 延迟加载

Resource Loading Strategy:
● Lazy Loading (按需加载)
○ Preload All (预加载全部)
○ Preload Critical (预加载关键资源)

Cache Size:
Limit: [50] MB
Policy: LRU

10.8.2 压缩级别选择

┌──────────────┬──────────┬──────────┬──────────┐
│  压缩级别    │ 压缩率   │  速度    │  内存    │
├──────────────┼──────────┼──────────┼──────────┤
│ Fast         │   60%    │  ★★★   │  ★★★   │
│ Optimal      │   75%    │  ★★☆   │  ★★☆   │
│ Maximum      │   85%    │  ★☆☆   │  ★☆☆   │
└──────────────┴──────────┴──────────┴──────────┘

10.9 实战案例

10.9.1 保护配置文件

<!-- 项目配置 -->
<ResourceProtection enabled="true">
  <Include>
    <Resource name="appsettings.json" encrypt="true" compress="true" />
    <Resource name="database.config" encrypt="true" compress="true" />
  </Include>
</ResourceProtection>

10.9.2 保护许可证密钥

public class LicenseKeyManager
{
    private static byte[] LoadEncryptedKey()
    {
        // 从受保护的资源加载
        return CustomResourceLoader.LoadBinaryResource("license.key");
    }
    
    public static string GetLicenseKey()
    {
        byte[] encrypted = LoadEncryptedKey();
        byte[] decrypted = Decrypt(encrypted);
        return Encoding.UTF8.GetString(decrypted);
    }
}

10.10 本章小结

本章介绍了资源保护技术,包括:

  • 各种资源类型的保护
  • 选择性保护策略
  • 嵌入和合并依赖
  • 性能优化方法

通过合理保护资源,可以进一步增强应用程序的整体安全性。

posted @ 2025-12-20 13:37  我才是银古  阅读(2)  评论(0)    收藏  举报