本文介绍了在C#应用程序中使用加密程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图保护我在WPF应用程序中使用的dll免受简单复制.我的解决方案是对这些dll的代码部分进行加密,并在将其加载到我的应用程序时解密.

I am trying to protect dlls I'm using in my WPF application from a simple copy. My solution is to encrypt the code section of these dlls and decrypt when it loads into my application.

有一种使用组装的可行方法:

There is a working way to do that using Assembly:

       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方法.这是一个可怕的观点,不是吗?

But I will need to use the CreateInstance and InvokeMember methods every time I want to work with an object from this dll. It is a terrible perspective, isn't it?

是否还有另一种方式来加载这些dll,而不是CLR加载器?还是只是简化前一种方式?

Is there another way to load these dlls instead of the CLR loader? Or just make the previous way easier?

推荐答案

是,实施 AppDomain.AssemblyResolve 事件.

当程序集解析失败时,将触发此事件(您需要将程序集存储在无法找到它们的地方),然后在加载并解密后可以说哦,就在这里".

This event is fired when assembly resolution fails (you would need to store your assemblies somewhere assembly resolution won't find them), and then you can say "oh, here it is" after having loaded and decrypted it.

这篇关于在C#应用程序中使用加密程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 02:24
查看更多