Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Base64 Encode "string" - command-line Windows?

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?

like image 838
Taapo Avatar asked May 05 '16 09:05

Taapo


People also ask

How do you Base64 encode and decode from the command line on 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 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.

Can PowerShell decode Base64?

You can easily write and execute scripts while using this program in Windows 10. Now, when you have developed a sufficient understanding of the Windows 10 PowerShell program, it is good to get started with the Base64 encoding and decoding in PowerShell.


1 Answers

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.parentWindow[WSH.Arguments(0).substr(1)](WSH.Arguments(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> 
like image 112
rojo Avatar answered Oct 11 '22 17:10

rojo