Click here to Skip to main content
15,893,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 14-Jul-16 12:05pm
Comments
Richard MacCutchan 14-Jul-16 4:08am    
The extension name means nothing in reality, it just helps to group different files. If the data inside the file is text then any application will be able to read it. If you want to protect your data then you need to encrypt it in some manner.

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).

C#
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();
      }
    }
  }
}

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

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

using (StreamReader reader = new StreamReader(decStream))
{
  plainText = reader.ReadToEnd();
}
 
Share this answer
 
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).
 
Share this answer
 
Comments
devonJones 14-Jul-16 14:40pm    
I like the compressing way, never done it before but compressing it to a zip file and putting a password on the zipfile would be nice, but since im contstantly accessing the file from the application I dont really know if it would work

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900