Error while decoding base64 string

Hi, I am using elixir version 1.8.2

while running in iex shell Base.decode64(“eyJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLCJhenAiOiI4NDgwMzc1NTA1Mi0zYjFpNXV2b2JubnRzOWw5MzJwaGdiMmE4dWRmaHRrbi5hcHBzLmdvb2dsZXVzZXJjb250ZW50LmNvbSIsImF1ZCI6Ijg0ODAzNzU1MDUyLTNiMWk1dXZvYm5udHM5bDkzMnBoZ2IyYTh1ZGZodGtuLmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwic3ViIjoiMTA4MDQ0Mjc2NDQ2NzkyNDUwODk0IiwiaGQiOiJhdmlhYmlyZC5jb20iLCJlbWFpbCI6ImFuc2h1bEBhdmlhYmlyZC5jb20iLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZSwiYXRfaGFzaCI6ImlabDJ0S2JZZngwNjVtVGpQcEowT0EiLCJpYXQiOjE1NTkxNTQyNDQsImV4cCI6MTU1OTE1Nzg0NH0”)

This function return error, while it is successfully encoded on online platform like https://www.base64decode.org/

I can confirm that it does not work for me to:

$ iex
Erlang/OTP 21 [erts-10.3.5.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]

Interactive Elixir (1.8.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> Base.decode64("eyJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLCJhenAiOiI4NDgwMzc1NTA1Mi0zYjFpNXV2b2JubnRzOWw5MzJwaGdiMmE4dWRmaHRrbi5hcHBzLmdvb2dsZXVzZXJjb250ZW50LmNvbSIsImF1ZCI6Ijg0ODAzNzU1MDUyLTNiMWk1dXZvYm5udHM5bDkzMnBoZ2IyYTh1ZGZodGtuLmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwic3ViIjoiMTA4MDQ0Mjc2NDQ2NzkyNDUwODk0IiwiaGQiOiJhdmlhYmlyZC5jb20iLCJlbWFpbCI6ImFuc2h1bEBhdmlhYmlyZC5jb20iLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZSwiYXRfaGFzaCI6ImlabDJ0S2JZZngwNjVtVGpQcEowT0EiLCJpYXQiOjE1NTkxNTQyNDQsImV4cCI6MTU1OTE1Nzg0NH0")
:error

and I can also confirm that works on https://www.base64decode.org/

It shouldn’t work there either, it’s an invalid base64 encoded string, it’s the wrong length. It looks like it is missing a = at the end, so it should be:

Base.decode64("eyJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLCJhenAiOiI4NDgwMzc1NTA1Mi0zYjFpNXV2b2JubnRzOWw5MzJwaGdiMmE4dWRmaHRrbi5hcHBzLmdvb2dsZXVzZXJjb250ZW50LmNvbSIsImF1ZCI6Ijg0ODAzNzU1MDUyLTNiMWk1dXZvYm5udHM5bDkzMnBoZ2IyYTh1ZGZodGtuLmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwic3ViIjoiMTA4MDQ0Mjc2NDQ2NzkyNDUwODk0IiwiaGQiOiJhdmlhYmlyZC5jb20iLCJlbWFpbCI6ImFuc2h1bEBhdmlhYmlyZC5jb20iLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZSwiYXRfaGFzaCI6ImlabDJ0S2JZZngwNjVtVGpQcEowT0EiLCJpYXQiOjE1NTkxNTQyNDQsImV4cCI6MTU1OTE1Nzg0NH0=")

So that website is probably just padding invalid base64 strings.

For note, you can add padding: false to Elixir’s Base.decode64/2 call to do the same automatically.

8 Likes

Yes, you are correct on this affirmation… I can decode it if I add the = in the end.

iex(8)> Base.decode64!("eyJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLCJhenAiOiI4NDgwMzc1NTA1Mi0zYjFpNXV2b2JubnRzOWw5MzJwaGdiMmE4dWRmaHRrbi5hcHBzLmdvb2dsZXVzZXJjb250ZW50LmNvbSIsImF1ZCI6Ijg0ODAzNzU1MDUyLTNiMWk1dXZvYm5udHM5bDkzMnBoZ2IyYTh1ZGZodGtuLmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwic3ViIjoiMTA4MDQ0Mjc2NDQ2NzkyNDUwODk0IiwiaGQiOiJhdmlhYmlyZC5jb20iLCJlbWFpbCI6ImFuc2h1bEBhdmlhYmlyZC5jb20iLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZSwiYXRfaGFzaCI6ImlabDJ0S2JZZngwNjVtVGpQcEowT0EiLCJpYXQiOjE1NTkxNTQyNDQsImV4cCI6MTU1OTE1Nzg0NH0", padding: false)
"{\"iss\":\"https://accounts.google.com\",\"azp\":\"84803755052-3b1i5uvobnnts9l932phgb2a8udfhtkn.apps.googleusercontent.com\",\"aud\":\"84803755052-3b1i5uvobnnts9l932phgb2a8udfhtkn.apps.googleusercontent.com\",\"sub\":\"108044276446792450894\",\"hd\":\"aviabird.com\",\"email\":\"anshul@aviabird.com\",\"email_verified\":true,\"at_hash\":\"iZl2tKbYfx065mTjPpJ0OA\",\"iat\":1559154244,\"exp\":1559157844}"
iex(9)>

Thank you. This is working fine.

1 Like