Shredding a File
I was wondering how to shred a file with C# the other day so I wrote the following lines of code.
System.IO.FileStream stream = new System.IO.FileStream(label1.Text, System.IO.FileMode.Open);
byte[] readbytes = new byte[stream.Length];
long length = stream.Length;
int position = 0;
for (int i = 0; i < readbytes.Length; i++ )
{
readbytes[i] = Randomize();
}
stream.Write(readbytes, position, (int)length);
stream.Close();
System.IO.File.Delete(label1.Text);
I know that the contents of the file will changed before it is deleted, but does the file get written back to the same exact location on disk? The great wiseman, Google, doesn't seem to be of much help on this one.


3 Comments:
"Don't forget to Flush!"
eg, stream.Flush();
Also, this won't work very well for huge files that are bigger than RAM, because your buffer won't be loaded.
Looks like I'll have to code my own! :)
By
Gibson Security, At
Tue Jun 27, 05:11:00 AM
Darryl Whitmore - forgive the "y" in his name - did the shredding work for me. He found 2 sources of sample code that show how to do it: one in C++ and the other in C#. The C++ code is located at
http://cyberia.dnsalias.com/Windows.App.10.htm (look for the shred.zip file). Daryl pointed out that "One key point in the code is opening the file with disabled write caching with the FILE_FLAG_WRITE_THROUGH flag". Keep it in mind for the C++ sample. The C# example is at http://www.codeproject.com/dotnet/CryptoLib.asp. It didnt sound like he was going to be working on this anytime soon, but i'll try to get him to keep me posted about developments. Thanks, Darryl
By
Darrell Hawley, At
Tue Sep 26, 02:01:00 PM
Darryl Whitmore is now a blogger! Keep up on his file shredding quest at http://copy-paste-repeat.blogspot.com/.
By
Darrell Hawley, At
Mon Oct 02, 01:09:00 PM
Post a Comment
Subscribe to Post Comments [Atom]
<< Home