Codz: @echo off cls :allyesno set errorlevel=>nul echo 请输入登录口令 set/p password= set |findstr "ph4nt0m" if "%errorlevel%"=="0" echo 口令正确&goto end echo 口令错误&goto allyesno :end echo 你成功登录系统
Codz: 请输入登录口令 test 口令错误 请输入登录口令 ph4nt0mallyesno password=ph4nt0mallyesno 口令正确 你成功登录系统
C:test>
由于程序的验证方式是 set |findstr "ph4nt0m" 所以只要包含ph4nt0m字符的 密码 都被当成正确密码 所以密码ph4nt0mallyesno 也通过了
为了避免这个问题 我设置了 匹配参数/</> 对数据进行检验 修改后的程序 如下
Codz: @echo off cls :allyesno set errorlevel=>nul echo 请输入登录口令 set/p password= set |findstr "/<ph4nt0m/>" if "%errorlevel%"=="0" echo 口令正确&goto end echo 口令错误&goto allyesno :end echo 你成功登录系统
Codz: @echo off cls :allyesno set errorlevel=>nul echo 请输入登录口令 set/p password= rem 如果密码字符串包含此行任一字符_+|-=[]{};':,./">~`!@#$%^&*()_+|-=[]{};':,./<>? 则必须使用匹配模式<> rem 需要双写的字符 rem 不可以作为密码的字符 " set password|findstr "/<ph4nt0m/>" if "%errorlevel%"=="0" echo 口令正确&goto end echo 口令错误&goto allyesno :end set password=>nul echo 你成功登录系统
注:当密码字符串中有字符/的时候 需要将字符双写// 例 set password|findstr "/<///>" 登录的时候 只需要写一次/不需要双写