Page 1 of 1

Make a password field

Posted: 11 Jul 2010 20:09
by phillid
Hey. I'm making a program that requires a password. I've got basic encryption and stuff, but when the user types in the password, it appears on-screen. How can I make it so that it writes the key presses to a variable but puts an * for each letter in the variable?

Thanks for any suggestions!

phillid

Re: Make a password field

Posted: 12 Jul 2010 11:22
by aGerman
There is no way to make a password field in batch. I suggest to write a temporary .hta file to have a separate window for password input.

batch file:

Code: Select all

@ECHO OFF &SETLOCAL &TITLE Password

FOR /F "DELIMS=: TOKENS=2" %%i IN ('CHCP') DO SET /A oemcp=%%~ni
:start
FINDSTR /B /C:"    " "%~0">%TEMP%\PassIn.hta
START /W %TEMP%\PassIn.hta
CHCP 1252>NUL
SET /P x=<%TEMP%\tmp.dll
CHCP %oemcp%>NUL
IF NOT DEFINED x GOTO :start
DEL %TEMP%\tmp.dll
DEL %TEMP%\PassIn.hta

ECHO %x%

PAUSE
GOTO :EOF

:: for PassIn.hta
    <html>
     <head>
      <title>Password-Input</title>
      <hta:application scroll="no" windowState="normal">
     </head>
     <script language="vbscript">
      Sub Window_onLoad()
        Me.ResizeTo 300,200
        Me.MoveTo ((Screen.Width / 2) - 150),((Screen.Height / 2) - 100)
      End Sub
      Sub KeyEnter()
        If Window.Event.KeyCode = 13 Then
          OK.Click
        End If
      End Sub
      Sub SaveDLL()
        pw = Passwort.Value
        pw = Replace(pw, "^", "^^")
        pw = Replace(pw, "<", "^<")
        pw = Replace(pw, ">", "^>")
        pw = Replace(pw, "|", "^|")
        pw = Replace(pw, "&", "^&")
        Set fs = CreateObject("Scripting.FileSystemObject")
        strFile = fs.GetAbsolutePathName(fs.BuildPath(fs.GetSpecialFolder(2), "tmp.dll"))
        Set ts = fs.OpenTextFile(strFile, 2, True)
        ts.WriteLine pw
        ts.Close
        Set fs = Nothing
        Set ts = Nothing
      End Sub
     </script>
     <body style="font:14 pt arial; color:white;
      filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr='#000000', EndColorStr='#0000FF')"
      onKeyPress="KeyEnter">
      <table width="80%" height="100%" align="center" border="0">
       <tr height="20%"><td>
       </td></tr>
       <tr height="10%"><td align="center">
        <b><u>Password:</u></b>
       </td></tr>
       <tr height="20%"><td align="center">
        <input type="password" name="Passwort" size="16">
       </td></tr>
       <tr height="30%"><td align="center">
        <input type="button" value="OK" name="OK" onClick="SaveDLL:Window.Close">
       </td></tr>
       <tr height="20%"><td>
       </td></tr>
      </table>
      <script language=vbscript>
       Passwort.Focus
      </script>
     </body>
    </html>


But note that password comparisons within batch files (and any other plain text scripts) are senseless. You can find the password with any text editor or you can change the code that no password is needed.

Regards
aGerman