Why does not changing a few number of bytes in a file corrupts the file?

551 views Asked by At

In C#, I have a ZIP file that I want to corrupt by XORing or Nulling its bytes. (by Nulling I mean make all the bytes in the file zeros) XORing its bytes requires me to first, read the bytes to a byte array, XOR the bytes in the array with some value, then write the bytes back to the file.

Now, if I XOR/Null All (or half) of the file's bytes, it gets corrupted, but if Just XOR/Null some of the bytes, say the first few bytes (or any few number of bytes in any position of the file) it doesn't get corrupted, and by that I mean that i can still access the file as if nothing really happend.

Same thing happened with mp3 files.

Why isn't the file getting corrupted ? and is there a "FAST" way that i could corrupt a file with ?

the problem is that the zip file that I'm dealing with is big, so XORing/Nulling even half of its bytes will take a couple of secs.

Thank You So Much In Advance .. :)

3

There are 3 answers

0
Henk Holterman On BEST ANSWER

Just read all files completely and you probaly will get reading errors.

But of course, if you want to keep something 'secret', use encryption.

A zip contains a small header, a directory structure (a the end) and in between the individual files. See Wikipedia for details.

Corrupting the first bytes is sure to corrupt the file but it is also very easily repaired. The reader won't be able to find the directory block at the end.

Damaging the last block has the same effect: the reader will give up immediately but it is repairable.

Changing a byte in the middle will corrupt 1 file. The CRC will fail.

0
kevindaub On

It depends on the file format you are trying to "corrupt". It also depends on what portion of the file you are trying to modify. Lastly, it depends how you are verifying if it is corrupted. Most file formats have some type of error detection.

The other thing working against you is that the zip file format uses a CRC algorithm for corruption. In addition, there are two copies of the directory structure, so you need to corrupt both.

I would suggest you corrupt the directory structure at the end and then modify some of the bytes in the front.

0
K893824 On

I could just lock the zip entries with a pass, but I don't want anybody to even open it up and see what's in it

That makes it sound as if you're looking for a method of secure deletion. If you simply didn't want someone to read the file, delete it. Otherwise, unless you do something extreme like go over it a dozen times with different values or apply some complex algorithm over it a hundred times, there are still going to be ways to read the data, even if the format is 'corrupt'.

On the other hand, breaking a file simply to stop someone else accessing it conventionally just seems overkill. If it's a zip, you can read it in (there are plenty of questions here for handling archive files), encrypt it with a password and then write it back out. If it's a different type of file, there are literally a million different questions and solutions for encrypting, hiding or otherwise preventing access to data. Breaking a file isn't something you should being going out of your way to do, unless this is to help test some sort of un-zip-corrputing-program or something similar, but your comments imply this is to prevent access. Perhaps a bit more background on why you want to do this could help us provide a better answer?