2

I'm trying to decode a javascript base64 string. The encoded string is from a dataUri. It is

<a href="data:application/octet-stream;charset=utf-16le;base64,//5mAG8AbwAgAGIAYQByAAoA">text file</a>

I'm trying to decode "//5mAG8AbwAgAGIAYQByAAoA" which is supposed to give "foo bar" as output. When I click the anchor tag it downloads a file as expected with the same expected string "foo bar" in it.

If I use atob('//5mAG8AbwAgAGIAYQByAAoA') I'm not getting the desired output

var a=atob('//5mAG8AbwAgAGIAYQByAAoA');
console.log(a);

I've even tried to give "utf8" as second arguement and no luck :(

How to extract the original encoded string from this dataUri? Thanks :)

4
  • This might help you en.wikipedia.org/wiki/Data_URI_scheme Commented Aug 1, 2017 at 13:34
  • 1
    btoa('foo bar') gives me "Zm9vIGJhcg==", then atob("Zm9vIGJhcg==") gives me foo bar are you sur about your base64 string ? Commented Aug 1, 2017 at 13:35
  • 1
    I'm trying to decode the base64 string in this dataUri:<a href="data:application/octet-stream;charset=utf-16le;base64,//5mAG8AbwAgAGIAYQByAAoA">text file</a> I believe that 5m... is the correct according to what given in en.wikipedia.org/wiki/Data_URI_scheme Commented Aug 1, 2017 at 13:39
  • 5mAG8AbwAgAGIAYQBy‌ is not a Base64 version of foo bar; it is the Base64 version of æ`ðð (+ SO trimmed some invalid chars); I'd argue that's more of an auth string. Commented Aug 1, 2017 at 15:19

1 Answer 1

1

// isn't valid in the function atob: it will only take valid Base64 strings, which contain numbers,letters, and sometimes a ==. Strip those first and then run it:

> atob('5mAG8AbwAgAGIAYQByAAoA') // console input
< "æ`ðð" // console output; SO stripped some chars

As @Guillaume Badi said in the comments, your Base64 string is not foo bar. To be honest, it looks more like an authentication key/tag, which usually aren't human readable anyways.

Sign up to request clarification or add additional context in comments.

1 Comment

Okay but there must be some way to decode this...I'm getting the correct string in the downloaded file...

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.