95

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.

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 completely using batch files? And how would I accomplish this?

Any help is appreciated.

(It's just an experiment, so I'm not worried about inefficiency and the like.)

2
  • 1
    Also check this out: f2ko.de/en/b64.php Aug 31, 2016 at 7:17
  • This comment was my favorite answer, that program is much more like a Linux experience.
    – neokyle
    Dec 10, 2018 at 5:43

2 Answers 2

183

Actually Windows does have a utility that encodes and decodes base64 - CERTUTIL

I'm not sure what version of Windows introduced this command.

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.

To get a list of nearly all available verbs:

certutil -?

To get help on a particular verb (-encode for example):

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

Update

Regarding David Morales' comment, there is a poorly documented type option to the -encodehex verb that allows creation of base64 strings without header or footer lines.

certutil [Options] -encodehex inFile outFile [type]

A type of 1 will yield base64 without the header or footer lines.

See https://www.dostips.com/forum/viewtopic.php?f=3&t=8521#p56536 for 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.

Not investigated, but the -decodehex verb also has an optional trailing type argument.

16
  • 8
    Certutil has been around since at least Windows Server 2003. Jun 5, 2013 at 18:00
  • 38
    I would have given some good odds that "base64 in batch" was the most desperate thing I ever entered in a search engine Aug 16, 2013 at 3:36
  • 4
    running certutil -encode inputFileName encodedOutputFileName generates a base64 string enclosed by "-----BEGIN CERTIFICATE-----" and "-----END CERTIFICATE-----" so you can't directly decode the file after it is produced. Jan 21, 2016 at 1:19
  • 2
    @DavidMorales - Not true! Did you try it? The -DECODE command works just fine with the header and footer in place. I'm not sure about the exact rules, but CERTUTIL -DECODE is very forgiving about the format of the encoded source.
    – dbenham
    Jan 21, 2016 at 1:26
  • 4
    @AndrzejMartyna - it's not that it's broken, but there is a maximum input file size of 74472684 bytes. Oct 3, 2016 at 16:37
5

Here's a batch file, called base64encode.bat, that encodes 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%
2
  • 4
    This script will work better if you add setlocal enabledelayedexpansion and use !base64tmp! inside the (...). Otherwise the value is determined when the (...) sequence is read in, not when it is executed. Nov 18, 2018 at 3:45
  • 2
    a much simpler solution, without having to deal with the headers: stackoverflow.com/a/60404255/12861751
    – ScriptKidd
    Apr 11, 2020 at 4:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.