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.

Answer from dbenham on Stack Overflow
Top answer
1 of 2
193

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.

2 of 2
6

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%
🌐
Base64Encode.org
base64encode.org › enc › batch
Base64 Encoding of "batch" - Online
Encode batch to Base64 format with various advanced options. Our site has an easy to use online tool to convert your data.
Discussions

[Question] Using Base64 to store a file in batch - DosTips.com
I also use base64 encoding to store data files in batch scripts. I use certutil to encode data, then paste the resulting encoded data removing the -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- lines. To extract the data, certutil may be used again (it is more simple), but I use this code (more complex, as I am). This script decode ... More on dostips.com
🌐 dostips.com
August 19, 2016
Encode to Base64?
You have to store the text in a file, but then you can use certutil to encode it in base64. certutil -encode inputfile outputfile More on reddit.com
🌐 r/Batch
12
1
May 27, 2020
cmd - Base64 decoding of a String batch file - Stack Overflow
I want to decode a base64 encoded text and use it to login to a website. The base64 encoded text is the username and password. I hav found tools like Base64.exe and b64.exe but they take input as a... More on stackoverflow.com
🌐 stackoverflow.com
batch file - Base64 Encode "string" - command-line Windows? - Stack Overflow
I have found numerous ways to base64 encode whole files using the command-line on Windows, but I can't seem to find a simple way to batch encode just a "string" using a command-line utility. How does one do this, for use in a batch file for example? ... Ever heard of CertUtil.exe, a native tool of Windows with many useful verbs? for instance, CertUtil -encode and CertUtil -decode ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Rob van der Woude
robvanderwoude.com › battech_base64.php
Batch files - How To ... Base64 Encode and Decode Files
July 1, 2023 - B64 by Bob Trower is a third party open source implementation of a base64 encoder/decoder.
🌐
DosTips.com
dostips.com › forum › viewtopic.php
[Question] Using Base64 to store a file in batch - DosTips.com
August 19, 2016 - @echo off for /f %%i in ("certutil.exe") do if not exist "%%~$path:i" ( echo CertUtil.exe not found. pause exit /b ) certutil.exe -f -encode "%~1" "temp.b64~" || (pause&exit /b) >"decode_%~n1_b64.bat" ( echo(@echo off echo(for /f %%%%i in ("certutil.exe"^) do if not exist "%%%%~$path:i" ( echo( echo CertUtil.exe not found. echo( pause echo( exit /b echo(^) echo(^>"temp.b64~" ( for /f "usebackq delims=" %%i in ("temp.b64~") do echo(echo(%%i echo(^) echo(certutil.exe -f -decode "temp.b64~" "%~nx1" echo(del "temp.b64~" echo(pause ) del "temp.b64~" Either drag/drop a file onto this "encode_b64.bat" or you pass the file name as argument when you call it. It automatically creates the batch file to decode and restore your passed file. Regards aGerman ... Another way to decode base64 using cscript hybrid.
🌐
GitHub
github.com › npocmaka › batch.scripts › blob › master › hybrids › jscript › base64.bat
batch.scripts/hybrids/jscript/base64.bat at master · npocmaka/batch.scripts
WScript.Echo(WScript.ScriptName + " decodes or encodes base64 strings"); WScript.Echo(""); WScript.Echo("Usage:"); WScript.Echo(""); WScript.Echo(WScript.ScriptName + "{-decode|-encode} input [-eval {yes|no}]"); WScript.Echo(" -eval - evaluate the special characters input"); WScript.Echo(""); WScript.Echo("Examples:"); WScript.Echo(""); WScript.Echo(WScript.ScriptName + " -encode \"\\u0022Hello\\u0022\" -eval yes " ); WScript.Echo(WScript.ScriptName + " -decode \"SGVsbG8=\""); } ·
Author   npocmaka
Find elsewhere
🌐
Igor's Blog
igorkromin.net › index.php › 2017 › 04 › 26 › base64-encode-or-decode-on-the-command-line-without-installing-extra-tools-on-linux-windows-or-macos
Base64 Encode or Decode on the command line without installing extra tools on Linux, Windows or macOS | Igor Kromin
April 26, 2017 - Given you're already dealing with Base64 data I am going to assume you know how to bring that up on your operating system. Scroll down to the relevant section based on your OS below, also substitute your file names as appropriate. I am going to use .txt for the decoded data file extension and .b64 for the Base64 encoded file extension.
Top answer
1 of 11
71

Here's a PowerShell one-liner you can run from a cmd console that'll Base64 encode a string.

powershell "[convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes(\"Hello world!\"))"

It's probably not as fast as npocmaka's solution, but you could set a console macro with it.

doskey btoa=powershell "[convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes(\"$*\"))"
doskey atob=powershell "[Text.Encoding]::UTF8.GetString([convert]::FromBase64String(\"$*\"))"

btoa Hello world!
btoa This is fun.
btoa wheeeeee!
atob SGVsbG8gd29ybGQh

Be advised that doskey doesn't work in batch scripts -- only the console. If you want do use this in a batch script, make a function.

@echo off
setlocal

call :btoa b64[0] "Hello world!"
call :btoa b64[1] "This is fun."
call :btoa b64[2] "wheeeeee!"
call :atob b64[3] SGVsbG8gd29ybGQh

set b64
goto :EOF

:btoa <var_to_set> <str>
for /f "delims=" %%I in (
    'powershell "[convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes(\"%~2\"))"'
) do set "%~1=%%I"
goto :EOF

:atob <var_to_set> <str>
for /f "delims=" %%I in (
    'powershell "[Text.Encoding]::UTF8.GetString([convert]::FromBase64String(\"%~2\"))"'
) do set "%~1=%%I"
goto :EOF

Or if you'd prefer a batch + JScript hybrid:

@if (@CodeSection==@Batch) @then
@echo off & setlocal

call :btoa b64[0] "Hello world!"
call :btoa b64[1] "This is fun."
call :btoa b64[2] "wheeeeee!"
call :atob b64[3] SGVsbG8gd29ybGQh

set b64
goto :EOF

:btoa <var_to_set> <str>
:atob <var_to_set> <str>
for /f "delims=" %%I in ('cscript /nologo /e:JScript "%~f0" %0 "%~2"') do set "%~1=%%I"
goto :EOF

@end // end batch / begin JScript hybrid code
var htmlfile = WSH.CreateObject('htmlfile');
htmlfile.write('<meta http-equiv="x-ua-compatible" content="IE=10" />');
WSH.Echo(htmlfile.parentWindowWSH.Arguments(0).substr(1)));

Edit: batch + VBScript hybrid for @Hackoo:

<!-- : batch portion
@echo off & setlocal

call :btoa b64[0] "Hello world!"
call :btoa b64[1] "This is fun."
call :btoa b64[2] "wheeeeee!"
call :atob b64[3] SGVsbG8gd29ybGQh

set b64
goto :EOF

:btoa <var_to_set> <str>
:atob <var_to_set> <str>
for /f "delims=" %%I in ('cscript /nologo "%~f0?.wsf" %0 "%~2"') do set "%~1=%%I"
goto :EOF

: VBScript -->
<job>
    <script language="VBScript">
        Set htmlfile = WSH.CreateObject("htmlfile")
        htmlfile.write("<meta http-equiv='x-ua-compatible' content='IE=10' />")
        if WSH.Arguments(0) = ":btoa" then
            WScript.Echo htmlfile.parentWindow.btoa(WSH.Arguments(1))
        else
            WScript.Echo htmlfile.parentWindow.atob(WSH.Arguments(1))
        end if
    </script>
</job>
2 of 11
64

According to the comments on the question, you can use certutil. e.g.,

certutil -encode raw.txt encoded.txt

or

certutil -f -encode raw.txt encoded.txt

The -f means "force overwrite". Otherwise you will get an error if the output file (encoded.txt above) already exists.

However, this will format the output into the encoded.txt file as if it were a certificate PEM file, complete with BEGIN and END lines, and split lines at the character max. So you would need to do further processing in a batch scenario, and a bit of extra work if the strings are long at all.

🌐
Base64 Decode
base64decode.org
Base64 Decode and Encode - Online
Decode from Base64 format or encode into it with various advanced options. Our site has an easy to use online tool to convert your data.
🌐
GitHub
gist.github.com › gialloporpora › 3105675
Batch script to obtain the encoded base64 url of a file · GitHub
July 13, 2012 - Batch script to obtain the encoded base64 url of a file · Raw · base64.bat · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
GitHub
gist.github.com › t-mat › f09152d13f58b31305b68f1ba45a7820
[Windows][Batch-file]base64 decoder · GitHub
[Windows][Batch-file]base64 decoder. GitHub Gist: instantly share code, notes, and snippets.
🌐
Herongyang
herongyang.com › Encoding › Base64-Tool-Windows-Command.html
Windows Command - "certutil -encode/-decode"
But you can use the built-in command "certutil -encode/-decode" to indirectly perform Base64 encoding and decoding.
Top answer
1 of 3
1

In general, HTML can't be parsed properly with regular expressions, but if you have a specific limited format then it could work.

Given a simple format like

<body>
<img src="data:image/jpeg;base64,DpFDPGOIg3renreGR43LGLJKds==">
<img src="data:image/jpeg;base64,DpFDPGOIg3renreGR43LGLJKds=="><img src="data:image/jpeg;base64,DpFaPGOIg3renreGR43LGLJKds==">
<div><img src="data:image/jpeg;base64,DpFdPGOIg3renreGR43LGLJKds=="></div>
</body>

the following can pull out the data

i=0; awk 'BEGIN{RS="<"} /="data:image\/jpeg;base64,[^\"]*"/ { match($0, /="data:image\/jpeg;base64,([^\"]*)"/, data); print data[1]; }' test.html | while read d; do echo $d  | base64 -d > $i.jpg; i=$(($i+1)); done

To break that down:

i=0 Keep a counter so we can output different filenames for each image.

awk 'BEGIN{RS="<"} Run awk with the Record Separator changed from the default newline to <, so we always treat each HTML element as a separate record.

/="data:image\/jpeg;base64,[^\"]*"/ Only run the following commands on records that have embedded base64 jpeg data.

{ match($0, /="data:image\/jpeg;base64,([^\"]*)"/, data); print data[1]; }' Pull out the data itself, the part matched with parentheses between the comma and the trailing quotation mark, then print it.

test.html Just the input filename.

| while read d; do Pipe the output base64 data to a loop. read will put each line into d until there's no more input.

echo $d | base64 -d > img$i.jpg; Pass the current image through the base64 decoder and store the output to a file.

i=$(($i+1)); Increment to change the next filename.

done Done.

There are a few things that could probably be done better here:

  • There should be a way to get the line-match regexp to capture the base64 data directly, instead of repeating the regexp in a call to the match() function, but I couldn't get it to work.
  • I don't like the technique of reading a pipe into the variable d, only to echo it back out to another pipe - it would be nicer to just pipe straight through - but base64 doesn't know to only use one line of the input.
  • For some reason I have not yet figured out, incrementing the counter directly where it's used (i.e. echo $d | base64 -d > img$((i++)).jpg) only wrote to the first file, even though echo $d > img$((i++)).b64 correctly wrote the encoded data to multiple files. Rather than waiting on working that out, I've just split the increment into its own command.
2 of 3
0

You can try scrapping the encoded strings of the images using Python. Then check this out for converting the encoded strings to images.

🌐
Tutorial Reference
tutorialreference.com › batch-scripting › examples › faq › batch-script-how-to-base64-decode-a-string
How to Base64 Decode a String in Batch Script | Tutorial Reference
You will often encounter it in API responses, configuration files, or data streams where a password, a key, or even a small file needs to be safely embedded in a text format. While batch scripting has no native function to decode Base64, it can control a powerful, built-in Windows utility, ...
🌐
YouTube
youtube.com › how to fix your computer
Decoding base64 in batch - YouTube
Decoding base64 in batch [ Gift : Animated Search Engine : https://www.hows.tech/p/recommended.html ] Decoding base64 in batch Note: The information provide...
Published   November 24, 2021
Views   64
Top answer
1 of 2
188

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.

2 of 2
21

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%