标题: [原创]在批处理输入密码显示星号(利用choice命令)
[打印本页]
作者: ljxinfeixiang
时间: 2008-3-24 17:02
标题: [原创]在批处理输入密码显示星号(利用choice命令)
我在网吧工作,做了很多批处理。其中有些让用户登陆的,但输入密码的时候都是明文显示。能不能像其他应用程序那样用星号屏蔽输入的密码呢?很早就有这样的想法了,但一直摸不到头绪。在网上看到几个也是生成其他程序的。好像至今还没有做出来的?请恕我孤陋寡闻!直到接触到choice命令(XP系统里没有,没有深入研究过)才豁然开朗。原理很简单,小弟班门弄斧了!
我写的用星号代替输入的密码的批处理基本的输入密码,读取密码都可以准确实现,甚至可以用一定的算法加密。但是有几个难题还是无法解决的:
1、密码只能用指定个数的字符。
2、输入密码时会轻微闪屏。
3、不能删除输入错误的字符(原来想加入退格符作为choice的选项,但是choice的有效字符只能是数字和字母,只好作罢)
4、输入指定个数后自动确定,不能用回车(这个问题并不是不能解决,只是感觉改了以后完全多余)
本人刚学批处理不长时间,代码难免有错误或多余的部分,希望高手们给小弟多加指点!也可以联系我的QQ,我的QQ号码是376076482
注:程序需要用到choice,如果您使用的是XP系统,请将choice.exe文件放到system32文件夹下。我已经把文件上传到“中国DOS联盟文件上传系统”。
代码如下:
@echo off
color 3a
title 输入密码显示星号批处理
echo.
echo.
echo 还有一个难题未解决:就是密码必须是指定个数的字母或数字!
echo.
echo 本演示程序指定密码字符限制为8个
echo.
echo 默认用户名是guest。请设置密码。用户名和密码不区分大小写。
pause
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::在下面输入密码,输入后跳转到编译模块,将choice的返回码编译成输入的字符::
::之后跳转到下一个字符并在屏幕输出上加一个星号字符。并把密码保存到文件。::
::使用时可以美化界面,甚至做成和正规程序差不多的登陆界面。保存密码的文件::
::也可以保存到隐秘的位置或者网络路径。甚至还可以按一定的算法把密码保存成::
::加密的,登录时再进行反运算恢复。原来我还想加入退格符使它可以有退格删除::
::的功能,但是choice的选项只能是字母和数字,只好作罢。为保持代码整洁凑字::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:pass
cls
set jindu=1
choice /c 1234567890abcdefghijklmnopqrstuvwxyz /m 请输入密码: /n
goto bianyi
:1
cls
set jindu=2
choice /c 1234567890abcdefghijklmnopqrstuvwxyz /m 请输入密码:* /n
goto bianyi
:2
cls
set b=%a%
set jindu=3
choice /c 1234567890abcdefghijklmnopqrstuvwxyz /m 请输入密码:** /n
goto bianyi
:3
cls
set c=%a%
set jindu=4
choice /c 1234567890abcdefghijklmnopqrstuvwxyz /m 请输入密码:*** /n
goto bianyi
:4
cls
set d=%a%
set jindu=5
choice /c 1234567890abcdefghijklmnopqrstuvwxyz /m 请输入密码:**** /n
goto bianyi
:5
cls
set e=%a%
set jindu=6
choice /c 1234567890abcdefghijklmnopqrstuvwxyz /m 请输入密码:***** /n
goto bianyi
:6
cls
set f=%a%
set jindu=7
choice /c 1234567890abcdefghijklmnopqrstuvwxyz /m 请输入密码:****** /n
goto bianyi
:7
cls
set g=%a%
set jindu=8
choice /c 1234567890abcdefghijklmnopqrstuvwxyz /m 请输入密码:******* /n
goto bianyi
:8
cls
echo 请输入密码:********
if '%login%'=='1' goto yanzheng
set h=%a%
echo %a%%b%%c%%d%%e%%f%%g%%h% >password
echo 密码设置成功,即将转到登陆界面!
ping 127.1>nul&goto login
:bianyi
if %errorlevel% lss 10 set a=%errorlevel%&&goto %jindu%
if '%errorlevel%'=='10' set a=0
if '%errorlevel%'=='11' set a=a
if '%errorlevel%'=='12' set a=b
if '%errorlevel%'=='13' set a=c
if '%errorlevel%'=='14' set a=d
if '%errorlevel%'=='15' set a=e
if '%errorlevel%'=='16' set a=f
if '%errorlevel%'=='17' set a=g
if '%errorlevel%'=='18' set a=h
if '%errorlevel%'=='19' set a=i
if '%errorlevel%'=='20' set a=j
if '%errorlevel%'=='21' set a=k
if '%errorlevel%'=='22' set a=l
if '%errorlevel%'=='23' set a=m
if '%errorlevel%'=='24' set a=n
if '%errorlevel%'=='25' set a=o
if '%errorlevel%'=='26' set a=p
if '%errorlevel%'=='27' set a=q
if '%errorlevel%'=='28' set a=r
if '%errorlevel%'=='29' set a=s
if '%errorlevel%'=='30' set a=t
if '%errorlevel%'=='31' set a=u
if '%errorlevel%'=='32' set a=v
if '%errorlevel%'=='33' set a=w
if '%errorlevel%'=='34' set a=x
if '%errorlevel%'=='35' set a=y
if '%errorlevel%'=='36' set a=z
goto %jindu%
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::以下是登陆模块:::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:login
cls
set login=1
echo 默认用户名是guest。用户名和密码不区分大小写。
set name=
set /p name=请输入用户名:
if /i '%name%'=='guest' goto pass
if /i not '%name%'=='guest' echo 用户名输入错误,请按任意键重新输入!&pause>nul&goto login
:yanzheng
set a=%a%%b%%c%%d%%e%%f%%g%%h%
for /f %%p in (password) do set p=%%p
if /i '%a%'=='%p%' echo 密码正确,登陆成功!&ping 127.1>nul&goto start
echo 密码错误,请重新输入!&ping 127.1>nul&goto pass
pause>nul&exit
:start
cls
echo 这里是程序正文......
echo 演示结束,按任意键退出...
pause>nul&exit
作者: Orilla
时间: 2008-3-24 17:04
说支持必须得过来支持。。。。。呵呵
作者: yuseecn
时间: 2008-3-28 18:13
支持
作者: Shinaterry
时间: 2008-3-30 12:40
支持, 希望继续完善..
作者: bat-zw
时间: 2008-3-30 13:16
标题: 加油,大家共同提高:
可惜XP没有CHOICE命令,也没细看,先顶了再说。
作者: Shinaterry
时间: 2008-3-30 16:30

觉得非常有必要加入"回车"..
作者: topmcs
时间: 2008-4-2 12:46
呵呵。。。。支持啊。。。。希望可以修好BUG啊