69

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.
Alois Mahdal
  • 10,763
  • 7
  • 51
  • 69
andyf
  • 3,262
  • 3
  • 23
  • 37
  • On my system at least, it's `-D` to decode, not `-d`. What does `base64 --help` say on your system? – John Zwinck Mar 19 '13 at 02:58
  • 1
    Consider upgrading; the current version is at least 8.20 (from 2012) and probably more recent still. – Jonathan Leffler Mar 19 '13 at 03:03
  • That version is what CentOS 5.x installs, so depending on who controls the environment he may not have the option. – Joe Mar 19 '13 at 04:07
  • Yes, it's an old version linux. Red Hat Enterprise Linux Server release 5.3 (Tikanga) released at 2009/1/20 – andyf Mar 19 '13 at 05:38

5 Answers5

117

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)
Felix
  • 433
  • 4
  • 8
Joe
  • 41,484
  • 20
  • 104
  • 125
9

Or even more simply

base64 -di foo.zip > foo2.zip

Shawn Lillemo
  • 99
  • 1
  • 2
  • 13
    Thanks Shawn, and welcome to StackOverflow. Your answer is nice and concise! To enhance it, you might consider explaining the `-di` options in a few words. – Jeremy Dec 20 '19 at 02:19
4

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.

Micah Elliott
  • 9,600
  • 5
  • 51
  • 54
  • is there another way than gbase64 for macOS? I am looking for one that works both on macos and linux. My linux command is currently ` echo ${ANDROID_KEYSTORE_TESTING} | base64 -di > file.keystore` – Dimitri Kopriwa Oct 31 '22 at 17:16
  • Sometimes you'll find the g* names for coreutils on a linux system too (pretty sure I've seen it before). Depends on if it was compiled with `--program-prefix=g`, which maybe you have the ability to do on yours. More details: https://unix.stackexchange.com/a/478055/101165 – Micah Elliott Nov 04 '22 at 20:09
2

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

=
0

For me, I copied the base64-output on Windows from the Linux console to a file, pasted it into a file in Windows (Sublime Text), and saved it. base64 -d complained on it with base64: invalid input. After investigating the input file, I found file had Windows line endings, CRLF, and original base64 data was built in Linux with Unix line endings. So I reopened source-base64-files, changed line endings to Unix format, and saved files, and decoding was successful without any issues.

denixx
  • 1
  • 3