windows 批量解码base64
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16945780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Decoding base64 in batch
提问by user2064000
I am trying to make an installer using batch. Of course, an installer needs to consist of files that will be installed, so I'm thinking of encoding the files in base64, and simply decode them and write them to their destination.
我正在尝试使用批处理制作安装程序。当然,安装程序需要由将要安装的文件组成,所以我想用 base64 对文件进行编码,然后简单地解码它们并将它们写入目的地。
Of course, my work would be very easy if Windows had something like the base64
tool that Linux boxes contain. However, since it's simply not there, is there any way to decode base64 content completelyusing batch files? And how would I accomplish this?
当然,如果 Windows 有类似base64
Linux 盒子包含的工具,我的工作会很容易。但是,由于它根本不存在,有没有办法使用批处理文件完全解码 base64 内容?我将如何做到这一点?
Any help is appreciated.
任何帮助表示赞赏。
(It's just an experiment, so I'm not worried about inefficiency and the like.)
(这只是一个实验,所以我不担心效率低下之类的。)
回答by dbenham
Actually Windows does have a utility that encodes and decodes base64 - CERTUTIL
实际上 Windows 确实有一个实用程序可以对 base64 进行编码和解码 - CERTUTIL
I'm not sure what version of Windows introduced this command.
我不确定哪个版本的 Windows 引入了这个命令。
To encode a file:
对文件进行编码:
certutil -encode inputFileName encodedOutputFileName
To decode a file:
解码文件:
certutil -decode encodedInputFileName decodedOutputFileName
There are a number of available verbs and options available to CERTUTIL.
CERTUTIL 有许多可用的动词和选项。
To get a list of nearly all available verbs:
要获得几乎所有可用动词的列表:
certutil -?
To get help on a particular verb (-encode for example):
要获得特定动词的帮助(例如 -encode):
certutil -encode -?
To get complete help for nearly all verbs:
要获得几乎所有动词的完整帮助:
certutil -v -?
Mysteriously, the -encodehex
verb is not listed with certutil -?
or certutil -v -?
. But it is described using certutil -encodehex -?
. It is another handy function :-)
神秘的-encodehex
是,动词没有用certutil -?
或列出certutil -v -?
。但它是使用certutil -encodehex -?
. 这是另一个方便的功能:-)
Update
更新
Regarding David Morales' comment, there is a poorly documented type optionto the -encodehex
verb that allows creation of base64 strings without header or footer lines.
关于 David Morales 的评论,该动词有一个记录不佳的类型选项,-encodehex
它允许创建没有页眉或页脚行的 base64 字符串。
certutil [Options] -encodehex inFile outFile [type]
A type of 1 will yield base64 without the header or footer lines.
类型 1 将产生没有页眉或页脚行的 base64。
See https://www.dostips.com/forum/viewtopic.php?f=3&t=8521#p56536for a brief listing of the available type formats. And for a more in depth look at the available formats, see https://www.dostips.com/forum/viewtopic.php?f=3&t=8521#p57918.
有关可用类型格式的简要列表,请参阅https://www.dostips.com/forum/viewtopic.php?f=3&t=8521#p56536。如需更深入地了解可用格式,请参阅https://www.dostips.com/forum/viewtopic.php?f=3&t=8521#p57918。
Not investigated, but the -decodehex
verb also has an optional trailing type argument.
未调查,但-decodehex
动词也有一个可选的尾随类型参数。
回答by BSalita
Here's a batch file, called base64encode.bat, that encodes base64.
这是一个名为 base64encode.bat 的批处理文件,用于对 base64 进行编码。
@echo off
if not "%1" == "" goto :arg1exists
echo usage: base64encode input-file [output-file]
goto :eof
:arg1exists
set base64out=%2
if "%base64out%" == "" set base64out=con
(
set base64tmp=base64.tmp
certutil -encode "%1" %base64tmp% > nul
findstr /v /c:- %base64tmp%
erase %base64tmp%
) > %base64out%