Parsi Coders
سورس پاک کردن فایل بصورتی که ریکاوری نشه - نسخه قابل چاپ

+- Parsi Coders (http://parsicoders.com)
+-- انجمن: Software Development Programming (http://parsicoders.com/forumdisplay.php?fid=37)
+--- انجمن: C# Programming (http://parsicoders.com/forumdisplay.php?fid=55)
+--- موضوع: سورس پاک کردن فایل بصورتی که ریکاوری نشه (/showthread.php?tid=1289)



سورس پاک کردن فایل بصورتی که ریکاوری نشه - Amin_Mansouri - 11-27-2011

[C#] Secure Deletion

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;
        }