Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Super Delete that file!

, 20 May 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
--- UPDATED with better console stuff and now using the crypto random number generator.I know that some of you have found yourself in that situation that requires you to delete something from your hard drive permanently, thus rendering your data unrecoverable.Well, there isn't a good way to do that
--- UPDATED with better console stuff and now using the crypto random number generator.

I know that some of you have found yourself in that situation that requires you to delete something from your hard drive permanently, thus rendering your data unrecoverable.

Well, there isn't a good way to do that through C# without some tomfoolery... so I hereby present you with SuperDelete. This code will permanently delete whatever file you pass in, overwriting the existing data with cryptographically random numbers a specified number of times before deleting the file. This renders the previously existing data pretty much useless to anyone.

As long as data is being flushed to the drive after each file write (hence the foreach over the FileStream), and your system doesn't have some sort of advanced write-caching in place that will cache data over multiple file opens and closes, your data will be as good as dead once this code is finished with it.

The recommended overwrite of 10 times is to cover the nominal servo-positioning errors that occur in all hard drives, however you can push this up to whatever value you want to make yourself feel secure.

I also recommend that you read this article on data storage and recovery: http://wipe.sourceforge.net/secure_del.html.

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;

namespace SuperDelete
{
class Program
{
// For large files this is the best performing buffer size
static byte[] buffer = new byte[32768];

/// <span class="code-SummaryComment"><summary>
</span>/// Main application entry point
/// 
/// Usage: SuperDelete.exe filename number_of_wipes
/// Example: SuperDelete.exe \"C:\\mySecrets.txt\" 10"
/// <span class="code-SummaryComment"></summary>
</span>static void Main(string[] args)
{
Console.WriteLine("Usage: SuperDelete.exe <filename> <number of wipes>");
Console.WriteLine("Example: SuperDelete.exe \"C:\\mySecrets.txt\" 10");
try
{
Console.WriteLine("");
Console.WriteLine("Are you sure you want to SuperDelete the file {0}?  (Y/N)", args[0]);

if (Console.ReadKey().Key == ConsoleKey.Y)
SuperDeleteFile(args[0], Convert.ToInt32(args[1]));
}
catch (Exception e)
{
Console.WriteLine("Failed to super delete :(");
Console.WriteLine(e.Message);
}
}

/// <span class="code-SummaryComment"><summary>
</span>/// Super delete that file!
/// <span class="code-SummaryComment"></summary>
</span>/// <span class="code-SummaryComment"><param name="fileName">Name of the file to delete</param>
</span>/// <span class="code-SummaryComment"><param name="passes">Number of passes to use in wiping the file.  Recommended is 10</param>
</span>public static void SuperDeleteFile(string fileName, int passes)
{
Console.WriteLine("");
Console.WriteLine("Deleting...");

int cursorTop = Console.CursorTop;
int cursorLeft = Console.CursorLeft;            

for (int i = 0; i < passes; i++)
{
using (FileStream fileToOverwrite = File.Open(fileName, FileMode.Open, FileAccess.Write, FileShare.None))
{

// 24 is the RSA-AES CSP
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(new CspParameters(24));

rng.GetBytes(buffer);

// Writing more than the file size (unless it comes out to be the exact size of the buffer *n) will mask the original filesize
long bytesToWrite = fileToOverwrite.Length;
long originalFileSize = fileToOverwrite.Length;

while (bytesToWrite > 0)
{
Console.CursorLeft = cursorLeft;
Console.CursorTop = cursorTop;

string messageOne = string.Format("Pass {0}, wiping bytes {1} - {2} of {3}. ", i, originalFileSize - bytesToWrite, (originalFileSize - bytesToWrite) + buffer.Length, originalFileSize);
string messageTwo = string.Format("This pass has %{0} left to complete.", Convert.ToInt64((Convert.ToDouble(bytesToWrite) / Convert.ToDouble(originalFileSize)) * 100));

Console.WriteLine(PadToLength(messageOne, Console.BufferWidth));
Console.WriteLine(PadToLength(messageTwo, Console.BufferWidth));

fileToOverwrite.Write(buffer, 0, buffer.Length);

bytesToWrite -= buffer.Length;
}
}
}

File.Delete(fileName);
}

static string PadToLength(string original, int length)
{
if (original.Length >


For the complete program, including the disabling of write-caching, please purchase my Super Delete application.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Aron Weiler
Technical Lead CareFusion
United States United States
I just looooove software.
 
Check out my technical blog here: The Fyslexic Duck. You can find most of what I've put on CodeProject there, plus some additional technical articles.

Comments and Discussions

 
GeneralNot effective on all storage devices Pinmembersupercat920-May-11 16:09 
GeneralRe: Not effective on all storage devices PinmvpAspDotNetDev20-May-11 16:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 20 May 2011
Article Copyright 2011 by Aron Weiler
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid