1

How can I compile raw C# code at runtime? So if I want to add security by removing important pieces of code and downloading it via a server, and then execute it from within my application?

JasonMArcher
  • 13,296
  • 21
  • 55
  • 51
Nahydrin
  • 12,719
  • 12
  • 58
  • 96

2 Answers2

0

I wrote this a while ago to simplify code execution, hope it helps:

CSharpCompiler

Large
  • 1
  • 1
0

Do you want to download the source code, or a compiled version?

You can download the MSIL and use Assembly.Load(byte[]) to load it for execution.

But none of these methods add security. If you send the code to the user's computer in ANY format, they can capture a copy. Never saving it to disk just adds a small speedbump.

See: The #1 law of software licensing.

Community
  • 1
  • 1
Ben Voigt
  • 269,602
  • 39
  • 394
  • 697
  • The downloaded source code is encrypted. It's an online-only program, so the inconveneice will always be there. – Nahydrin Jul 05 '11 at 05:21
  • @Dark: Doesn't really matter, since the decryption keys are on the user's computer. Just another speedbump, no real security. – Ben Voigt Jul 05 '11 at 05:29
  • @Dark: Anyway, why send source code, when you can encrypt and send the MSIL output from the C# compiler? – Ben Voigt Jul 05 '11 at 05:41
  • Because I don't know how to do that. – Nahydrin Jul 05 '11 at 05:57
  • 1
    @Dark: Just build a DLL project. Send that file, when you decrypt it on the receiving side, instead of writing it to disk, call `Assembly.Load(byte[])`. – Ben Voigt Jul 05 '11 at 06:25