0

I am working on simple batch script to hide the password inputted by user. The solution is to create a popup and change text-color for that popup. Following is the code and it works properly

@echo off
Echo Please enter your password in the popup window and then press enter

set tempbat="%temp%\p.cmd"

REM Create temporary batch file to make popup window for entering password 'masked'
echo mode 20,1 >%tempbat%
echo color 01 >>%tempbat%
echo Title Enter Password >>%tempbat%
echo setlocal enabledelayedexpansion >>%tempbat%
echo set /p Pass= >>%tempbat%
echo echo !pass!^>"%temp%\pass.txt" >>%tempbat%
echo exit >>%tempbat%

start /wait "" %tempbat%
del %tempbat% 2>NUL

set /p Pwd=<"%temp%\pass.txt"
del "%temp%\pass.txt" 2>NUL

echo %Pwd%

My only concern is that when the popup occurs, can I set it always on top of main cmd window, and even disable access to main cmd window (I expect the behavior like Bootstrap Modal)?

Thank for reading and hope to receive helps from you

Liverpool
  • 131
  • 7
  • 12
  • Using a popup for cowering password can be useless but if this is what you want you can use CMDOW. http://www.commandline.co.uk/cmdow/ – John DOE Oct 28 '14 at 15:51
  • Thank for your suggestion. But my work require the pure batch scripts only. I don't have permission to user third-party scripts – Liverpool Oct 29 '14 at 02:40

1 Answers1

1

Instead of trying to cover the password with something, it is possible to obfuscate user input in batch. See MC ND's code from this answer:

@echo off
setlocal enableextensions disabledelayedexpansion

rem Call the subroutine to get the password    
call :getPassword password 

rem Echo what the function returns
if defined password (
    echo You have typed [%password%]
) else (
    echo You have typed nothing
)

rem End of the process    
endlocal
exit /b


rem Subroutine to get the password
:getPassword returnVar
setlocal enableextensions disabledelayedexpansion
set "_password="

rem We need a backspace to handle character removal
for /f %%a in ('"prompt;$H&for %%b in (0) do rem"') do set "BS=%%a"

rem Prompt the user 
set /p "=password ?:" <nul 

:keyLoop
rem retrieve a keypress
set "key="
for /f "delims=" %%a in ('xcopy /l /w "%~f0" "%~f0" 2^>nul') do if not defined key set "key=%%a"
set "key=%key:~-1%"

rem handle the keypress 
rem     if No keypress (enter), then exit
rem     if backspace, remove character from password and console
rem     else add character to password and go ask for next one
if defined key (
    if "%key%"=="%BS%" (
        if defined _password (
            set "_password=%_password:~0,-1%"
            setlocal enabledelayedexpansion & set /p "=!BS! !BS!"<nul & endlocal
        )
    ) else (
        set "_password=%_password%%key%"
        set /p "=*"<nul
    )
    goto :keyLoop
)
echo(
rem return password to caller
if defined _password ( set "exitCode=0" ) else ( set "exitCode=1" )
endlocal & set "%~1=%_password%" & exit /b %exitCode%
Community
  • 1
  • 1
SomethingDark
  • 13,229
  • 5
  • 50
  • 55
  • I just found out that it is not working if my password includes some special character such as ^ " ' (). Could you help ? – Liverpool Oct 30 '14 at 06:02