6

I was wondering how I would go about Hiding the input from a 'set /p' command in a batch file.

set /p Password=What is your password?

We all know that inputting your password, you would be able to see it. How would I go 'bout hiding it ?

I tried conset.exe from here. And used:

conset /PH Password=What is your password?

And i get "Conset: Error setting variable" :(

Another idea I had, was to change the colour of the console window. But how could I change the colour on the same line? So that you could see the question, but not see the answer?

Any ideas from the pros?

James
  • 63
  • 1
  • 1
  • 3
  • Possible duplicate of http://stackoverflow.com/questions/286871/what-would-be-the-windows-batch-equivalent-for-htmls-input-type-password – nobody Jun 24 '14 at 21:03
  • Possible duplicate of [Can I mask an input text in a bat file](http://stackoverflow.com/questions/664957/can-i-mask-an-input-text-in-a-bat-file) – Mofi Mar 29 '16 at 18:25

5 Answers5

11

1.Pure batch solution that (ab)uses XCOPY command and its /P /L switches found here :

:: 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 Another way based on replace command

@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.Password submitter that uses a HTA pop-up . This is a hybrid .bat/jscript/mshta file and should be saved as a .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.A self-compiled .net hybrid .Again should be saved as .bat .In difference with other solutions it will create/compile a small .exe file that will be called (if you wish you can delete it). Also requires installed .net framework but that's rather not a problem:

@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);
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • Nice answer, but xcopy.exe is not a "pure batch" solution. There is no pure batch solution as far as I know. – Eryk Sun Jun 18 '17 at 18:32
1

OK - so this answer comes 9 years later, but I guess better late than never...

I wrote the editv32/editv64 tool noted in the marked answer, but since then I wrote a slightly better tool:

https://github.com/Bill-Stewart/editenv

The tool is free and open-source. Binary download is available from the Releases tab.

The --maskinput (-m) option lets you mask input (optionally with a user-defined character). Note that this feature is NOT SECURE (the variable value is still in memory and the environment in plain-text) -- it's for convenience only.

My most frequent use of this tool is to interactively edit the Path variable for the current process (it's turned out to be pretty handy for that purpose).

Bill_Stewart
  • 22,916
  • 4
  • 51
  • 62
0

The simple code worked for me

: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%
Vinayak Dornala
  • 1,609
  • 1
  • 21
  • 27
0

You can probably look at the answers here: Can I mask an input text in a bat file

I think EditV32.exe and EditV64.exe are the best options you have (and since you were OK to use conset, which as far as I know does not come with Windows, I don't think you would oppose to using editv?)

http://www.westmesatech.com/editv.html

Also: http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/7d2d92a5-5d59-4954-bf3e-22eeb5cf0ee6/

Community
  • 1
  • 1
manojlds
  • 290,304
  • 63
  • 469
  • 417
  • Note that my `editv32`/`edit64` utilities have been superseded by my open-source [`editenv`](https://github.com/Bill-Stewart/editenv) utility. – Bill_Stewart Jul 16 '20 at 16:50
0

To enter hidden numbers:

@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)
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Jeremy Caney Sep 08 '22 at 00:49