1

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?

academica
  • 325
  • 3
  • 13
  • AppDomain.AssemblyResolve is the place to do such things, but note that this probably won't be much of a protection anyway. One can run your application inside WinDBG and then do a [SaveAllModules](http://stackoverflow.com/a/1644001/21567). I'm not a 100% sure, but pretty much so, that this will save the unencrypted version that you have in memory. – Christian.K Aug 21 '15 at 04:32

1 Answers1

2

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.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825