11-27-2011، 01:37 PM
[C#] Secure Deletion
2 passes, one with random bytes, and one with zero bytes.
Can easily add more passes if you wish.
2 passes, one with random bytes, and one with zero bytes.
Can easily add more passes if you wish.
کد:
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool MoveFileEx(string lpExistingFileName, string lpNewFileName,
uint dwFlags);
private static int Main(string[] args)
{
if (args.Length != 1) return 1;
if (!File.Exists(args[0])) return 1;
string renamed = Path.GetDirectoryName(args[0]) + "\\" + "temp";
Random R = new Random();
MoveFileEx(args[0], renamed, 0x00000008);
FileStream fs = new FileStream(renamed, FileMode.Open, FileAccess.Write, FileShare.Write);
byte[] zero = new byte[1024];
byte[] random = new byte[1024];
R.NextBytes(random);
Console.WriteLine("Starting...");
//Random pass
for (int i = 0; i < fs.Length; i += random.Length)
{
fs.Seek(i, SeekOrigin.Begin);
fs.Write(random, 0, random.Length);
R.NextBytes(random);
}
fs.Seek(0, SeekOrigin.Begin);
fs.Flush();
//Zero pass
for (int i = 0; i < fs.Length; i += zero.Length)
{
fs.Seek(i, SeekOrigin.Begin);
fs.Write(zero, 0, zero.Length);
}
fs.Flush();
fs.Close();
fs.Dispose();
Array.Clear(random, 0, random.Length);
Array.Clear(zero, 0, zero.Length);
Console.WriteLine("Deleting...");
Thread.Sleep(1000);
File.Delete(renamed);
Console.WriteLine("Finished...");
return 0;
}