Eric
Eric

Reputation: 13

base64string to string back to base64string

I am trying an experiment to convert a base64string to a string then back to a base64string, however, I am not getting my original base64string:

String profilepic = "/9j/4AAQ";

string Orig = System.Text.Encoding.Unicode.GetString(Convert.FromBase64String(profilepic));

string New = Convert.ToBase64String(System.Text.Encoding.Unicode.GetBytes(Orig));

The string New returns "/f//4AAQ".

Any thoughts of why this is happening?

Upvotes: 0

Views: 380

Answers (2)

Bhushan Firake
Bhushan Firake

Reputation: 9448

You are doing it wrong. You should do it as below:

namespace ConsoleApplication1
{
    using System;
    using System.Text;

    class Program
    {
        static void Main(string[] args)
        {
            string profilepic = "/9j/4AAQ";
            string New = Convert.ToBase64String(Encoding.Unicode.GetBytes(profilepic));
            byte[] raw = Convert.FromBase64String(New); // unpack the base-64 to a blob
            string s = Encoding.Unicode.GetString(raw); // outputs /9j/4AAQ
            Console.ReadKey();
        }
    }
}

Upvotes: 2

Dave Van den Eynde
Dave Van den Eynde

Reputation: 17405

You're assuming that the base64-encoded binary data in your example contains a UTF-16 encoded message. This may simply not be the case, and the System.Text.Encoding.Unicode class may alter the contents by discarding the bytes that it doesn't understand.

Therefore, the result of base64-encoding the UTF-16 encoded byte stream of the returned string may not yield the same output.

Your input string contains the binary sequence 0xff 0xd8 0xff 0xe0 0x00 0x10 (in hex). Interpreting this as UTF-16LE (which you're using with System.Text.Encoding.Unicode) the first character would be 0xffd8, but is placed in the string as 0xfffd, which explains the change.

I tried decoding it with Encoding.Unicode, Encoding.UTF8 and Encoding.Default, but none of them yielded anything intelligible.

Upvotes: 1

Related Questions