Wednesday, September 22, 2010

File Shredder

Do you have critical data files that you need to permanently destroy? you can use file shredders for this task, but if you're paranoid and don't want to run executables downloaded from untrusted resources, you can simply write your own file shredder, all you need is an IDE (e.g. Visual Studio, Net Beans.. etc.)

File Deletion Concept (OS 101):
The OS does not physically remove deleted files from your hard disk. Emptying your Recycle Bin does NOT protect you at all. Your sensitive files can be recovered, even after you format your hard disk! When you delete a file from Windows, the OS just marks the file as deleted in the file allocation table (FAT), but the data of the file is still on your hard disk, even though not accessable through the system friendly UI. There are many tools available which can let you or others easily recover your sensitive files even after you format your hard disk. Thus, make sure you ALWAYS shred your sensitive files if you plan to sell your PC later on.

A simple analogy would be like adding a new column to a database table to indicate if a record has been deleted, without actually deleting the record from the database. And in the system front-end, fitching only records of which the Deleted column is set negatively.


Technique:
The simplest technique to shred a file, is to overwrite the contents of that file multiple times with junk data before the file is deleted, so that the previous portions of the hard disk drive that were allocated to that file would be actually overwritten and untracable.

Download Executable
Download Source Code



Have a glance at the code
Main Function (DestroyFile) [Tip]: This function could also be implemented in a separate asynchronous thread to keep the main thread and UI responding and interactive with the user while the file is being shredded, because if the file size is too large, then the main thread (including the UI event handlers) will stop responding until the file shredding operation is completely executed.

private void DestroyFile(string filePath)
        {
            try
            {
                FileStream filestream = new FileStream(filePath, FileMode.Open, FileAccess.Write);
                long length = filestream.Length;
                StreamWriter streamWriter = new StreamWriter(filestream);
                char[] buffer = new char[length];
                for (long i = 0; i < length; i++)
                {
                    buffer[i] = '0';
                }
                streamWriter.Write(buffer);

                streamWriter.Close();
                filestream.Close();

                File.Delete(filePath);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error!");
            }
        }