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

4

我希望能够解码一个base64编码的文本并用它来登录网站。这个base64编码的文本是用户名和密码。我已经找到了类似Base64.exe和b64.exe这样的工具,但它们的输入是文件,而在我的情况下输入是字符串。请帮忙解决。所有这些都应该在批处理文件中完成,并且我正在从config.txt文件中读取编码后的文本。


3
如果你展示给我们你所尝试的内容,我们或许能够帮助你。如果你只是需要一个解决方案,先在谷歌上寻找一下。 - The_Cthulhu_Kid
3个回答

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

2
不错的答案,但PowerShell让我笑了。 它总是比可比较的Linux命令复杂五倍,并且更难记住。 echo "string" | base64 - Elliott B
如果有其他人想知道,certutil 应该安装在所有版本的 Windows 上,从 Vista / Server 2008 开始。https://social.technet.microsoft.com/Forums/en-US/09fb7a70-69c2-4cc0-ad82-173c06774261/availbility-of-certutil-on-different-windows-versions?forum=winserversecurity - BuvinJ

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

1
b64.exe是什么?感觉不像是Windows原生的。 - ScriptKidd

0

你可以使用由@jeb@dbenham@Ed Dyreen发明的宏样式

@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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接