Search code examples
windowsbatch-file

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?


Solution

  • 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>