Use encrypted assembly in C# application

旧街凉风 提交于 2021-01-28 18:37:58

问题


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);
        }

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?

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


回答1:


Yes, implement the AppDomain.AssemblyResolve event.

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.



来源:https://stackoverflow.com/questions/32090099/use-encrypted-assembly-in-c-sharp-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!