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

扶醉桌前 提交于 2020-01-05 06:55:34

问题


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);


来源:https://stackoverflow.com/questions/39810837/c-sharp-calculate-md5-from-self-to-check-that-file-is-not-modified

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