Thursday, September 28, 2006

Shredding a file in C#

A possible project that I have in mind would have the need to shred a file. That is, not just to delete the file, but to wipe its contents from the disk such that a snoop with the right tools would be unable to recover it.

How to do this in C#? I knew there were tools that do this, but I'd never come across any code that does it. Trusty Google turned up a post by Darrell Hawley (whose parents gave him a great name but alas, spelled it incorrectly) who pondered the same question.

Resuming my search, I found a great article by Gregory A. Wolking from 2001: Shred Deleted Files. Unfortunately, the source was not available for free. Cheapskate that I am, I kept looking, and finally found the source (SHRED.ZIP) here.

I took a quick look at the C++ source and found that the task didn't seem as involved as I had thought that it might be. A key point in the code seemed to be opening the file with disabled write caching via the FILE_FLAG_WRITE_THROUGH flag. I Googled that flag and found a C# article named .NET cryptography library for files and strings which uses the flag in a method named OpenFileForSecureOverwrite(). Perfect!

That's about as far as I got, but these resources should be a good starting point if that project of mine ever gets beyond a germ of an idea.



No comments: