Question: Let's say we have a message that is signed with an HMAC, then that message and the HMAC are encrypted, then that is sent over a TCP socket:

```
// endpoint info excluded
TcpClient client = new TcpClient();
var stream = client.GetStream();

// assume pre-shared keys are used and set at this point
AesManaged aes = new AesManaged();
var aesEncryptor = aes.CreateEncryptor();
CryptoStream aesStream = new CryptoStream(
    stream, aesEncryptor, CryptoStreamMode.Write);

// assume pre-shared keys here too
HMACSHA256 mac = new HMACSHA256();
CryptoStream macStream = new CryptoStream(
    aesStream, mac, CryptoStreamMode.Write);

// assume a message with actual data is written to the macStream
// which updates the hash of the HMAC and also pipes the message
// to the aesStream which encrypts the data and writes it to the
// TCP socket stream
byte[] message = new byte[1024];
macStream.Write(message, 0, message.Length);
macStream.FlushFinalBlock();

// flushing the final block of the macStream actually flushes the
// final block of the aesStream, so I get an error when trying to
// write the HMAC hash to the aesStream
aesStream.Write(mac.Hash, 0, mac.Hash.Length);
aesStream.FlushFinalBlock();

```

I abstracted out a lot of the code, so this isn't a working example. I could probably work around this my writing the data twice, once to HMAC.TransformBlock and again to the aesStream, but I'd like to avoid that. Any ideas?

Answer: 
You really should perform encryption before MAC to create a secure solution, *especially* if you send the data over a socket (as you may be vulnerable to padding oracle attacs). So although your question is valid, it it better to change the order of the streams.
So I put a hack in place that seems to be working for the moment. I created a stream wrapper class called `ProtectedStream`. Here's the gist:

```
public class ProtectedStream : Stream
{
  private Stream stream;

  public ProtectedStream(Stream stream)
  {
    if(stream == null)
      throw new ArgumentNullException("stream");

    this.stream = stream;
  }

  public override void Close()
  {
    this.stream.Close();
    base.Close();
  }

  public override int Read(byte[] buffer, int offset, int count)
  {
    return this.stream.Read(buffer, offset, count);
  }

  public override void Write(byte[] buffer, int offset, int count)
  {
    this.stream.Write(buffer, offset, count);
  }

  // and continue overriding every other overridable Stream member
  // in the same fashion
}

```

And then the original code looks like this:

```
// assume pre-shared keys are used and set at this point
AesManaged aes = new AesManaged();
var aesEncryptor = aes.CreateEncryptor();
CryptoStream aesStream = new CryptoStream(
    stream, aesEncryptor, CryptoStreamMode.Write);

// assume pre-shared keys here too
HMACSHA256 mac = new HMACSHA256();
CryptoStream macStream = new CryptoStream(
    new ProtectedStream(aesStream), mac, CryptoStreamMode.Write);

```

Note that `aesStream` is wrapped inside a `ProtectedStream`. This hides the fact that `aesStream` is a `CryptoStream` from `macStream` so when you call `FlushFinalBlock` on `macStream` it doesn't call `FlushFinalBlock` on `aesStream`.