That Homeless Guy
That Homeless Guy

Reputation: 149

c# Convert.To/FromBase64String confusion

Assuming I have this Method.

private static void Example(string data)
    {
        Console.WriteLine("Initial : {0}", data);

        data = data.PadRight(data.Length + 1, '0'); 
        Console.WriteLine("Step 1 : {0}", data);

        data = data.PadRight(data.Length + 4 - data.Length % 4, '=');
        Console.WriteLine("Step 2 : {0}", data);

        byte[] byteArray = Convert.FromBase64String(data);
        string newData = Convert.ToBase64String(byteArray);
            Console.WriteLine("Step 3 : {0}", newData);
    }

I expect the output given the input string "1" to be as follows

Initial : 1
Step 1 : 10
Step 2 : 10==
Step 3 : 10==

Instead the output is this.

Initial : 1
Step 1 : 10
Step 2 : 10==
Step 3 : 1w==

And I have no idea why. I would expect the output to be the same as the input but it isn't. I have tried replacing

data = data.PadRight(data.Length + 1, '0'); 

with

data = data + "0";

It appears with longer input strings too, for example strings with a length of 5 or 9. It works fine if I add "=" but then I exceed my padding limit with Convert.FromBase64String()

So my question is really what is going on and how can I get my expected output,? What am I doing wrong?

Edit: For those confused as to why I'm using bas64 it is related to this PHP decrypting data with RSA Private Key

Upvotes: 0

Views: 1232

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500665

Basically, there's no byte array which would be encoded to 10==.

If a base64 string ends with ==, that means that the final 4 characters only represent a single byte. So only the first character and the first 2 bits of the second character are relevant. Looking at the Wikipedia table, 10 means values of:

'1' = 53  '0' = 52
110101    110100

So that's encoding a byte of 1101 0111, and then the final four bits (0100) are ignored. When you re-encode the data, it's using 0s for the final four bits instead, giving:

'1' = 53  'w' = 48
110101    110000

Fundamentally, it's not clear what you're trying to do - but if your input is part of a base64-encoded value, that's pretty odd. The code is behaving the way I'd expect it to - it's just not useful code...

Upvotes: 3

Related Questions