Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decoding Base64 in C# sometimes gives incorrect result with one extra padding character

Tags:

c#

.net

base64

I stumbled upon a strange behavior of Convert.FromBase64String in .NET 4.7.2. Normally it would throw an exception when the padding is not correct. But I found a case where adding another padding character produces an incorrect result instead of an exception.

var correct = Convert.FromBase64String("YWE=");

In this case correct is [97, 97] or "aa" in a string form. But when I add another =:

var incorrect = Convert.FromBase64String("YWE==");

instead of getting an exception I get one byte less and incorrect is [88] or "X" is a string form.

Weird. Is this a bug and it should be reported? Or is it a known/documented behavior? I couldn't find any references to this.

Compare to Ruby. This evaluates to "aa":

Base64.strict_decode64 "YWE="

And this raises an exception:

Base64.strict_decode64 "YWE=="

ArgumentError: invalid base64
from /usr/local/Cellar/ruby/2.6.1/lib/ruby/2.6.0/base64.rb:74:in `unpack1'
like image 208
detunized Avatar asked Feb 24 '19 13:02

detunized


People also ask

How do I decode a base64 string?

To decode with base64 you need to use the --decode flag. With encoded string, you can pipe an echo command into base64 as you did to encode it. Using the example encoding shown above, let's decode it back into its original form. Provided your encoding was not corrupted the output should be your original string.

Can we decode base64?

You can DECODE the base64 values into bytes (so just a sequence of bits). And from there, you need to know what these bytes represent and what original encoding they were represented in, if you wish to convert them again to a legible format.

What is base64 decoding?

Base64 is an encoding and decoding technique used to convert binary data to an American Standard for Information Interchange (ASCII) text format, and vice versa.

What is CG == in base64?

Cg== is the base64 encode of the new line character in the latest position. So if you want to encode ABC you will get QUJD , however if you include a "return character" after ABC you will get QUJDCg== . Follow this answer to receive notifications. answered Jan 13, 2020 at 17:06.


1 Answers

Looks like this was a bug: https://github.com/dotnet/corefx/issues/30793 It's fixed in .NET Core but still present in .NET Framework up to and including 4.8.

This code should abort with exception instead of printing 1:

using System;

public class Program
{
    public static void Main()
    {
        Console.WriteLine(Convert.FromBase64String("YWE==").Length);
    }
}

Here's a test: https://dotnetfiddle.net/x2X9CT

like image 127
detunized Avatar answered Sep 22 '22 05:09

detunized