解码base64:无效输入

问题描述 投票:0回答:5

尝试在 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.
linux shell base64
5个回答
119
投票

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

一种解决方案:

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)

9
投票

或者更简单

base64 -di foo.zip > foo2.zip


4
投票

如果您在 Mac 上执行此操作,您的

base64
版本可能无法灵活地处理忽略垃圾。如果您安装
coreutils
,您将拥有
gbase64
实用程序并使用它 正如 Joe 所描述的那样


3
投票

您也可以尝试使用

echo -n

抑制新行并用一到三个相等的字符将输入长度填充到 4 的倍数

=

0
投票

对我来说,我将Windows上的base64输出从Linux控制台复制到一个文件,将其粘贴到Windows(Sublime Text)中的文件中,然后保存。

base64 -d
base64: invalid input
投诉。 在调查输入文件后,我发现文件有 Windows 行结尾、CRLF,并且原始的 base64 数据是在 Linux 中构建的,带有 Unix 行结尾。 所以我重新打开source-base64-files,将行结尾更改为Unix格式,并保存文件,解码成功,没有任何问题。

© www.soinside.com 2019 - 2024. All rights reserved.