【问题标题】:Use encrypted assembly in C# application在 C# 应用程序中使用加密程序集
【发布时间】:2015-08-19 08:18:47
【问题描述】:

我正在尝试保护我在 WPF 应用程序中使用的 dll 免受简单副本的影响。我的解决方案是加密这些 dll 的代码部分并在加载到我的应用程序时解密。

有一种使用组装的工作方法:

       using (FileStream fs = new FileStream(@"Mydll.dll", FileMode.Open, FileAccess.Read))
        {
            byte[] file = new byte[fs.Length];
            fs.Read(file, 0, (int) fs.Length);
            assemblyCashCode = Assembly.Load(file);
            Type[] types = assemblyCashCode.GetExportedTypes();

            Type t = MainWindow.assemblyCashCode.GetType("MyClass");
            MethodInfo[] mi = t.GetMethods();
            TypeInfo ti = t.GetTypeInfo();
            object cc = assemblyCashCode.CreateInstance("MyClass");
            int i = (int) t.InvokeMember("M3", BindingFlags.InvokeMethod, null, cc, null);
        }

但每次我想使用这个 dll 中的对象时,我都需要使用 CreateInstance 和 InvokeMember 方法。这是一个可怕的观点,不是吗?

是否有另一种方法来加载这些 dll 而不是 CLR 加载器?或者只是让以前的方法更容易?

【问题讨论】:

  • AppDomain.AssemblyResolve 是做这些事情的地方,但请注意,这可能不会起到太大的保护作用。可以在 WinDBG 中运行您的应用程序,然后执行SaveAllModules。我不是 100% 确定,但几乎可以肯定,这将保存您在内存中的未加密版本。

标签: c# .net encryption dll clr


【解决方案1】:

是的,实现AppDomain.AssemblyResolve 事件。

当程序集解析失败时会触发此事件(您需要将程序集存储在程序集解析找不到它们的地方),然后您可以在加载和解密后说“哦,这里是”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-28
    • 2015-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多