Dynamically create a compiled .NET exe from source code?

泄露秘密 提交于 2019-12-17 20:56:11

问题


I need to inject a custom, unique, static string into my .NET based EXE. For all intents and purposes, assume that this is my Private key of a PKI.

I have a few ideas on how I'll approach protecting the Private key within the C# source code, my question is how do I take my .CS files and create an executable based off it?


回答1:


Take a look at CSharpCodeProvider, which can essentially be used take a string containing source code and compile it to an assembly (EXE or DLL), or if you require, an in memory assembly.

You can also use the codeDOM for the code generation side of things. Take a look at 'Generating Source Code and Compiling a Program from a CodeDOM Graph' as a starting point.




回答2:


The simplest thing to do would be to call csc, the C# compiler.

For example: (from the msdn page linked above)

csc /out:My.exe File.cs

Would produce an executable called My.exe from an input file called File.cs.

If the executable you want to compile is fairly complex, it might prove simpler to keep that pre-compiled and only compile a DLL that it calls a method in at runtime to obtain the private key, as that would be a simpler command line to pass to csc.



来源:https://stackoverflow.com/questions/3591587/dynamically-create-a-compiled-net-exe-from-source-code

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