0

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?

2

2 Answers 2

2

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();
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

Thank you for the insight. I'll remember to keep this in mind next time I'm messing with the different encoding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.