private static void EncryptStream(BinaryWriter writer, Action <BinaryWriter> save) { // Create a temporary memory stream to hold the unencrypted data using (MemoryStream ms = new MemoryStream()) { // Write to the memory stream BinaryWriter memoryWriter = new BinaryWriter(ms); save(memoryWriter); memoryWriter.Flush(); // Reset to the start of the memory stream, then copy it encrypted ms.Seek(0, SeekOrigin.Begin); StreamEncryption .EncryptStream(SAVE_KEY, writer, ms); } }