decode base64: invalid input

LinuxShellBase64

Linux Problem Overview


Trying to decode base64 file on GNU/Linux, I get "base64: invalid input".

$ base64 test.zip | base64 -d > test2.zip
base64: invalid input
$ ll test*
-rw-r--r-- 1 user grp 152 19 11:41 test.zip
-rw-r--r-- 1 user grp  57 19 11:42 test2.zip

I tried dos2unix command, but it did not help.

My base64 version:

$ base64 --version
base64 (GNU coreutils) 5.97
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software.  You may redistribute copies of it under the terms of
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.

Written by Simon Josefsson.

Linux Solutions


Solution 1 - Linux

That version will not decode (by default) lines with separators, yet the encoder does that by default. (Newer versions don't have this problem.)

One solution:

base64 -w 0 foo.zip | base64 -d > foo2.zip

Alternate:

base64 foo.zip | base64 -di > foo2.zip

The -i option stands for (from the man page):

-i, --ignore-garbage
       When decoding, ignore non-alphabet characters.
[...]
Decoding require compliant input by default, use --ignore-garbage to
attempt to recover from non-alphabet characters (such as newlines)

Solution 2 - Linux

Or even more simply

base64 -di foo.zip > foo2.zip

Solution 3 - Linux

If you're doing this on a mac, your version of base64 might not have the flexibility to handle ignoring garbage. If you brew install coreutils, you'll have the gbase64 utility and use it as Joe has described.

Solution 4 - Linux

You can also try using

echo -n

to suppress new lines and padding the input length to a multiple of 4 with one to three equal characters

=

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionandyfView Question on Stackoverflow
Solution 1 - LinuxJoeView Answer on Stackoverflow
Solution 2 - LinuxShawn LillemoView Answer on Stackoverflow
Solution 3 - LinuxMicah ElliottView Answer on Stackoverflow
Solution 4 - LinuxTim MilsteadView Answer on Stackoverflow