Everybody knows that simply deleting a file from a hard disk doesn't truly make the file impossible to recover. Often, this is a good thing, but sometimes you want a file to be thoroughly deleted. Keep reading to find out how to accomplish this.
A downloadable zip of the source code is available here.
While cleaning my hard disk, I started to wonder what happens to files when they are deleted. We hear all the time that when we delete files, they do not really disappear and can be recovered. To understand what happens to the files, we need to know what happens when files are created. When a new file is created, Windows allocates a space or place on the hard disk that uniquely identifies this file. Windows does not really keep taps on what a file contains; the only things it knows about a file is where it is located, its size and perhaps its name.
Now, when you delete a file (empty the recycling bin), all that happens is that Windows puts up a flag indicating that the space that used to belong to the deleted file is now available for re-allocation. At this point the file can still be recovered, but if you then create a new file or do anything that requires the OS to write something to the hard disk (such as restarting your computer), then the file will not be recoverable.
So, to completely delete a file, Windows overwrites it over and over. The more it overwrites, the more difficult it becomes to recover a file, and thus the more secure the deletion of the file. The US department of Defense has created a standard (considered to be the safest) called ĎDOD 5220.22-M.' It encourages rewriting file contents up to 20 times to make it completely unrecoverable, even with the most sophisticated recovery software known today. What we will do in this tutorial is devise an algorithm that will do the following:
Open and overwrite the file contents.
Rename and delete the file.
Itís fairly simple, but sufficient for anyone who wants to permanently delete their "secret" files. With this algorithm, even if someone manages to undo the renaming of the file or is able to undelete the file, the file would have been overwritten so many times that it will not be possible to recover its contents.