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.
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.
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%
[Question] Using Base64 to store a file in batch - DosTips.com
Encode to Base64?
cmd - Base64 decoding of a String batch file - Stack Overflow
batch file - Base64 Encode "string" - command-line Windows? - Stack Overflow
How can I encode given text to Base64 in a Batch file? So it asks for input, type what you want and outputs it as Base64.
You can use the CERTUTIL command without installing external software (though it will need temp files):
@echo off
del /q /f "%temp%\b64" >nul 2>nul
del /q /f "%temp%\decoded" >nul 2>nul
set "base64string=YmFzZTY0c3RyaW5n"
echo -----BEGIN CERTIFICATE----->"%temp%\b64"
<nul set /p=%base64string% >>"%temp%\b64"
echo -----END CERTIFICATE----->>"%temp%\b64"
certutil /decode "%temp%\b64" "%temp%\decoded" >nul 2>nul
for /f "useback tokens=* delims=" %%# in ("%temp%\decoded") do set "decoded=%%#"
echo %decoded%
del /q /f "%temp%\b64" >nul 2>nul
del /q /f "%temp%\decoded" >nul 2>nul
You can also use powershell which will be slower despite there will be no temp files:
set "base64string=YmFzZTY0c3RyaW5n"
for /f "tokens=* delims=" %%# in ('powershell [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String("""%base64string%"""^)^)') do set "decoded=%%#"
echo %decoded%
You can try also with atob.bat :
call atob.bat YmFzZTY0c3RyaW5n decoded
echo %decoded%
Or with base64.bat
for /f "tokens=* delims=" %%# in ('base64.bat -decode YmFzZTY0c3RyaW5n') do set "decoded=%%#"
echo %decoded%
I found the solution. I am extracting the base64 encoded text from "config.txt" to a new file say "tmp.txt". now I run the cmd "b64.exe -d tmp.txt tmp2.txt".I get the decoded text in the new "tmp2.txt" file. I delete the "tmp.txt" and "tmp2.txt" once i read the username and password. Thank you
Just use the base64 program from the coreutils package:
echo QWxhZGRpbjpvcGVuIHNlc2FtZQ== | base64 --decode
Or, to include the newline character
echo `echo QWxhZGRpbjpvcGVuIHNlc2FtZQ== | base64 --decode`
output (includes newline):
Aladdin:open sesame
openssl can also encode and decode base64
$ openssl enc -base64 <<< 'Hello, World!'
SGVsbG8sIFdvcmxkIQo=
$ openssl enc -base64 -d <<< SGVsbG8sIFdvcmxkIQo=
Hello, World!
EDIT: An example where the base64 encoded string ends up on multiple lines:
$ openssl enc -base64 <<< 'And if the data is a bit longer, the base64 encoded data will span multiple lines.'
QW5kIGlmIHRoZSBkYXRhIGlzIGEgYml0IGxvbmdlciwgdGhlIGJhc2U2NCBlbmNv
ZGVkIGRhdGEgd2lsbCBzcGFuIG11bHRpcGxlIGxpbmVzLgo=
$ openssl enc -base64 -d << EOF
> QW5kIGlmIHRoZSBkYXRhIGlzIGEgYml0IGxvbmdlciwgdGhlIGJhc2U2NCBlbmNv
> ZGVkIGRhdGEgd2lsbCBzcGFuIG11bHRpcGxlIGxpbmVzLgo=
> EOF
And if the data is a bit longer, the base64 encoded data will span multiple lines.
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>
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.
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
base64doesn'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 thoughecho $d > img$((i++)).b64correctly wrote the encoded data to multiple files. Rather than waiting on working that out, I've just split the increment into its own command.
You can try scrapping the encoded strings of the images using Python. Then check this out for converting the encoded strings to images.
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.
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%