C# - Calculate md5 from self to check that file is

2019-07-26 23:48发布

问题:

I want to check that my program is not modified (cracked).

So I want program to calculate md5 from self exe and compare.

if(GetMD5FromSelf() != "hash")
    Application.Exit(); //modified so exit

But when I put hash to string then the md5 of file will get changed.

Is there any way to do this?

回答1:

These are some ways you could do it,

Option 1

You could store the hash online, This is probably safer because if someone is going to change your program they can also change the hash.

Option 2

You could add 4 bytes and a string to the end of your application and save the checksum there, Beware to not include those in your checksum and only validate your own file size, not the 4 bytes and the string.

code snippet

  List<byte> total = new List<byte>(File.ReadAllBytes(System.Reflection.Assembly.GetEntryAssembly().Location));
                byte[] totalByteArray = total.ToArray(); 
                int OwnSize = 115200;//Size of you exe file without checksum
                int Md5Length = BitConverter.ToInt32(totalByteArray, OwnSize+4);
                string NormalFileNameString = Encoding.ASCII.GetString(totalByteArray, OwnSize, Md5Length);