title: C# Encrypt and Decrypt a File Stream: System.Runtime.Serialization.SerializationException
body: <p>I need to encrypt and decrypt file - streams so the data that is written to the file system is encrypted already and gets decrypted in memory only after reading.</p>
  
  <p>I wrote a small demo - program: </p>
  
  <pre><code>using System.Data;
  using System.IO;
  using System.Runtime.Serialization.Formatters.Binary;
  using System.Security.Cryptography;
  using System.Windows.Forms;
  
  namespace House
  {
      public partial class Form1 : Form
      {
          public static string PassPhrase =&gt; "JustATest";
          public static string PathToFile = @"C:\TMP\test.bin";
  
          public Form1()
          {
              InitializeComponent();
  
              var iv = new byte[]
              {
                  222, 49, 46, 125, 250, 194, 122, 241, 234, 22, 151, 76, 181, 83, 149, 92, 195, 11, 146, 213, 195, 40, 0,
                  183, 84, 149, 185, 100, 250, 156, 123, 75
              };
  
              if (File.Exists(PathToFile))
                  File.Delete(PathToFile);
  
              var set = CreateData();
  
              using (var symmetricKey = new RijndaelManaged())
              {
                  var password = new Rfc2898DeriveBytes(PassPhrase, 100);
                  var keyBytes = password.GetBytes(32);
  
                  symmetricKey.BlockSize = 256;
                  symmetricKey.Mode = CipherMode.CBC;
                  symmetricKey.Padding = PaddingMode.PKCS7;
                  var encryptor = symmetricKey.CreateEncryptor(keyBytes, iv);
  
                  using (var fileStream = new FileStream(PathToFile, FileMode.Create))
                  {
                      using (var cryptoStream = new CryptoStream(fileStream, encryptor, CryptoStreamMode.Write))
                      {
                          var formatter = new BinaryFormatter();
                          formatter.Serialize(cryptoStream, set);
                          cryptoStream.FlushFinalBlock();
                          fileStream.Flush(true);
                      }
                  }
              }
  
              using (var symmetricKey = new RijndaelManaged())
              {
                  var password = new Rfc2898DeriveBytes(PassPhrase, 100);
                  var keyBytes = password.GetBytes(32);
  
                  symmetricKey.BlockSize = 256;
                  symmetricKey.Mode = CipherMode.CBC;
                  symmetricKey.Padding = PaddingMode.PKCS7;
                  var decryptor = symmetricKey.CreateDecryptor(keyBytes, iv);
  
                  using (var fileStream = new FileStream(PathToFile, FileMode.Open, FileAccess.Read))
                  {
                      using (var cryptoStream = new CryptoStream(fileStream, decryptor, CryptoStreamMode.Read))
                      {
                          var formatter = new BinaryFormatter();
                          var deserialized = (DataSet)formatter.Deserialize(cryptoStream);
                      }
                  }
              }
          }
  
          private static DataSet CreateData()
          {
              var customer = new DataTable("customer");
              var customerId = customer.Columns.Add("Id", typeof(int));
              customer.Columns.Add("Name", typeof(string));
  
              var order = new DataTable("orders");
              order.Columns.Add("Id", typeof(int));
              var fkCustomerId = order.Columns.Add("FkCustomerId", typeof(int));
              order.Columns.Add("Name", typeof(string));
  
              var set = new DataSet();
              set.Tables.Add(customer);
              set.Tables.Add(order);
              set.Relations.Add(new DataRelation("r1", customerId, fkCustomerId));
  
              var r1 = set.Tables["customer"].NewRow();
              r1[0] = 5;
              r1[1] = "Test";
              set.Tables["customer"].Rows.Add(r1);
  
              var r2 = set.Tables["orders"].NewRow();
              r2[0] = 1;
              r2[1] = 5;
              r2[2] = "test";
              set.Tables["orders"].Rows.Add(r2);
              return set;
          }
      }
  }
  </code></pre>
  
  <p>When I run the program the following exception is thrown as soon as it comes to data de-serialization: <strong>System.Runtime.Serialization.SerializationException: 
  'Binary stream 'XX' does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change between serialization and deserialization.'</strong></p>
  
  <p>Right now I am running out of ideas, does anyone see the problem?</p>
  
  <p>Thank you!</p>