title: C# Error - Invalid character in a Base-64 string
body: <p>I am writing a C# assembly to take advantage of the GZip functionality in newer versions of the .NET framework, and I am copying code I have used successfully elsewhere.</p>
  
  <p>The way this works is that an application we are using feeds a stream to the assembly, the assembly reads it, compresses it, then returns the compressed result as a string.  That string is then put back into the assembly through a stream to uncompress it (normally it will be stored, but I am simply running a basic test).</p>
  
  <p>However, when I feed the string back to the assembly, it errors out while reading it from a byte array that is read from the application's stream.  Here is the code:</p>
  
  <pre><code>    private void ReadStream(IStream stream, out byte[] data)
      {
          using (MemoryStream writer = new MemoryStream())
          {
              IntPtr rwBytes = Marshal.AllocHGlobal(4);
              int _rwBytes = 0;
              data = new byte[0xafc8];
  
              do
              {
                  stream.Read(data, 0xafc8, rwBytes);
                  _rwBytes = Marshal.ReadInt32(rwBytes);
                  writer.Write(data, 0, _rwBytes);
              }
              while (_rwBytes &gt; 0);
              writer.Close();
  
              Marshal.FreeHGlobal(rwBytes);
          }
      }
  
      public string CompressString([In, MarshalAs(UnmanagedType.AsAny)] object appStream)
      {
          byte[] buffer = null;
          ReadStream(appStream as IStream, out buffer);
  
          MemoryStream ms = new MemoryStream();
          using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
          {
              zip.Write(buffer, 0, buffer.Length);
          }
  
          ms.Position = 0;
          MemoryStream outStream = new MemoryStream();
  
          byte[] compressed = new byte[ms.Length];
          ms.Read(compressed, 0, compressed.Length);
  
          byte[] gzBuffer = new byte[compressed.Length + 4];
          System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
          System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
          string str = Convert.ToBase64String(gzBuffer);
  
          gzBuffer = Convert.FromBase64String(str);
          return str;
      }
  
      public string DecompressString([In, MarshalAs(UnmanagedType.AsAny)] object appStream)
      {
          byte[] buffer = null;
          ReadStream(appStream as IStream, out buffer);
  
          string compressedText = Encoding.UTF8.GetString(buffer); 
  
          byte[] gzBuffer = Convert.FromBase64String(compressedText);
  
          using (MemoryStream ms = new MemoryStream())
          {
              int msgLength = BitConverter.ToInt32(gzBuffer, 0);
              ms.Write(gzBuffer, 4, gzBuffer.Length - 4);
  
              buffer = new byte[msgLength];
  
              ms.Position = 0;
              using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
              {
                  zip.Read(buffer, 0, buffer.Length);
              }
          }
  
          return Encoding.UTF8.GetString(buffer);
      }
  </code></pre>
  
  <p>The string is returned from the CompressString function as:</p>
  
  <pre><code>yK8AAB+LCAAAAAAABADtvQdgHEmWJSYvbcp7f0r1StfgdKEIgGATJNiQQBDswYjN5pLsHWlHIymrKoHKZVZlXWYWQMztnbz33nvvvffee++997o7nU4n99//P1xmZAFs9s5K2smeIYCqyB8/fnwfPyK+ndd5WjRplk6rxarOm6aolmmbN+04TZ/n7cdN2uTU4jwt2vSqqt8241/jR8+Pnh89P3p+9Pzo+dHzo+dHz4+eHz0/en70/Oj50fOj50fPj54fPT96fvT86PnR86PnR8+Pnh89P3p+9Pzo+dHz/67nR9j86PnR86PnR8+Pnh89P3p+9Pzo+dHzo+dHz4+eHz0/en70/Oj50fOj50fPj54fPT96fvT86PnR86PnR8+Pnh89/99/fjSCHz0/en70/Oj50fOj50fPj54fPT96fvT86PnR86PnR8+Pnh89P3p+9Pzo+dHzo+dHz4+eHz0/en70/Oj50fOj5//7z49G8KPnR8+Pnh89P3p+9Pzo+dHzo+dHz4+eHz0/en70/Oj50fOj50fPj54fPT96fvT86PnR86PnR8+Pnh89P3p+9Px///nRCH70/Oj50fOj50fPj54fPT96fvT86PnR86PnR8+Pnh89P3p+9Pzo+dHzo+dHz4+eHz0/en70/Oj50fOj50fPj57/7z8/GsGPnh89P3p+9Pzo+dHzo+dHz4+eHz0/en70/Oj50fP/nef/AYk/UbjIrwAA
  </code></pre>
  
  <p>When it is read back from the stream in the Encoding.UTF8.GetString call, it is:</p>
  
  <pre><code>yK8AAB+LCAAAAAAABADtvQdgHEmWJSYvbcp7f0r1StfgdKEIgGATJNiQQBDswYjN5pLsHWlHIymrKoHKZVZlXWYWQMztnbz33nvvvffee++997o7nU4n99//P1xmZAFs9s5K2smeIYCqyB8/fnwfPyK+ndd5WjRplk6rxarOm6aolmmbN+04TZ/n7cdN2uTU4jwt2vSqqt8241/jR8+Pnh89P3p+9Pzo+dHzo+dHz4+eHz0/en70/Oj50fOj50fPj54fPT96fvT86PnR86PnR8+Pnh89P3p+9Pzo+dHz/67nR9j86PnR86PnR8+Pnh89P3p+9Pzo+dHzo+dHz4+eHz0/en70/Oj50fOj50fPj54fPT96fvT86PnR86PnR8+Pnh89/99/fjSCHz0/en70/Oj50fOj50fPj54fPT96fvT86PnR86PnR8+Pnh89P3p+9Pzo+dHzo+dHz4+eHz0/en70/Oj50fOj5//7z49G8KPnR8+Pnh89P3p+9Pzo+dHzo+dHz4+eHz0/en70/Oj50fOj50fPj54fPT96fvT86PnR86PnR8+Pnh89P3p+9Px///nRCH70/Oj50fOj50fPj54fPT96fvT86PnR86PnR8+Pnh89P3p+9Pzo+dHzo+dHz4+eHz0/en70/Oj50fOj50fPj57/7z8/GsGPnh89P3p+9Pzo+dHzo+dHz4+eHz0/en70/Oj50fP/nef/AYk/UbjIrwAA
  </code></pre>
  
  <p>These strings are identical.  I can even convert it back and forth in the CompressString function with no problem.  Any thoughts out there?  This is very strange.</p>