Base64 decode without padding

2,450 views
Skip to first unread message

seki...@googlemail.com

unread,
Jul 30, 2011, 10:44:29 PM7/30/11
to golan...@googlegroups.com
When i use with a specific decoded string

func (*Encoding) Decode

it does give an "illegal base64 data at input byte 344" error.
When i add a padding char to the end of the String it works.
Why is the function no able to do this without them, when there is an private decode function with the comment "decode also assumes len(src)%4==0, 
since it is meant for internal use".

Am i doing something wrong?

David Symonds

unread,
Jul 30, 2011, 10:58:32 PM7/30/11
to golan...@googlegroups.com
base64 is specified with padding. If you have a string with the
padding stripped then it's trivial to add it back on.

// enc is your string
if m := len(encoded) % 4; m != 0 {
encoded += strings.Repeat("=", 4-m)
}


Dave.

seki...@googlemail.com

unread,
Jul 31, 2011, 4:19:10 AM7/31/11
to golan...@googlegroups.com
Thanks for the answer, i just thought i missed something.

The standard is with padding, but not in base64 for URL (base64.URLEncoding). It would be confusing not to add this.

zbin...@gmail.com

unread,
Sep 24, 2015, 9:30:13 AM9/24/15
to golang-nuts, seki...@googlemail.com
you can try this free online service to convert string to base64 without padding online(http://www.online-code.net/base64-string.html).

jark...@gmail.com

unread,
Sep 25, 2015, 12:50:44 PM9/25/15
to golang-nuts, seki...@googlemail.com
Use the Raw{Std,URL}Encoding, then you don't need to add in the padding.
Reply all
Reply to author
Forward
0 new messages