Bug 18524 - Base64 encoding bug
Summary: Base64 encoding bug
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: marcos.henrich
Depends on:
Reported: 2014-03-21 16:39 UTC by Craig
Modified: 2014-09-02 12:04 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Craig 2014-03-21 16:39:21 UTC
The code below produces an incorrect result for variables s and b.  The results do not match what is produced in standard .NET.

  byte[] b = Convert.FromBase64String("UBo3FDjs8DabAbDhVwzqqQ==");
  string s = Encoding.UTF8.GetString(b) + "WQ3cgGvwGI10jSZoQQTTGsdk3nFH2xTVUvOXIV1JVM0=";
  b = Encoding.UTF8.GetBytes(s);
  s = Convert.ToBase64String(b);
  b = Convert.FromBase64String(s);

Expected Result:
  s = UBo3FDjvv73vv70277+9Ae+/ve+/vVcM77+9V1EzY2dHdndHSTEwalNab1FRVFRHc2RrM25GSDJ4VFZVdk9YSVYxSlZNMD0=
  b = 71

Actual Result:
  s = UBo3FDjvv73vv70277+9Ae+/ve+/vVcM77+977+9V1EzY2dHdndHSTEwalNab1FRVFRHc2RrM25GSDJ4VFZVdk9YSVYxSlZNMD0=
  b = 74
Comment 1 Craig 2014-03-21 16:43:59 UTC
The above code may actually be a problem with UTF8 Encoding rather than Base64 encoding.  Regardless, there seems to be a problem.
Comment 2 Craig 2014-03-21 17:09:21 UTC
This may be part of my problem, although I still wonder why it produces a different result than .NET:

"Not every byte sequence represents an Unicode string encoded in UTF-8 or UTF-16.

Not every Unicode string represents a byte sequence encoded in Base64."
Comment 3 marcos.henrich 2014-09-02 11:40:03 UTC
Hi Craig,

Thank you for the detailed bug report.

The pull request for this issue can be found in the link below.
Comment 4 marcos.henrich 2014-09-02 12:04:07 UTC
Fixed in master b5fcd926f8ee870e107cf534c1f9592586b8a0e9.