Wipe free space on your hard drive with C #

I was instructed to overwrite all the free space on several laptops 3 times. I know there are several alternatives, but I like to know how everything works, and if I can do it myself with C #.

1) yes, I know that there are many free applications that will do this

2) no, we do not need to comply with any specific government standard

Where to look for ideas on how to start this?

Thanks if you can point me in the right direction.

Can this be done using C #? If so, how?

+6
security c # hard-drive
source share
6 answers

Simple algorithm:

  • Create a large text file full of arbitrary text (it is best to use a pre-created file instead of regenerating random ones for performance reasons).
  • Create a smart file and file name scheme to be able to track files. You should also keep track of the files with your application, but if it crashes, especially at the end of the first few test runs, you will want to easily find and clear your convenient work.
  • Burn it to your hard drive until complete
  • Delete created files.
  • Repeat the above steps two more times

Update: A closer look at wiping for later discussion:

  • At the first stage, write files with values ​​0x0000 (all bits are off)
  • In the second step, write all bits as 0xFFFF (all bits are on)
  • In the last walk, repeat with 0x0000

The above ignores a few ideas, such as the best file size, anyway depending on your file system. You can also get different actions from the OS as you get closer to a full hard drive ...

+3
source share

It is really dangerous, but ..

You can use the Defrag API (here the C # wrapper) to get a disk map and, in particular, to target freespace and write garbage to those parts of the disk.

+2
source share

This code is from Draft Code I think. I'm not sure where the orignal article is located, but it does what you requested:

Based on the comments, I obviously need a spoonfeed a bit more.

You can do this very simply based on your requirements.

  • Make 1 large file that fills the remaining free size of your disk. Then just wipe this file.

  • Make a few files until your drive is full. (It might be better if you want to use the machine while it is running). Then you can start erasing each file, so actually the total time the system has a complete disj hard drive is less than using method 1. But it will probably be a little slower and will use a little more code.

The advantage of using is a few simple instructions for you. You do not need to play with the low-level APIs that let you down.

using System; using System.IO; using System.Security.Cryptography; namespace QuickStarterShared { public class Wipe { /// <summary> /// Deletes a file in a secure way by overwriting it with /// random garbage data n times. /// </summary> /// <param name="filename">Full path of the file to be deleted</param> /// <param name="timesToWrite">Specifies the number of times the file should be overwritten</param> public void WipeFile(string filename, int timesToWrite) { #if !DEBUG try { #endif if (File.Exists(filename)) { // Set the files attributes to normal in case it read-only. File.SetAttributes(filename, FileAttributes.Normal); // Calculate the total number of sectors in the file. double sectors = Math.Ceiling(new FileInfo(filename).Length/512.0); // Create a dummy-buffer the size of a sector. byte[] dummyBuffer = new byte[512]; // Create a cryptographic Random Number Generator. // This is what I use to create the garbage data. RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); // Open a FileStream to the file. FileStream inputStream = new FileStream(filename, FileMode.Open); for (int currentPass = 0; currentPass < timesToWrite; currentPass++) { // Go to the beginning of the stream inputStream.Position = 0; // Loop all sectors for (int sectorsWritten = 0; sectorsWritten < sectors; sectorsWritten++) { // Fill the dummy-buffer with random data rng.GetBytes(dummyBuffer); // Write it to the stream inputStream.Write(dummyBuffer, 0, dummyBuffer.Length); } } // Truncate the file to 0 bytes. // This will hide the original file-length if you try to recover the file. inputStream.SetLength(0); // Close the stream. inputStream.Close(); // As an extra precaution I change the dates of the file so the // original dates are hidden if you try to recover the file. DateTime dt = new DateTime(2037, 1, 1, 0, 0, 0); File.SetCreationTime(filename, dt); File.SetLastAccessTime(filename, dt); File.SetLastWriteTime(filename, dt); File.SetCreationTimeUtc(filename, dt); File.SetLastAccessTimeUtc(filename, dt); File.SetLastWriteTimeUtc(filename, dt); // Finally, delete the file File.Delete(filename); } #if !DEBUG } catch(Exception e) { } #endif } } # region Events # region PassInfo public delegate void PassInfoEventHandler(PassInfoEventArgs e); public class PassInfoEventArgs : EventArgs { private readonly int cPass; private readonly int tPass; public PassInfoEventArgs(int currentPass, int totalPasses) { cPass = currentPass; tPass = totalPasses; } /// <summary> Get the current pass </summary> public int CurrentPass { get { return cPass; } } /// <summary> Get the total number of passes to be run </summary> public int TotalPasses { get { return tPass; } } } # endregion # region SectorInfo public delegate void SectorInfoEventHandler(SectorInfoEventArgs e); public class SectorInfoEventArgs : EventArgs { private readonly int cSector; private readonly int tSectors; public SectorInfoEventArgs(int currentSector, int totalSectors) { cSector = currentSector; tSectors = totalSectors; } /// <summary> Get the current sector </summary> public int CurrentSector { get { return cSector; } } /// <summary> Get the total number of sectors to be run </summary> public int TotalSectors { get { return tSectors; } } } # endregion # region WipeDone public delegate void WipeDoneEventHandler(WipeDoneEventArgs e); public class WipeDoneEventArgs : EventArgs { } # endregion # region WipeError public delegate void WipeErrorEventHandler(WipeErrorEventArgs e); public class WipeErrorEventArgs : EventArgs { private readonly Exception e; public WipeErrorEventArgs(Exception error) { e = error; } public Exception WipeError{get{ return e;}} } # endregion # endregion } 
+1
source share

You will need to do some manipulations at a low level, so you will definitely have to talk to the Win32 API. I did not do this, so I can’t give you the details, but a link to the Win32 API might be a good place to search: http://msdn.microsoft.com/en-us/library/aa383749%28VS.85%29.aspx

I'm really not an expert in this field at all, but it seems to me that I naively understand what you need: 1) get information about where the file system starts and stops 2) using unused files as a link, get a list of physical locations of what should be free space 3) write 0 to these places

Maybe this is not an excellent answer, since I am not an expert in this field, but it was too long for a comment;) I hope this helps a little.

0
source share

Check the SDelete documentation, maybe you can get the key there.

-one
source share
 System.Diagonstics.Process.Start("chipher.exe /WC:\"); 

By default it is asynchronous, you get the idea.

-one
source share

All Articles