Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"illegal base64 data" when trying to base64 decode

Tags:

base64

go

I tried to decode a valid (based on my understanding) base64 encoded string in Go with:

data, err := base64.StdEncoding.DecodeString(s)
if err != nil {
     ...
}

A full example is here. I have a string "eyJlbWFpbF9hZGRyZXNzIjoiIiwiZXhwIjoxNDQ3NzIzMzY4LCJmaXJzdG5hbWUiOiIiLCJpYXQiOjE0NDc0NjQxNjgsImlzcyI6Imh0dHA6Ly91ZGFjaXR5LmNvbSIsImtpZCI6ImE3ZTg5ZWQyMSIsImxhc3RuYW1lIjoiIiwidXNlcl9pZCI6IjEyMzQ1Njc4IiwidXNlcm5hbWUiOiJoYW5zb2xvQGhvdGguY29tIn0", which can be properly decoded for example here or even in your browser's console with atob(that_string);, but for some reason go complains with:

illegal base64 data at input byte 236

Notice, that I can decode some other strings. So why can not I base64decode a valid encoded string in Go?

like image 942
Salvador Dali Avatar asked Jan 25 '26 10:01

Salvador Dali


1 Answers

Your input does not have any padding. Therefore, you should use base64.RawStdEncoding over base64.StdEncoding:

data, err := base64.RawStdEncoding.DecodeString(s)

Example: https://play.golang.org/p/ZWfzYXQ5Ye


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!