title: Accessing a decrypted Rijndael encrypted file with a StreamReader without writing to disk
body: <p>I'm currently working on encryption/decryption of plain text files that are embedded in a .dll file. The plain text files are scripts, and are being parsed as and when they're needed.</p>
  
  <p>At the minute, this is how the (plain text) script files are being loaded :</p>
  
  <pre><code>string RunLocation = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
  Assembly _assembly = Assembly.LoadFrom(RunLocation + "\\&lt;name of dll&gt;.dll");
  s = new StreamReader(_assembly.GetManifestResourceStream(File));
  RunScript(s);
  </code></pre>
  
  <p>The RunScript method reads a single line of the script into a string, then parses it. It looks through each line in turn, and performs each of the actions we define in the script as they are found.</p>
  
  <p>Since out dll files aren't encrypted in anyway, we've decided that we should encrypt each of the script files before they are embedded into the dll. We decided on the Rijndael algorithm because of the ease of which it can be implemented in C#/.NET.</p>
  
  <p>Currently encryption runs like this:</p>
  
  <pre><code>FileStream fileStreamForEncryption = new FileStream (inputFileToBeEncrypted, FileMode.Create);
  RijndaelManaged rijndaelCryptography = new RijndaelManaged();
  CryptoStream encryptionStream = new CryptoStream(fileStreamForEncryption, rijndaelCryptography .CreateEncryptor(key, iv), CryptoStreamMode.Write);
  FileStream fsIn = new FileStream(inputFile, FileMode.Open);
  int data;
  while ((data = fsIn.ReadByte()) != -1)
    cs.WriteByte((byte)data);
  fsIn.Close();
  cs.Close();
  fileStreamForEncryption .Close()
  </code></pre>
  
  <p>This means that decryption is a matter of running a few commands and getting the decrypted file... except that, because of the design of the rest of the code (encryption seems to have been a "once we've got the system running to a given standard"), the file needs to be returned as a StreamReader object. At the minute, this is how I'm decrypting a requested file:</p>
  
  <pre><code>string decodedFile = null;
  FileStream fileToDecrypt= new FileStream(_assembly.GetManifestResourceStream(File).ToString(), FileMode.Open);
  /* I know that the above line is overly complicated, it's because our files are read into StreamReader objects (as I've explained above) */
  RijndaelManaged rijndaelCryptography = new RijndaelManaged();
  try
  {
    CryptoStream cs = new CryptoStream(fileToDecrypt, rijndaelCryptography .CreateDecryptor(key, iv), CryptoStreamMode.Read);
    using (StreamReader decryptReader = new StreamReader(cs))
    {
      decodedFile = decryptReader.ReadToEnd();
    }
  catch (Exception e)
  {
  MessageBox.Show(e.Message);
  }
  </code></pre>
  
  <p>Does anyone know how I can decrypt a file, using Rijndael, but have the output accessible as a / read into an instance of a StreamReader object?</p>
  
  <p>I've had a long look around The Internet for ways to do this and the closest I came was copying the contents of decryptReader into another StreamReader object instead of decrypting out to a string, but it seems that you can't copy the contents of one StreamReader to another StreamReader - correct me if I'm wrong.</p>
  
  <p>I'd love to have to change as little of the existing (that which I haven't shared here) code base as possible as that could cause bugs to arise. I'd also love to do this without writing the file back to disk (there's a reason we're encrypting them, obviously).</p>
  
  <p>Thank you in advance, and sorry if I haven't been clear enough or a little vague. I can add more information if you need it to help me.</p>
  
  <p>Jamie</p>