解码base64:输入无效

时间:2013-03-19 02:54:59

标签: linux shell base64

尝试在GNU / Linux上解码base64文件,我得到“base64:无效输入”。

$ 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

我尝试了dos2unix命令,但没有帮助。

我的base64版本:

$ 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.

4 个答案:

答案 0 :(得分:52)

该版本不会解码(默认情况下)带分隔符的行,但编码器默认情况下会这样做。 (较新版本没有此问题。)

一个解决方案:

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

替代:

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

-i选项代表(来自man页面):

-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)

答案 1 :(得分:1)

如果在Mac上执行此操作,则您的base64版本可能没有灵活性来处理忽略垃圾的问题。如果您冲煮安装coreutils,您将拥有gbase64实用程序并按照Joe的描述使用它。

答案 2 :(得分:0)

或更简单地

base64 -di foo.zip > foo2.zip

答案 3 :(得分:0)

您也可以尝试使用

echo -n

抑制新行并将输入长度填充为4的倍数,并使用1-3个相等的字符

=
相关问题