Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Base64 decoding of a String batch file

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 file whereas in my case input is a string. plz help me out. All this shud be done in a batch file only and also am reading the encoded text from a config.txt file.

like image 407
Zayn malik Avatar asked Jan 04 '23 08:01

Zayn malik


1 Answers

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% 
like image 193
npocmaka Avatar answered Jan 13 '23 10:01

npocmaka