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.
the usual windows way to generate a base 64 string is
Make any file (here simple text but can be PDF, docX or whatever.)
echo Hello World!>input.ext
Convert to base64
certutil -encodehex -f "input.ext" "output.txt" 0x40000001 1>nul
to see the result use
type output.txt
SGVsbG8gV29ybGQhDQo=
To send to another command use a pipe to use the TYPE output.txt as input. However like many conversion tasks it needs to be a file IO. To avoid any hint of a hard drive file, it would need a memory file system.
For looping a folder use a for loop with variables to suit
to decode output.txt
certutil -decode -f output.txt output.ext 1>nul
type output.ext
Hello World!
Answer
There is a very small (7.5 kB) downloadable exe c 2015 that can be used to encode/decode to file or console. You can try to download direct in a curl command like this but it must be 7680 bytes.
>curl -O https://master.dl.sourceforge.net/project/base64/b64.exe?viasf=1
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 7680 100 7680 0 0 12070 0 --:--:-- --:--:-- --:--:-- 12132
>b64.exe
b64 (Base64 Encode/Decode) Bob Trower 2001/08/03
(C) Copr Bob Trower 1986-2015 Version 0.94R
Usage: b64 -option [-l<num>] [<FileIn> [<FileOut>]]
Purpose: This program is a simple utility that implements
Base64 Content-Transfer-Encoding (RFC1113).
Use -h option for additional help.
>
Thus use is very simple:
b64 -e input.ext
Result on screen or use in a pipe
SGVsbG8gV29ybGQhDQo=
Note that's exactly the same as above however there has been no update as to most recent query about why it may not always work inbound as expected, but this is an outbound usage.
example of open issue
echo Hello World!|b64 -e
not an accurate result compared to above but an acceptable troughput
SGVsbG8gV29ybGQhCg==
so when using
echo SGVsbG8gV29ybGQhCg==|b64 -d
It does correctly report
Hello World!
The reason for the difference is the last EOL
0000000C: 0A 0D
And depending on circumstances, may or may not be a material difference. Also note that command lines are limited in working length, so console usage is also limited for string handling too
I suggest to install OpenSSL on your Windows machine. After you set the PATH variable it's easy as:
type <file_name> | openssl base64