字符串批处理文件的 Base64 解码

Base64 decoding of a String batch file

我想解码 base64 编码的文本并用它来登录网站。 base64 编码的文本是用户名和密码。我找到了像 Base64.exe 和 b64.exe 这样的工具,但它们将输入作为文件,而在我的例子中,输入是一个字符串。请帮帮我。所有这些都应该只在批处理文件中完成,并且我也在从 config.txt 文件中读取编码文本。

您可以在不安装外部软件的情况下使用 CERTUTIL 命令(尽管它需要临时文件):

@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

您也可以使用 powershell,尽管没有临时文件,但速度会更慢:

set "base64string=YmFzZTY0c3RyaW5n"
for /f "tokens=* delims=" %%# in ('powershell [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String("""%base64string%"""^)^)') do set "decoded=%%#"
echo %decoded%

你也可以尝试 atob.bat :

call atob.bat YmFzZTY0c3RyaW5n decoded
echo %decoded%

base64.bat

for /f "tokens=* delims=" %%# in ('base64.bat -decode YmFzZTY0c3RyaW5n') do set "decoded=%%#"
echo %decoded% 

我找到了解决方案。我正在将 base64 编码的文本从 "config.txt" 中提取到一个新文件中,比如 "tmp.txt"。现在我 运行 命令 "b64.exe -d tmp.txt tmp2.txt"。我在新的 "tmp2.txt" 文件中得到了解码文本。读取用户名和密码后,我删除了 "tmp.txt" 和 "tmp2.txt"。 谢谢

您可以使用 @jeb@dbenham@Ed Dyreen 发明的 macro style :

@echo off
====SETLOCAL DisableDelayedExpansion EnableExtensions


set ^"LF=^
%===EXPANDS TO NOTHING===%
"
REM \n is an escaped LF + caret
set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"


REM Initalize macro
set ^"$b64d=FOR %%$ in (%%$ MainMacro) do if "%%$" == "MainMacro" (%\n%
    ^>nul %__APPDIR__%CERTUTIL.EXE -f -decodehex args.tmp proc.tmp 1%\n%
    type proc.tmp%\n%
    del args.tmp proc.tmp%\n%
) ELSE ^>args.tmp echo("


echo BEFORE :: %time%
%$b64d%dGhpcwppcwpzaWNrIQo=
echo AFTER  :: %time%

输出:

BEFORE :: 21:13:31.94
this
is
sick!
AFTER  :: 21:13:32.02