Base64 decoding gives different result

19,183

Solution 1

The issue is that you're encoding in UTF-16 and decoding back to ASCII or UTF8. Change your string encoding to UTF8 before encoding the string to base64 and it should work fine.

Here's the hex dump of that base64 blob:

54 00 65 00 73 00 74 00 69 00 6e 00 67 00 20 00 62 00 61 00 73 00 65 00 36 00 34 00

If you remove the null bytes, you get this:

54 65 73 74 69 6e 67 20 62 61 73 65 36 34

Which translates to the following ASCII text:

Testing base64

Solution 2

The result of decoding base64 is binary data (and likewise the input when encoding it is binary data). To go from binary data to a string or vice versa, you need to apply an encoding such as UTF-8 or UTF-16. You need to find out which encoding Streamserve is using, and use the same encoding when you convert your text data to binary data to start with, before base64-encoding it.

It sounds like you might want to use UTF-16 to encode your text to start with, although in that case I'm surprised you're not just getting garbage out... it looks like it's actually ignoring every other byte in the decoded base64, rather than taking it as the high byte in a UTF-16 code unit.

Share:
19,183
Linora
Author by

Linora

Updated on June 05, 2022

Comments

  • Linora
    Linora almost 2 years

    I'm working on a little Streamserve project (google it :P) where I get some Base64 encoded content. I've tried to decode the base64 string with multiple decoders and all return the correct result.. except the Base64DecodeString method in Streamserve.

    The encoded string is: 'VABlAHMAdABpAG4AZwAgAGIAYQBzAGUANgA0AA==' The expected result is: 'Testing base64'

    However within Streamserve the result is: 'Tsig ae4'

    It simply skips every other letter. Now I know most people dont know Streamserve, but I have a hunch that this might be a character encoding problem.. problem and was hoping someone has a clue what might be happening here.

    I can without any problem encode/decode strings within streamserve.. just not strings I get as input

  • Linora
    Linora over 12 years
    Hi there.. Ive tried what you said. In java I did this: Base64.encodeBase64String(content.getBytes("UTF-8")); But it did not change anything :(
  • Linora
    Linora over 12 years
    seems like there is no difference.. I get the same results no matter what charset I choose. Also I've noticed that the newline's in the generated file dont work in notepad. I also really need to get that working..
  • Polynomial
    Polynomial over 12 years
    Odd. I suppose you could just use a loop to produce a new array using all the non-zero bytes...
  • Polynomial
    Polynomial over 12 years
    The newline issue in Notepad is just because you're using Unix-style \n newlines rather than the Windows-style \r\n.
  • Linora
    Linora over 12 years
    I got it working.. turns out I can set which charset I want to use when decoding. Setting it to the same when encoding works (who knew ;P) anyhoo.. thanks for the help! :)