批量制作密码

Car*_*479 1 batch-file

我做了一个批量游戏,用户可以登录/注册.但如果站在附近的人可以偷看密码,那么密码就没有意义了.普通密码字段用星号(*)掩盖字符.

如何在批处理文件中屏蔽字符?

我之前在cmd上看过这个,但我不知道怎么做.

jeb*_*jeb 13

您可以使用XCOPY隐藏输入,它可以处理几乎所有字符,您还可以实现退格逻辑.

@echo off
setlocal EnableDelayedExpansion
call :input
echo(
echo '!input!'
if "!input!"=="password" echo OK
exit /b

:input
for /F "tokens=1 delims=# " %%a in ('"prompt #$H# & echo on & for %%b in (1) do rem"') do (
  set "\b=%%a"
)

set "input="
:keyLoop
call :GetKey
if not defined key exit /b
if "!key!"=="!\b!" (
    if defined input (
        set "input=!input:~0,-1!"
        <nul set /p ".=!\b! !\b!"
    )
) ELSE (
    <nul set /p ".=*"
    set "input=!input!!key!"
)
goto :keyLoop


:GetKey
setlocal DisableDelayedExpansion
set "key="
for /F "usebackq delims=" %%L in (`xcopy /L /w "%~f0" "%~f0" 2^>NUL`) do (
  if not defined key set "key=%%L"
)
(
endlocal
set "key=^%key:~-1%" !
exit /b
)
Run Code Online (Sandbox Code Playgroud)

这段代码应该能够处理所有字符,比如^!&%<>.
也可以使用退格键删除最后输入的字符.

这条线set "key=^%key:~-1%" !看起来很奇怪,但它用于在延迟扩展上下文中转义!字符set "key=^!" !.
并且为了避免所有其他字符的问题,最后!删除了插入符号,就像set "key=^A" !将被评估为"set"键= A"