none
Problem reading saved encrypted data RRS feed

  • Question

  • At my wits end trying to make this right.  What I've been trying to do is encrypt a string and store it to a file and then read it back and decrypt.  I think my Encrypt/Decrypt functions are alright because I can encrypt a string and immediately pass it to Decrypt and it comes back as expected.  So, I'm left with the thought that something must be going ary reading the encrypted string from the file.  Below is my code.
      
          public static string EncryptString(string stringToEncrypt)
            {
                string ret = "";
                byte[] input = Encoding.UTF8.GetBytes(stringToEncrypt);  
                byte[] key = Convert.FromBase64String("1234567/LR3t49HgybYtns9nfCemAr0w"); 
                byte[] IV = Convert.FromBase64String("1234567lS/v=");
     
                TripleDESCryptoServiceProvider tdesProvider = new TripleDESCryptoServiceProvider();
                ICryptoTransform cryptoTransform  = tdesProvider.CreateEncryptor(key, IV);
                using(MemoryStream decryptedStream = new MemoryStream())
                using(CryptoStream cryptStream = new CryptoStream(decryptedStream, cryptoTransform, CryptoStreamMode.Write))
                {
                    cryptStream.Write(input, 0, input.Length);  
                    cryptStream.FlushFinalBlock();
                    ret = Convert.ToBase64String(decryptedStream.ToArray());
                }
                return ret;
            }

            public static string DecryptString(string stringToDecrypt)
            {
                string ret = "";
                byte[] input = Convert.FromBase64String(stringToDecrypt);
                byte[] key = Convert.FromBase64String("1234567/LR3t49HgybYtns9nfCemAr0w");
                byte[] IV = Convert.FromBase64String("1234567lS/v=");
                TripleDESCryptoServiceProvider tdesProvider = new TripleDESCryptoServiceProvider();
                ICryptoTransform cryptoTransform = tdesProvider.CreateDecryptor(key, IV);
                using (MemoryStream decryptedStream = new MemoryStream())
                using (CryptoStream cryptStream = new CryptoStream(decryptedStream, cryptoTransform, CryptoStreamMode.Write))
                {
                    cryptStream.Write(input, 0, input.Length);  
                    cryptStream.FlushFinalBlock();
                    ret = Encoding.UTF8.GetString(decryptedStream.ToArray());
                }
                return ret;
            }

            public static void WriteFile(string contents, string path)
            {
                using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    byte[] buffer = Encoding.Unicode.GetBytes(contents);
                    fs.Write(buffer, 0, buffer.Length);
                    fs.Flush();
                    fs.Close();
                }
            }

     public static string ReadFile(string path)
            {
                string result;
                using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    StreamReader sr = new StreamReader(fs);
                    result = sr.ReadToEnd();

                    sr.Close();
                    fs.Close();
                }
                return result;
            }

    Monday, September 28, 2009 11:07 PM

Answers

  • The problem is in ReadFile(), StreamReader will default to UTF8 encoding.  But you've written the string in UTF16 encoding.  The better fix is to use a StreamWriter in WriteFile().
    Hans Passant.
    Monday, September 28, 2009 11:20 PM
    Moderator

All replies

  • The problem is in ReadFile(), StreamReader will default to UTF8 encoding.  But you've written the string in UTF16 encoding.  The better fix is to use a StreamWriter in WriteFile().
    Hans Passant.
    Monday, September 28, 2009 11:20 PM
    Moderator
  • Looking at some other code using a streamwriter, I just now decided to try it and voila.

    Thanks for the reply
    Monday, September 28, 2009 11:26 PM