Search code examples
c#storing-information

C# Encrypt Text Output


I have created a few little programs that export data to a text file using StreamWriter and then I read them back in using StreamReader. This works great and does what I need it to do but I was wondering if there was a way that I could save this information without the user being able to access or modify it either intentionally or unintentionally. An example of something I would have in a text file would be if a checkbox was ticked, when you tick it it outputs "Ticked" to a text file, when the program is re - opened I know what state the form was in when it was closed. I obviously don't want to keep using text files. Does anyone have any ideas on how I can easily store this information without the user being able to modify it? Thank you very much.


Solution

  • The simplest way is to Base-64 encode/decode this text. This is not secure, but will prevent a casual user from modifying the data.

    static public string EncodeTo64(string toEncode)
    {
      byte[] toEncodeAsBytes
            = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
      string returnValue
            = System.Convert.ToBase64String(toEncodeAsBytes);
      return returnValue;
    }
    
    static public string DecodeFrom64(string encodedData)
    {
      byte[] encodedDataAsBytes
          = System.Convert.FromBase64String(encodedData);
      string returnValue =
         System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
      return returnValue;
    }
    

    EDIT: Real encryption

    #region Encryption
    
            string passPhrase = "Pasword";        // can be any string
            string saltValue = "sALtValue";        // can be any string
            string hashAlgorithm = "SHA1";             // can be "MD5"
            int passwordIterations = 7;                  // can be any number
            string initVector = "~1B2c3D4e5F6g7H8"; // must be 16 bytes
            int keySize = 256;                // can be 192 or 128
    
            private string Encrypt(string data)
            {
                byte[] bytes = Encoding.ASCII.GetBytes(this.initVector);
                byte[] rgbSalt = Encoding.ASCII.GetBytes(this.saltValue);
                byte[] buffer = Encoding.UTF8.GetBytes(data);
                byte[] rgbKey = new PasswordDeriveBytes(this.passPhrase, rgbSalt, this.hashAlgorithm, this.passwordIterations).GetBytes(this.keySize / 8);
                RijndaelManaged managed = new RijndaelManaged();
                managed.Mode = CipherMode.CBC;
                ICryptoTransform transform = managed.CreateEncryptor(rgbKey, bytes);
                MemoryStream stream = new MemoryStream();
                CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Write);
                stream2.Write(buffer, 0, buffer.Length);
                stream2.FlushFinalBlock();
                byte[] inArray = stream.ToArray();
                stream.Close();
                stream2.Close();
                return Convert.ToBase64String(inArray);
            }
    
            private string Decrypt(string data)
            {
                byte[] bytes = Encoding.ASCII.GetBytes(this.initVector);
                byte[] rgbSalt = Encoding.ASCII.GetBytes(this.saltValue);
                byte[] buffer = Convert.FromBase64String(data);
                byte[] rgbKey = new PasswordDeriveBytes(this.passPhrase, rgbSalt, this.hashAlgorithm, this.passwordIterations).GetBytes(this.keySize / 8);
                RijndaelManaged managed = new RijndaelManaged();
                managed.Mode = CipherMode.CBC;
                ICryptoTransform transform = managed.CreateDecryptor(rgbKey, bytes);
                MemoryStream stream = new MemoryStream(buffer);
                CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Read);
                byte[] buffer5 = new byte[buffer.Length];
                int count = stream2.Read(buffer5, 0, buffer5.Length);
                stream.Close();
                stream2.Close();
                return Encoding.UTF8.GetString(buffer5, 0, count);
            }
            #endregion