Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

decode base64: invalid input

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. 
like image 987
andyf Avatar asked Mar 19 '13 02:03

andyf


People also ask

What characters are invalid in Base64?

The base 64 digits in ascending order from zero are the uppercase characters 'A' to 'Z', lowercase characters 'a' to 'z', numerals '0' to '9', and the symbols '+' and '/'. % is not allowed in base64 encoding. Save this answer.

How do I decrypt Base64?

To decode a file with contents that are base64 encoded, you simply provide the path of the file with the --decode flag. As with encoding files, the output will be a very long string of the original file. You may want to output stdout directly to a file.

Can Base64 encoding be decoded?

In JavaScript there are two functions respectively for decoding and encoding Base64 strings: btoa() : creates a Base64-encoded ASCII string from a "string" of binary data ("btoa" should be read as "binary to ASCII"). atob() : decodes a Base64-encoded string ("atob" should be read as "ASCII to binary").

What does == mean in Base64?

When decoding Base64 text, four characters are typically converted back to three bytes. The only exceptions are when padding characters exist. A single = indicates that the four characters will decode to only two bytes, while == indicates that the four characters will decode to only a single byte.


2 Answers

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) 
like image 84
Joe Avatar answered Sep 22 '22 22:09

Joe


Or even more simply

base64 -di foo.zip > foo2.zip

like image 35
Shawn Lillemo Avatar answered Sep 26 '22 22:09

Shawn Lillemo