vb.net 如何以编程方式销毁/使文件不可读

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23075743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:22:54  来源:igfitidea点击:

How to destroy/make a file unreadable programmatically

c#vb.net

提问by Codemunkeee

While I was doing some stuff with vb programming, I have come across a pdf file that is corrupted and unreadable. Now, I just wondered if is it possible to create an unreadable file programmatically?

当我在用 vb 编程做一些事情时,我遇到了一个已损坏且无法读取的 pdf 文件。现在,我只是想知道是否有可能以编程方式创建一个不可读的文件?

I actually have it, when I open the file it says "There was an error opening this document. There was a problem reading this document(14)"

我真的有它,当我打开文件时,它说“打开此文档时出错。阅读此文档时出现问题(14)”

Now, what I want to achieve is on how to "destroy" a file. Any kind of file. Say I have a working pdf file which can be opened by Adobe Reader, how to make it "unreadable" to Adobe Reader so that when I open it, it will show some messagebox saying "error opening file" or something like that.. here's an example link

现在,我想要实现的是如何“销毁”文件。任何类型的文件。假设我有一个可以由 Adob​​e Reader 打开的工作 pdf 文件,如何使它对 Adob​​e Reader“不可读”,以便当我打开它时,它会显示一些消息框说“错误打开文件”或类似的东西..这里是示例链接

回答by nvoigt

Technically, short of hardware error, there is no such thing as "unreadable". A file contains bytes. It's readable. However, a PDF-reader expects those bunch of bytes to have a specific format. If it does not, the program cannot make a valid pdf from it.

从技术上讲,除了硬件错误,没有“不可读”这样的东西。文件包含字节。它是可读的。但是,PDF 阅读器希望这些字节具有特定的格式。如果不是,则程序无法从中生成有效的 pdf。

Imagine a piece of paper for a actor to read. If you ripped half the page of, the other page is far from unreadable. It's just that the actor will complain, that he won't play his role on only half a script.

想象一张供演员阅读的纸。如果你撕了一半的页面,另一页就不是不可读了。只是演员会抱怨,他不会在一半的剧本中扮演他的角色。

You can overwrite the bytes in a file. If your file is a 500 bytes file that can be interpreted as a pdf, you can instead make it a file with 2 bytes that say "FU". Then it's a two byte file. The pdf reader will complain. If you open it in a text editor, it will read "FU". One programs garbage is another programs data.

您可以覆盖文件中的字节。如果您的文件是可以解释为 pdf 的 500 字节文件,您可以改为将其设为 2 字节的文件,并显示“FU”。然后它是一个两字节的文件。pdf 阅读器会抱怨。如果您在文本编辑器中打开它,它将显示“FU”。一个程序垃圾是另一个程序数据。

回答by Scott Chamberlain

What made the pdf unreadable is the file was not in the format the reader expects it to be in. For a basic txt file there is no format to follow, whatever bytes are there is what is displayed on screen. You will likely get gibberish, but it would still open.

使 pdf 不可读的原因是该文件不是读者期望的格式。对于基本的 txt 文件,没有格式可遵循,无论字节是什么,屏幕上都会显示什么。你可能会得到胡言乱语,但它仍然会打开。

To "Break" the file you would have to break the filesystem. There may be ways to do it but you can't do it with managed code, you will need to invoke native API calls to get to the raw filesystem to do it and you would need to parse the filesystem yourself, windows does not provide anything like that. Perhaps look for a open source hex editor that can read the MRR to help figure out which calls you would need to make. (IMPORTANT NOTE: if you try this you will likely make your computer un-bootable during your experimentation, backup all of your files before you start playing with the filesystem structure)

要“破坏”文件,您必须破坏文件系统。可能有办法做到这一点,但你不能用托管代码做到这一点,你需要调用本机 API 调用来获取原始文件系统来做到这一点,你需要自己解析文件系统,windows 不提供任何东西像那样。也许寻找一个可以读取 MRR 的开源十六进制编辑器,以帮助确定您需要进行哪些调用。(重要提示:如果您尝试此操作,您可能会在实验期间使您的计算机无法启动,请在开始使用文件系统结构之前备份所有文件)

回答by Icemanind

Use this to encrypt the file:

使用它来加密文件:

byte[] keySalt = new byte[] { 0x49, 0x76, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 };  

Encrypt("original.txt",  "encrypted.txt", "myPassword", keySalt);  

public static void Encrypt(string fileIn, string fileOut, string pass, byte[] keySalt)  
{  
   PasswordDeriveBytes pdb = new PasswordDeriveBytes(pass, keySalt);  

   Rijndael alg = Rijndael.Create();  
   alg.Key = pdb.GetBytes(32);  
   alg.IV = pdb.GetBytes(16);  

   using (FileStream fsOut = new FileStream(fileOut, FileMode.OpenOrCreate, FileAccess.Write))  
   {  
      using (FileStream fsIn = new FileStream(fileIn, FileMode.Open, FileAccess.Read))  
      {  
         using (BinaryReader br = new BinaryReader(fsIn, Encoding.Default))  
         {  
            using (CryptoStream cs = new CryptoStream(fsOut, alg.CreateEncryptor(), CryptoStreamMode.Write))  
            {  
              int len = (int)br.BaseStream.Length;  
              cs.Write(br.ReadBytes(len), 0, len);  
              cs.FlushFinalBlock();  
            }  
         }  
       }  
   }  
}  

回答by bit

If you want to hide the contents of the file for others, you could simply encrypt the file.

如果您想为其他人隐藏文件的内容,您可以简单地加密文件。