0

I've created a program to receive a hash code of an .exe file. And than with streamwriter I wrote it to an .exe file. If I wanna open the .exe of it I get the error: is not a valid win32 application.

What can I do with the hash and is it possible to create an .exe file from it that works.

Thank you very much!

byte[] hash;
using (Stream stream = File.OpenRead(@"C:\Program files\CCleaner\CCleaner.exe"))
{
    hash = SHA1.Create().ComputeHash(stream);
}
string base64Hash = Convert.ToBase64String(hash);
Console.WriteLine(base64Hash);

StreamWriter myWriter = new StreamWriter(@"C:\Users\Win7\Desktop\Hash.exe");
myWriter.Write(base64Hash);
6
  • 1
    No, this is never going to work. What are you trying to do? Normally you would calculate a hash for a received file, and compare it to the known good hash for the file to check its integrity. Commented Aug 3, 2013 at 14:13
  • @Lee I wrote that if you get a hashcode, that you create a program from it. So I wanna test it Commented Aug 3, 2013 at 14:14
  • What are you expecting the program to do? Windows executables are usually Portable executable files. The only way this could do anything is if your hash just happened to be a valid PE file. Commented Aug 3, 2013 at 14:17
  • 1
    You want to know if the SHA-1 hash of an executable file is itself a valid PE file which does the same as the source program? No, that is not possible. Commented Aug 3, 2013 at 14:27
  • 1
    A hash code is not analogous to DNA. It's more like a fingerprint. You can't recreate a person from a fingerprint. Commented Aug 5, 2013 at 3:12

3 Answers 3

1

The hash code of an executable is a string, not a valid executable. If you want to store the hash code, then write it to a text file, like this:

byte[] hash;
using (Stream stream = File.OpenRead(@"C:\Program files\CCleaner\CCleaner.exe"))
{
    hash = SHA1.Create().ComputeHash(stream);
}
string base64Hash = Convert.ToBase64String(hash);
Console.WriteLine(base64Hash);

StreamWriter myWriter = new StreamWriter(@"C:\Users\Win7\Desktop\Hash.txt");
myWriter.Write(base64Hash);
Sign up to request clarification or add additional context in comments.

Comments

0

The reason for this error:

I get the error: is not a valid win32 application

is that you are overwriting the beginning of the application. While you can append anything to most exe files, overwriting data is not going to work.

If you append the hash like this:

StreamWriter myWriter = new StreamWriter(@"C:\Users\Win7\Desktop\Hash.exe", true);

the application should run normally. Within this application, you can access the last bytes of the exe file to retrieve the hash.

1 Comment

thanks, but I still get that error, but with the comments of Lee I got the feeling that it ain't possible to do what I wanted to do, what is cloning a program
0

You need to learn what a Hash Algorithm is. It cannot be used for what you intend. A hash cannot be reversed to get the original contents. That would be fantastic data compression.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.