锁定文本文件,这可能吗?我需要加密吗? C#.NET [英] Locking a textfile, is this possible? Do I need to encrypt it? C# .NET

查看:128
本文介绍了锁定文本文件,这可能吗?我需要加密吗? C#.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我正在处理的应用程序,因为我想了解更多有关streamwriters / readers的信息..不重要。



无论如何..它的作用是什么有一个textBox,在textBox中你可以编写东西,然后一旦应用程序关闭,它将该文本保存到我创建的扩展名的文件中,它可以通过Notepad ++或sublime打开但是当我用记事本打开它时它会清除整件事。



我想知道是否有办法锁定这些文件?也许加密他们,任何方式使他们不容易打开和混乱。它们基本上是文本文件,但不是真的,我已经创建了自己的扩展名为CREDS,因此它是一个CREDS文件。



我尝试过:



如果有任何答案但是找不到任何答案,我试过在msdn上查找。

I have a application that I am working on because I want to learn more about streamwriters / readers.. not important.

Anyways.. What it does is that there is a textBox, in that textBox you can write stuff and then once the application closes it saves that text to a file with an extension that I created, it can be opened by Notepad++ or sublime but when I open it with notepad it clears the whole thing.

I would like to know if there is a way to lock these files? Maybe encrypt them, any way to make them not easy to open and mess with. They are basically textfiles but not really, I've made my own extension called CREDS so its a CREDS File.

What I have tried:

I've tried looking on msdn if there was any answers but couldnt find anything about it.

推荐答案

另一种可能的途径是使用AES加密(msdn link: AES类



由于AES是对称密钥加密,您只需要一个密码就可以记住并且可以加密什么都有。您可以使用SHA512哈希生成加密密钥。



此示例包含扩展函数byte []。GetSubArray(int,int)。



Another possible avenue is to use AES Encryption (msdn link: AES Class)

Since AES is symmetric-key encryption, all you need is a passphrase that you can remember and you can encrypt just about anything. You can use a SHA512 hash to generate your crypto keys.

This example includes an extension function byte[].GetSubArray(int, int).

public byte[] Encrypt(string passphrase, string text)
{
  byte[] hashedPassphrase;

  // hash the passphrase
  using (HashAlgorithm hash = SHA512.Create())
  {
    hashedPassphrase = hash.ComputeHash(Encoding.UTF8.GetBytes(passphrase));
  }

  using (Aes crypto = Aes.Create())
  {
    // use the passphrase hash to set the initialization vector and crypto key
    crypto.IV = hashedPassphrase.GetSubArray(0, 16);
    crypto.Key = hashedPassphrase.GetSubArray(16, 32);

    // encrypt your data
    using (ICryptoTransform encryptor = crypto.CreateEncryptor(crypto.Key, crypto.IV))
    {
      MemoryStream memStream = new MemoryStream();
      using (CryptoStream encStream = new CryptoStream(memStream, encryptor, CryptoStreamMode.Write))
      {
        encStream.Write(Encoding.UTF8.GetBytes(text), 0, text.Length);
        encStream.FlushFinalBlock();
        return memStream.ToArray();
      }
    }
  }
}



除了crypto.CreateEncryptor()变为加密之外,解密几乎是相同的代码。 CreateDecryptor



实际的解密代码有点不同。


Decryption is nearly the same code except crypto.CreateEncryptor() becomes crypto.CreateDecryptor
and
the actual decryption code is a little different.

string plainText;

MemoryStream memStream = new MemoryStream(encryptedPassword);
CryptoStream decStream = new CryptoStream(memStream, decryptor, CryptoStreamMode.Read);

using (StreamReader reader = new StreamReader(decStream))
{
  plainText = reader.ReadToEnd();
}


这取决于用户弄乱文件的难易程度。您可以以二进制格式而不是文本格式存储内容。您可以使用Compression命名空间的类压缩(压缩)文件。您还可以生成受密码保护的zip文件(但这需要第三方工具,或链接加密流和压缩流)。
It depends on how hard it should be for users to mess with the files. You could store the content in binary format instead of text format. You could compress (zip) the files, using classes of the Compression namespace. You could also generate a password protected zip file (but that requires third party tools, or chaining of encryption streams and compression streams).


这篇关于锁定文本文件,这可能吗?我需要加密吗? C#.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆