Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decoding base64 in batch

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

like image 775
user2064000 Avatar asked Jun 05 '13 17:06

user2064000


People also ask

How do I decode a Base64 string in Windows?

If you are using a Windows system, there is no built-in command to directly perform Base64 encoding and decoding. But you can use the built-in command "certutil -encode/-decode" to indirectly perform Base64 encoding and decoding.

How do I decode a Base64 string?

To decode with base64 you need to use the --decode flag. With encoded string, you can pipe an echo command into base64 as you did to encode it. Using the example encoding shown above, let's decode it back into its original form. Provided your encoding was not corrupted the output should be your original string.

Can PowerShell decode Base64?

In this regard, we shared with you the PowerShell commands that help in performing the Base64 encoding and decoding of the desired data. By making use of these commands, you can easily encode and decode any given strings with the Base64 coding in PowerShell in Windows 10.

How do I install Base64 on Windows?

To install, just copy the file base64.exe to a folder on your Windows PATH, for example C:\Windows . You may need administrator permissions to do this. We recommend you set up a C:\Bin directory for files like this.


2 Answers

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.

like image 188
dbenham Avatar answered Sep 22 '22 16:09

dbenham


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% 
like image 21
BSalita Avatar answered Sep 19 '22 16:09

BSalita