【问题标题】:Base64 decoding of a String batch file字符串批处理文件的Base64解码
【发布时间】:2023-03-28 18:58:01
【问题描述】:

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

【问题讨论】:

  • 如果您向我们展示您尝试过的方法,我们可能会提供帮助。如果您只需要解决方案,请先查看 google。

标签: batch-file cmd base64 decode encode


【解决方案1】:

您可以在不安装外部软件的情况下使用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% 

【讨论】:

  • 很好的答案,但 powershell 让我发笑。它似乎总是比类似的 linux 命令复杂和难记 5 倍。 echo "string" | base64
  • 如果其他人想知道,certutil 应该安装在 Windows 上的所有版本上,从 Vista / Server 2008 开始。social.technet.microsoft.com/Forums/en-US/…
【解决方案2】:

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

【讨论】:

  • 什么是b64.exe?感觉不是 Windows 原生的。
【解决方案3】:

您可以使用 @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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多