title: How Can I Edit Bytes As They Pass Through A Stream?
body: <p>My goal is to have a file stream open up a user-chosen file, then, it should stream the files bytes through in chunks (buffers) of about 4mb (this can be changed it's just for fun). As the bytes travel (in chunks) through the stream, I'd like to have a looping if-statement see if the bytes value is contained in an array I have declared elsewhere. (The code below will build a random array for replacing bytes), and the replacement loop could just say something like the bottom for-loop. As you can see I'm fairly fluent in this language but for some reason the editing and rewriting of chunks as they are read from a file to a new one is eluding me. Thanks in advance!</p>
  
  <pre><code>    private void button2_Click(object sender, EventArgs e)
      {
          GenNewKey();
  
          const int chunkSize = 4096; // read the file by chunks of 4KB
          using (var file = File.OpenRead(textBox1.Text))
          {
              int bytesRead;
              var buffer = new byte[chunkSize];
              while ((bytesRead = file.Read(buffer, 0, buffer.Length)) &gt; 0)
              {
                  byte[] newbytes = buffer;
                  int index = 0;
                  foreach (byte b in buffer)
                  {
                      for (int x = 0; x &lt; 256; x++)
                      {
                          if (buffer[index] == Convert.ToByte(lst[x]))
                          {
                              try
                              {
                                  newbytes[index] = Convert.ToByte(lst[256 - x]);
                              }
                              catch (System.Exception ex)
                              {
                                  //just to show why the error was thrown, but not really helpful..
                                  MessageBox.Show(index + ", " + newbytes.Count().ToString());
                              }
                          }
                      }
                      index++;
                  }
                  AppendAllBytes(textBox1.Text + ".ENC", newbytes);
              }
          }
      }
  
      private void GenNewKey()
      {
          Random rnd = new Random();
  
          while (lst.Count &lt; 256)
          {
              int x = rnd.Next(0, 255);
              if (!lst.Contains(x))
              {
                  lst.Add(x);
              }
          }
  
          foreach (int x in lst)
          {
              textBox2.Text += ", " + x.ToString();
              //just for me to see what was generated
          }
      }
  
      public static void AppendAllBytes(string path, byte[] bytes)
      {
          if (!File.Exists(path + ".ENC"))
          {
              File.Create(path + ".ENC");
          }
          using (var stream = new FileStream(path, FileMode.Append))
          {
              stream.Write(bytes, 0, bytes.Length);
          }
      }
  </code></pre>
  
  <p>Where textbox1 holds the path and name of file to encrypt, textBox2 holds the generated cipher for personal debugging purposes, button two is the encrypt button, and of course I am using System.IO.</p>