cmd批处理中的密码生成器

Ori*_*Ori 0 windows cmd batch-file password-generator

我正在尝试使用批处理创建一个简单的密码生成器。我知道如果我尝试使用真正的语言会容易得多,但我需要它在 cmd 批处理中。我需要它来生成一个 10 位密码,其中至少包含 1 个小写字母、1 个大写字母、1 个数字和一个特殊字符。

我找到了这段代码,但正如你所看到的,它并不能确保我的所有限制都适用

@Echo Off
cd %~dp0
Setlocal EnableDelayedExpansion
Set _RNDLength=10
Set _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~!@#$%
Set _Str=%_Alphanumeric%9876543210
echo %_Str:~18%
:_LenLoop
IF NOT "%_Str:~18%"=="" SET _Str=%_Str:~9%& SET /A _Len+=9& GOTO :_LenLoop
SET _tmp=%_Str:~9,1%
SET /A _Len=_Len+_tmp
Set _count=0
SET _RndAlphaNum=
:_loop
Set /a _count+=1
SET _RND=%Random%
Set /A _RND=_RND%%%_Len%
SET _RndAlphaNum=!_RndAlphaNum!!_Alphanumeric:~%_RND%,1!
If !_count! lss %_RNDLength% goto _loop
echo !_RndAlphaNum!
Run Code Online (Sandbox Code Playgroud)

Aac*_*ini 5

这个问题有一个固有的复杂性:对于每个所需的子集(大写、小写、数字和特殊),字符集具有不同的频率,因此出现特殊字符的可能性较小,而出现字母的可能性更大。但是,即使频率相等(重复特殊字符和数字,直到所有四个子集的字符数相同),这也不能保证所有四个集合至少出现一次。

下面的解决方案使用了一种不同的方法:它分别管理每个子集并创建一个由 10 个数字组成的列表,这些数字表示每个子集。这样,就可以计算每个子集出现在列表中的次数,并轻松强制所需条件。

@echo off

setlocal
set "set[1]=ABCDEFGHIJKLMNOPQRSTUVWXYZ"  &  set "len[1]=26"  &  set "num[1]=0"
set "set[2]=abcdefghijklmnopqrstuvwxyz"  &  set "len[2]=26"  &  set "num[2]=0"
set "set[3]=0123456789"                  &  set "len[3]=10"  &  set "num[3]=0"
set "set[4]=~!@#$%%"                     &  set "len[4]=6"   &  set "num[4]=0"
setlocal EnableDelayedExpansion

rem Create a list of 10 random numbers between 1 and 4;
rem the condition is that it must be at least one digit of each one

rem Initialize the list with 10 numbers
set "list="
for /L %%i in (1,1,10) do (
   set /A rnd=!random! %% 4 + 1
   set "list=!list!!rnd! "
   set /A num[!rnd!]+=1
)

:checkList
rem Check that all digits appear in the list at least one time
set /A mul=num[1]*num[2]*num[3]*num[4]
if %mul% neq 0 goto listOK

   rem Change elements in the list until fulfill the condition

   rem Remove first element from list
   set /A num[%list:~0,1%]-=1
   set "list=%list:~2%"

   rem Insert new element at end of list
   set /A rnd=%random% %% 4 + 1
   set "list=%list%%rnd% "
   set /A num[%rnd%]+=1

goto checkList
:listOK

rem Generate the password with the sets indicated by the numbers in the list
set "RndAlphaNum="
for %%a in (%list%) do (
   set /A rnd=!random! %% len[%%a]
   for %%r in (!rnd!) do set "RndAlphaNum=!RndAlphaNum!!set[%%a]:~%%r,1!"
)

echo !RndAlphaNum!
Run Code Online (Sandbox Code Playgroud)

这个代码可以很容易地修改,以便生成一个 10 个字符的密码,每个子集中有给定数量的元素;为此,只需使用所需的数字(总和必须为10)初始化“num”数组,每次在列表中插入新元素时递减这些数字,并更改所有元素总和为零的条件。