我想知道如何在批处理文件中隐藏“set /p”命令的输入。我们都知道,在输入密码时,密码会被显示出来。我该怎么做才能隐藏它呢?我尝试使用这里的conset.exe,并使用以下命令:“conset /PH Password=What is your password?”但是出现了“Conset: Error setting variable”的错误。我还有一个想法,就是改变控制台窗口的颜色。但我该如何在同一行上更改颜色呢?这样你就可以看到问题,但看不到答案了。有没有专业人士有什么好的建议呢?
1.纯批处理解决方案,利用XCOPY
命令及其/P /L
开关,可以在此处找到:这里:
:: Hidden.cmd
::Tom Lavedas, 02/05/2013, 02/20/2013
::Carlos, 02/22/2013
::https://groups.google.com/forum/#!topic/alt.msdos.batch.nt/f7mb_f99lYI
@Echo Off
:HInput
SetLocal EnableExtensions EnableDelayedExpansion
Set "FILE=%Temp%.\T"
Set "FILE=.\T"
Keys List >"%File%"
Set /P "=Hidden text ending with Ctrl-C?: " <Nul
Echo.
Set "HInput="
:HInput_
For /F "tokens=1* delims=?" %%A In (
'"Xcopy /P /L "%FILE%" "%FILE%" 2>Nul"'
) Do (
Set "Text=%%B"
If Defined Text (
Set "Char=!Text:~1,1!"
Set "Intro=1"
For /F delims^=^ eol^= %%Z in ("!Char!") Do Set "Intro=0"
Rem If press Intro
If 1 Equ !Intro! Goto :HInput#
Set "HInput=!HInput!!Char!"
)
)
Goto :HInput_
:HInput#
Echo(!HInput!
Goto :Eof
2.1 基于替换命令的另一种方法
@Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
Set /P "=Enter a Password:" < Nul
Call :PasswordInput
Echo(Your input was:!Line!
Goto :Eof
:PasswordInput
::Author: Carlos Montiers Aguilera
::Last updated: 20150401. Created: 20150401.
::Set in variable Line a input password
For /F skip^=1^ delims^=^ eol^= %%# in (
'"Echo(|Replace.exe "%~f0" . /U /W"') Do Set "CR=%%#"
For /F %%# In (
'"Prompt $H &For %%_ In (_) Do Rem"') Do Set "BS=%%#"
Set "Line="
:_PasswordInput_Kbd
Set "CHR=" & For /F skip^=1^ delims^=^ eol^= %%# in (
'Replace.exe "%~f0" . /U /W') Do Set "CHR=%%#"
If !CHR!==!CR! Echo(&Goto :Eof
If !CHR!==!BS! (If Defined Line (Set /P "=!BS! !BS!" <Nul
Set "Line=!Line:~0,-1!"
)
) Else (Set /P "=*" <Nul
If !CHR!==! (Set "Line=!Line!^!"
) Else Set "Line=!Line!!CHR!"
)
Goto :_PasswordInput_Kbd
2.使用HTA弹出窗口的密码提交器。这是一个混合的.bat/jscript/mshta文件,应该保存为.bat文件:(链接)
<!-- :
:: PasswordSubmitter.bat
@echo off
for /f "tokens=* delims=" %%p in ('mshta.exe "%~f0"') do (
set "pass=%%p"
)
echo your password is %pass%
exit /b
-->
<html>
<head><title>Password submitter</title></head>
<body>
<script language='javascript' >
function pipePass() {
var pass=document.getElementById('pass').value;
var fso= new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1);
close(fso.Write(pass));
}
</script>
<input type='password' name='pass' size='15'></input>
<hr>
<button onclick='pipePass()'>Submit</button>
</body>
</html>
3.自编译的 .net 混合版 。同样应该保存为 .bat
。与其他解决方案不同的是,它将创建/编译一个小的 .exe 文件,该文件将被调用(如果您希望,可以删除它)。还需要安装 .net framework,但这并不是问题:
@if (@X)==(@Y) @end /* JScript comment
@echo off
setlocal enableDelayedExpansion
for /f "tokens=* delims=" %%v in ('dir /b /s /a:-d /o:-n "%SystemRoot%\Microsoft.NET\Framework\*jsc.exe"') do (
set "jsc=%%v"
)
if not exist "%~n0.exe" (
"%jsc%" /nologo /out:"%~n0.exe" "%~dpsfnx0"
)
for /f "tokens=* delims=" %%p in ('"%~n0.exe"') do (
set "pass=%%p"
)
echo your password is !pass!
endlocal & exit /b %errorlevel%
*/
import System;
var pwd = "";
var key;
Console.Error.Write("Enter password: ");
do {
key = Console.ReadKey(true);
if ( (key.KeyChar.ToString().charCodeAt(0)) >= 20 && (key.KeyChar.ToString().charCodeAt(0) <= 126) ) {
pwd=pwd+(key.KeyChar.ToString());
Console.Error.Write("*");
}
if ( key.Key == ConsoleKey.Backspace && pwd.Length > 0 ) {
pwd=pwd.Remove(pwd.Length-1);
Console.Error.Write("\b \b");
}
} while (key.Key != ConsoleKey.Enter);
Console.Error.WriteLine();
Console.WriteLine(pwd);
好的-所以这个答案是9年后才来的,但我想晚点总比没有好...
我写了标记答案中提到的editv32/editv64工具,但自那时以来,我写了一个稍微更好的工具:
https://github.com/Bill-Stewart/editenv
这个工具是免费且开源的。二进制下载可从“Releases”选项卡中获得。你可以在这里查看答案:我能在批处理文件中掩盖输入文本吗?
我认为EditV32.exe和EditV64.exe是你最好的选择(既然你愿意使用conset,而据我所知它不随Windows一起提供,我认为你也不会反对使用editv?)
http://www.westmesatech.com/editv.html
另外:http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/7d2d92a5-5d59-4954-bf3e-22eeb5cf0ee6/
输入隐藏的数字:
@echo off
setlocal enabledelayedexpansion
call:SecretInput "Password: " pw 4
call:SecretInput "Code: " c 5
echo.
echo Password is %pw%
echo Code is %c%
pause&exit
:SecretInput <String> <Var> <Len>
set %2=
set/a len=%3
for /l %%i in (1,1,99) do (
set/a l=%3-len
echo Press d to Delete
set/p .=%~1 <nul
for /l %%j in (1,1,!l!) do (
<nul set/p .=*)
choice /n /c:1234567890d>nul
set e=!errorlevel!&cls
if !e!==11 (
if "!%2!" neq "" (
set %2=!%2:~,-1!
set/a len+=1)
) else (
set/a e%%=10,len-=1
set %2=!%2!!e!)
if !len!==0 exit/b)
这个简单的代码对我有效
:CVSPassword
echo hP1X500P[PZBBBfh#b##fXf-V@`$fPf]f3/f1/5++u5>hide.com
set /P cvspassword=Please enter CVS Password:: <nul
for /f "tokens=*" %%i in ('hide.com') do set cvspassword=%%i
IF "%cvspassword%"=="" GOTO CVSPassword
echo cvspassword %cvspassword%