C# Code Snippets

Wednesday, December 27, 2006

Secure Deletion of files

Here it is a safe method based on the .NET Random class:
///
/// Secure Deletion method
///

///
/// the path of the file to be erased
///
///
/// number of times to run the erase algorithm
///
public static void EraseFile(string strFilename, int nTimes)
{
if (!File.Exists(strFilename))
{
throw new
ArgumentException("The file does not exist");
}
while (nTimes >= 0)
{
FileStream fs = File.OpenWrite(strFilename);
int nBytesInFile = (int)fs.Length;

Random random = new Random();
Byte[] b = new Byte[nBytesInFile];
random.NextBytes(b);

for (int i = 0; i < nBytesInFile; i++)
{
fs.WriteByte(b[i]);
}
fs.Close();

nTimes--;
}
}

0 Comments:

Post a Comment

<< Home