6

我的任务是覆盖几台笔记本电脑上的所有可用空间 3 次。我知道有一些替代方案,但我想知道事情是如何工作的,以及我是否可以自己用 C# 来做。

1) 是的,我知道有很多免费软件应用程序可以做到这一点

2) 不,我们不需要遵守任何特定的政府标准

我在哪里寻找关于如何开始的想法?

谢谢,如果你能指出我正确的方向。

可以用C#实现吗?如果是这样,怎么做?

4

6 回答 6

3

简单算法:

  • 创建一个包含任意文本的大文本文件(最好使用预先创建的文件,而不是出于性能原因重新生成随机文件。测试一下。)
  • 创建一个巧妙的文件夹和文件命名方案,以便能够跟踪文件。您还应该使用您的应用程序跟踪文件,但如果它崩溃,尤其是在前几次测试运行快结束时,您将希望能够轻松找到并清理您方便的工作。
  • 将其写入HDD直到它已满
  • 删除您创建的文件
  • 再重复以上步骤两次

更新:根据后续讨论对擦除进行更高级的考虑:

  • 在第一次通过写入 0x0000 值的文件(所有位关闭)
  • 在第二遍将所有位写入 0xFFFF(所有位打开)
  • 在最后一次通过 0x0000 重复

以上忽略了一些想法,例如文件的最佳大小是多少,这取决于您的文件系统。当您靠近已装满的硬盘时,您可能还会从操作系统获得不同的行为......

于 2010-01-14T15:21:07.983 回答
3

真的很危险但是..

您可以使用Defrag API(这里是 C# 包装器)来获取驱动器“映射”并专门针对可用空间并将垃圾写入磁盘的这些部分。

于 2010-01-14T15:33:08.050 回答
1

检查SDelete文档,也许您可​​以在那里找到线索。

于 2010-01-14T15:23:10.533 回答
0

您将不得不进行一些低级别的操作,因此您肯定必须与 Win32 API 对话。我还没有做过这种事情,所以我不能给你细节,但一个很好的起点可能是 Win32 API 参考:http: //msdn.microsoft.com/en-us/library/aa383749 %28VS.85%29.aspx

我真的不是这个领域的专家,但在我看来,你需要:1)获取有关文件系统开始和停止位置的信息 2)使用未删除的文件作为参考,获取应该是可用空间的物理位置列表 3) 将 0 写入这些位置

也许这不是一个很好的答案,因为我不是该领域的专家,但是评论有点太长了;)我希望能有所帮助。

于 2010-01-14T15:21:42.210 回答
0

这段代码来自我认为的代码项目。我不确定原始文章在哪里,但它可以满足您的要求:

根据评论,我显然需要多喂一点..

根据您的要求,您可以非常简单地执行此操作。

  1. 制作 1 个大文件,填充驱动器上剩余的可用大小。然后简单地擦除这个文件。

  2. 制作几个文件,直到您的驱动器已满。(如果您想在机器运行时使用机器,这可能会更好)。然后您可以开始擦除每个文件,因此实际上系统拥有一个完整的硬盘驱动器的总时间比使用方法 1 的时间要短。但它可能会慢一些并且使用更多的代码。

使用的好处是一些简单的代码供您使用。您不必使用会让您不知所措的低级 API。

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's 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
}
于 2010-01-14T15:24:24.883 回答
-1
System.Diagonstics.Process.Start("chipher.exe /WC:\");

默认情况下这是异步的,你明白了。

于 2010-01-14T15:26:53.287 回答