Sign or password any exe in c#
I have executables written in C#. I want to try this situation on my windows service. Normally we started the executable in our program using the code below:
Process process = new Process();
process.StartInfo.FileName = applicationpathandname.exe;
process.Start();
I want to compare my executable for sign or password before i start开发者_运维知识库 it. If the sign or password doesn't match it should exit the main program in the Windows service. So my pseudocode would be like this:
*/load exe
*/compare sign or password of my assembly
*/if match start exe
*/else exit main program
In effect, I have two questions:
- Can I sign or password protect any executable or assembly?
- Can I reach that sign or password executable in my other C# program?
I am not sure sure if I got your qustion right, but I'll try my best.
You can strong-name
your assemblies with the sn
tool.
Here is a short tutorial:
http://sharpertutorials.com/creating-strong-named-assemblies/enter link description here
You can then add the created snk
file to your projects in the solution. as a different option, instead of adding it manually in the
[assembly: AssemblyKeyFile("c:\\mykey.sn")]
attribute (like it is said in the article), you can even easier define it in the properties of each project under the tab Signing => Sign the assembly and enter the path to your snk file there.
After strong-naming the assemblies you can perform some checks on its password:
Checking an assembly for a strong name
I think in the end you want to hash the complete exe with SHA or another cryptographic-hash and use some encryption-algorithm (can be symmetric as you do the signing yourself I guess) with a salt value to encrpyt this hash. Then either pack the files-content together with the encrpyted hash and the salt-value into another file or add only the hash+salt in a additional file besides the exe.
On runtime to the same again: get the bytes of the exe (or from the combined file) hash them and compare if the hash is the same as the decrpyted hash.
If yes you can run the programm, if no there is some mischief going on or some bytes got flipped ;)
精彩评论