0

After writing a basic LFSR-based stream cipher encryption module in C, I tried it on usual text files, and then on a .exe file in Windows. However, after decrypting it back the file is not running, giving some error about being a 16-bit. Evidently some error in decrypting. Or are files made so that if I tamper with their binary code they become corrupted?

I'm checking my program on text-files in the hope of locating any error on my part. However, the question is had anyone tried running your own encryption programs on an executable file? Is their any obvious answer to this?

AruniRC
  • 5,070
  • 7
  • 43
  • 73
  • 1
    as what you said implied that it worked on text files, _make sure your opening the input and output files in binary mode_ (applies mostly to windows and dos as unixes don't have a text mode) – Dan D. Dec 16 '11 at 11:57
  • yes, they are being opened in binary mode. – AruniRC Dec 17 '11 at 03:09

3 Answers3

5

There is nothing special about executables. They are obviously binary files and thus contain 00 bytes and bytes >127. As long as your algorithm is binary safe, it should work.

Compare the original file and the decrypted file using a hex-editor. To see how they differ.

The error you get means that you didn't decrypt the executable header correctly, so the decryption mistake must already affect the first few bytes of your file.

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
2

Evidently some error in decrypting. An exe is a bag 'o bytes just like any other file, there's no magic. You are merely likely to run into byte values that you won't get in a text file. Like a zero.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

A decryption process should be the inverse of its encryption. In other words, Decrypt(Encrypt(X)) == X for all inputs X, of all possible lengths, of all possible byte values.

I suggest you build yourself a test harness that will run some pairwise checks with randomised data so you can prove to yourself that the two transformations do indeed cancel each other out. I mean something like:

for length from 0 to 1000000:
 generate a string of that length with random contents
 encrypt it to a fresh memory buffer
 decrypt it to a fresh memory buffer
 compare the decrypted string with the original string     

Do this first of all on in-memory strings so you can isolate the algorithm from your file-handling code. Once you've proved the algorithm is properly inverting, you can then do the same for files; as others have said you might well be running into issues with handling binary files, that's a common gotcha.

crazyscot
  • 11,819
  • 2
  • 39
  • 40