How does this batch password generation work? - batch-file

I am creating a password generator in batch, and had to use some help from the internet to finish it, but I am having difficulty interpreting the section of the code I had to borrow. Would someone please explain it?
:generate
#Echo Off
color 0a
set /P usernumberlength="What length do you want your password to be? "
pause
cls
Setlocal EnableDelayedExpansion
Set RNDLength=%usernumberlength%
/// from here
Set Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
Set Str=%Alphanumeric%987654321
:loop
IF NOT "%Str:~18%"=="" SET Str=%Str:~9%& SET /A Length+=9& GOTO :loop
SET tmp=%Str:~9,1%
SET /A Length=Length+tmp
Set count=0
SET RndAlphaNum=
:loop2
Set /a count+=1
SET RND=%Random%
Set /A RND=RND%%%Length%
SET RndAlphaNum=!RndAlphaNum!!Alphanumeric:~%RND%,1!
If !count! lss %RNDLength% goto loop2
/// to here
Echo Password is: is !RndAlphaNum!
Echo Now choose what you want to do.
Echo 1) Go back to the beginning
Echo 2) Exit
set input=
set /p input= Choice:
if %input%==1 goto generate
if %input&==2 exit
pause

This working example (from the code you provided) contains the all-you-need section.
#echo Off
color 0a
setlocal enabledelayedexpansion
:generate
set /p usernumberlength="What length do you want your password to be? "
rem ----- begin section all you need -----
Set Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
rem "Length" is the string length of %alphanumeric%
Set Length=62
set count=0
rem "RndAlphaNum" is the output
SET RndAlphaNum=
:loop
Set /a count+=1
Set /A RND=%Random% %% %Length%
SET RndAlphaNum=!RndAlphaNum!!Alphanumeric:~%RND%,1!
if !count! lss %usernumberlength% goto loop
rem ----- end section all you need -----
echo Password is: !RndAlphaNum!
goto generate

Related

Why does my batch file generated text not format properly?

The Problem:
I'm trying to create a batch file which randomly generates passwords, but I cannot seem to get the generated passwords to format properly.
Before, I just chose to generate numbers, but I realized that it wasn't very secure for users, and I needed to include letters and numbers. I attempted, but it output the wrong thing (kind of).
I want the generated passwords to be at least 8 characters long, and 32 characters maximum. I tried it, but it completely broke the code and it wouldn't output anything at all. Also, it outputs the same passwords for each one on every option. For example, if I chose option 1 (generate 2 passwords), it would output ud8aqas and ud8aqas. If I chose option 2 (generate 5 passwords), it would output p9dkda, p9dkda, p9dkda, p9dkda, and p9dkda, and so on.
The code that I want to use:
#echo off
:Start2
cls
goto Start
:Start
title DotPass Password Generator [BETA]
echo Thanks for using DotPass! If you have any questions, feel free to email us at mhartmix#gmail.com.
echo Please maximise this program so you can see all of the details we have shown!
echo ------------------------------------------------------------------------------------------------------
echo DotPass will generate you a new PIN (or password, if that's what you're going to use it for).
echo You can use these for any of your online accounts! Remember to always note the PIN/password down somewhere!
echo -------------------------------------------------------------------------------------------------------
echo Note: The passwords/PIN's can only be seen by you, not us. To read more, go to github.com/TechGeekMic/DotPass/blob/Main/security.md
echo Passwords generated are never stored on the cloud, or locally, so that's why you should make a note of your randomly generated PIN/password.
echo -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
echo How many passwords do you want DotPass to generate?
echo 1) 2 Generated Random Password
echo 2) 5 Generated Random Passwords
echo 3) 10 Generated Random Passwords
echo Type the number of your choice below. ( i.e 2 )
set input=
set /p input= Choice:
if %input%==1 goto A if NOT goto Start2
if %input%==2 goto B if NOT goto Start2
if %input%==3 goto C if NOT goto Start2
:A
cls
Setlocal EnableDelayedExpansion
Set _RNDLength=12
Set _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
Set _Str=%_Alphanumeric%987654321
:_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 Your two randomly generated passwords are !_RndAlphaNum! and !_RndAlphaNum!
)
echo What do you want do to? Here are a few options.
echo 1) Go back to the beginning
echo 2) Exit DotPass
echo 3) Shutdown your computer
echo Type the number of your choice below. ( i.e 1 )
set input=
set /p input= Choice:
if %input%==1 goto Start2 if NOT goto Start 2
if %input%==2 goto Exit if NOT goto Start 2
if %input%==3 goto Shutdown if NOT goto Start 2
:Shutdown
shutdown
:Exit
exit
:B
cls
Setlocal EnableDelayedExpansion
Set _RNDLength=12
Set _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
Set _Str=%_Alphanumeric%987654321
:_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 Your five randomly generated passwords for you are !_RndAlphaNum!, !_RndAlphaNum!, !_RndAlphaNum!, !_RndAlphaNum! and !_RndAlphaNum!
)
echo What do you want do to? Here are a few options.
echo 1) Go back to the beginning and start again
echo 2) Exit DotPass
echo Type the number of your choice below. ( i.e 1 )
set input=
set /p input= Choice:
if %input%==1 goto Start2 if NOT goto Start 2
if %input%==2 goto Exit if NOT goto Start 2
if %input%==3 goto Shutdown if NOT goto Start 2
:C
cls
Setlocal EnableDelayedExpansion
Set _RNDLength=12
Set _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
Set _Str=%_Alphanumeric%987654321
:_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 Your ten randomly generated passwords for you are !_RndAlphaNum!, !_RndAlphaNum!, !_RndAlphaNum!, !_RndAlphaNum!, !_RndAlphaNum!, !_RndAlphaNum!, !_RndAlphaNum!, !_RndAlphaNum!, !_RndAlphaNum! and !_RndAlphaNum!
echo What do you want do to? Here are a few options.
echo 1) Start again
echo 2) Exit DotPass
set input=
set /p input= Choice:
if %input%==1 goto Start2 if NOT goto Start 2
if %input%==2 goto Exit if NOT goto Start 2
if %input%==3 goto Shutdown if NOT goto Start 2
You can try it out for yourself as long as you don't publish it or modify it.
I am very new to batch scripts! Can someone help?

Batch Random Charachters Err

I was playing around with batch scripts on windows and I was trying to make a basic password generator.
( Maximum setlocal recursion level reached. )
My code :
#Echo Off
color 0a
set /P lengthnumberuser="What length do you want your password to be? "
pause
cls
:code
Setlocal EnableDelayedExpansion
set _RNDLength=16
set _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
set _Str=%_Alphanumeric%987654321
:_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!
goto code
pause
Just delete the goto code at the end of the your code.
See Here : Link

Using Mode Command to Resize Command prompt Clears Entire Screen

So I try Writing the codes to generate random characters and I want to make the screen always at the same size but when I tried using mode command it clears all text or you can call it entire screen
Here is the code:
:startprg
mode con: cols=120 lines=24
setlocal EnableDelayedExpansion
set char=abcdeijklmnstuvwxyzABCDETUVYZ23456789^^%%_`^^%%_`^^%%_`^^%%_`^^%%_`☺☻♥♦♣♠◘•○☺☻♥♦♣♠◘♂♀q7•-6○Z♂n¡* /*-+##$*()_=-=+
set count=0
set hla=0
:Number
set hla=120
set /a length=-1 + !hla!
:Loop
set /a count+=1
set /a rand=%Random%%%61
set buffer=!buffer!!char:~%rand%,1!
if !count! leq !length! goto Loop
title ^!buffer!
echo ^!buffer!
set /a length+=1
endlocal
goto startprg
Please help me I'm on Windows 10 and I don't know how to fix this problem
Currently each time you goto :startprg you reset the mode. Simply move the label to after mode con: in order to initiate it only once. So it should be:
#echo off
mode con: cols=120 lines=24
:startprg
setlocal EnableDelayedExpansion
So the full code would be as:
#echo off
mode con: cols=120 lines=24
:startprg
setlocal EnableDelayedExpansion
set char=abcdeijklmnstuvwxyzABCDETUVYZ23456789^^%%_`^^%%_`^^%%_`^^%%_`^^%%_`☺☻♥♦♣♠◘•○☺☻♥♦♣♠◘♂♀q7•-6○Z♂n¡* /*-+##$*()_=-=+
set count=0
set hla=0
:Number
set hla=120
set /a length=-1 + !hla!
:Loop
set /a count+=1
set /a rand=%Random%%%61
set buffer=!buffer!!char:~%rand%,1!
if !count! leq !length! goto Loop
title ^!buffer!
echo ^!buffer!
set /a length+=1
endlocal
goto startprg

How to fix this fibonacci batch lister?

I've researched about operand and operators, set /a , and setlocal EnableDelayedExpansion from this website but still somehow my code ends up telling
me that my operand is missing.
#echo off
cls
setlocal EnableDelayedExpansion
color 0a
title Fibonacci Lister
:Fibonacci
cls
set /a n1=1
echo %n1%
set /a n2=1
echo %n2%
set /a j=3
:loopfunction
set /a n%j%=!n%j-1%!+!n%j-2%!
echo !n%j%!
set /a j=%j%+1
goto :loopfunction
So I wanted to know what is the problem here. It is probably in line 14 where
the code is complex and hard to understand easily.
On line #14, you were using a variables named %j-1% and %j-2% which weren't declared.
Updated Script:
#echo off
setlocal EnableDelayedExpansion
color 0a
title Fibonacci Lister
:Fibonacci
set /a n1=1
echo %n1%
set /a n2=1
echo %n2%
set /a j=3
:loopfunction
set /a j1=j-1&set /a j2=j-2
set /a n%j%=!n%j1%!+!n%j2%!
if "!n%j%!"=="1836311903" goto :EOF
echo !n%j%!
set /a j=j+1
goto :loopfunction
#echo off
setlocal enableextensions disabledelayedexpansion
set n1=0
set n2=1
:loop
echo %n2%
set /a "n2=n1+n2" & set "n1=%n2%"
if %n2% gtr 0 goto loop

Search random line

I got 2 files bot.bat
#echo off & setlocal EnableDelayedExpansion
:botconfig
set botname=Unknow
set botcity=unknow
set botadress=unknow
set botage=unknow
for /f "delims=*" %%x in (botfeel.txt) do set botfeel=%%x
:askme
set /p how=
echo I feel %botfeel%
goto askme
and botfeel.txt
good
bad
sick
What i want is instead of showing just "i feel sick" it shows randomly one of the 3 words in botfeel.txt
Make your batch file look like this:
#echo off
:botconfig
set botname=Unknow
set botcity=unknow
set botadress=unknow
set botage=unknow
:askme
set /p how=
SET /a line=%RANDOM% * 3 / 32768
if %line%==0 goto firstline
for /F "skip=%line% delims=" %%i in (botfeel.txt) do set "botfeel=%%i"&goto nextline
:nextline
echo I feel %botfeel%
goto askme
:firstline
set /p botfeel= <botfeel.txt
goto nextline

Resources