0

I am wanting to create a password generator which uses a list of pre-generated passwords using batch. The following script I am trying to adapt. I want the system to automatically generate a password using the prefix LONDON Also I want a section where we can can change the word if possible. Can anyone assist?

Also, it needs to generate a password from LONDON01 to LONDON100.

@echo off
:Start2
cls
goto Start
:Start
title Welcome to my Password Generator
echo I will make you a new password.
echo Please write the password down somewhere in case you forget it.
echo ----------------------------------------­-----------------------
echo 1) 1 Random Password
echo 2) 5 Random Passwords
echo 3) 10 Random Passwords
echo Input your choice
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
echo Your password is %random%
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 Start2 if NOT goto Start 2
if %input%==2 goto Exit if NOT goto Start 2
:Exit
exit
:B
cls
echo Your 5 passwords are %random%, %random%, %random%, %random%, %random%.
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 Start2 if NOT goto Start 2
if %input%==2 goto Exit if NOT goto Start 2
:C
cls
echo Your 10 Passwords are %random%, %random%, %random%, %random%, %random%, %random%, %random%, %random%, %random%, %random%
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 Start2 if NOT goto Start 2
if %input%==2 goto Exit if NOT goto Start 2
8
  • My current opinion would be to get with the times and move it to Powershell... Would that work too? Or is Batch a requirement? Apr 8, 2013 at 19:16
  • Batch is a requirement due to restrictions on the office PCs. We need this for our desktop agents. Apr 8, 2013 at 19:24
  • Check out superuser.com/questions/349474/…
    – Ofiris
    Apr 8, 2013 at 19:34
  • How do I make it use "LONDON" Then a pre-defined number for eg. LONDON05 LONDON04 LONDON03 Apr 8, 2013 at 19:39
  • 3
    Why do you need passwords so easily guessable? Apr 8, 2013 at 20:53

3 Answers 3

4

If you want a fairly decent "random", then I would suggest a fast program in C++ or something else (which can be distributed to your office systems). But, if you are stuck on cmd then:

@echo off
set minimum=0
set maximum=100
set pre=LONDON

:A
set randnumber=%random%
if %randnumber% GEQ %minimum% (
if %randnumber% LEQ %maximum% (
echo %pre%%randnumber%
goto END
)
)
goto A
:END

Output Examples:

enter image description here

Shouldn't be too hard to adjust that to work for your situation.


Edit:

As a supplement, I wanted to speak to this as many others have:

This is an extremely insecure way to generate passwords and really shouldn't be used for anything outside of locking a phone, perhaps even not that.

A true password generator should have 'salt' that is randomly generated for each password and is always unique. Brute force on your password set (LONDON0 - LONDON100) would take seconds on a mid-line computer.

Instead, you could use Python, something simple like:

import string, random

def genPass(prefix='LONDON', size=8, chars=string.ascii_uppercase + string.ascii_lowercase + string.digits + '@!.&%$'):
    return prefix + ''.join(random.choice(chars) for i in range(size))

print genPass()

Which gets you a much more random, or at least unique, password set. If you want to change anything, just call genPass(prefix='NOTLONDON', size=2) instead, for example.

0

This should fix your existing batch file so it works.

@echo off

set "prefix=LONDON"

:Start2
cls
goto Start

:Start
title Welcome to my Password Generator
echo I will make you a new password.
echo Please write the password down somewhere in case you forget it.
echo ----------------------------------------­-----------------------
echo 1) 1 Random Password
echo 2) 5 Random Passwords
echo 3) 10 Random Passwords
echo Input your choice
set input=
set /p input= Choice:
if "%input%."=="." goto :Start
if %input%==1 goto A
if %input%==2 goto B
if %input%==3 goto C
goto :Start



:A
cls
echo Your password is %prefix%%random%

:reprompt1
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%."=="." goto :reprompt1
if %input%==1 goto Start2
if %input%==2 goto Exit
goto :reprompt1

:Exit
goto :EOF
rem exit



:B
cls
echo Your 5 passwords are %prefix%%random%, %prefix%%random%, %prefix%%random%, %prefix%%random%, %prefix%%random%.

:reprompt5
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%."=="." goto :reprompt5
if %input%==1 goto Start2
if %input%==2 goto Exit
goto :reprompt5



:C
cls
echo Your 10 Passwords are %prefix%%random%, %prefix%%random%, %prefix%%random%, %prefix%%random%, %prefix%%random%, %prefix%%random%, %prefix%%random%, %prefix%%random%, %prefix%%random%, %prefix%%random%

:reprompt10
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%."=="." goto :reprompt10
if %input%==1 goto Start2
if %input%==2 goto Exit
goto :reprompt10

If you need something different, just let me know.


Edit:

I just noticed the comment indicating you need the results to be in the range of LONDON01 to LONDON100, so I modified the batch file to do that:

@echo off

set "prefix=LONDON"

:Start2
cls
goto Start

:Start
title Welcome to my Password Generator
echo I will make you a new password.
echo Please write the password down somewhere in case you forget it.
echo ----------------------------------------­-----------------------
echo 1) 1 Random Password
echo 2) 5 Random Passwords
echo 3) 10 Random Passwords
echo Input your choice
set input=
set /p input= Choice:
if "%input%."=="." goto :Start
if %input%==1 goto A
if %input%==2 goto B
if %input%==3 goto C
goto :Start



:A
cls
call :getpws 1
call :showpws 1

:reprompt1
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%."=="." goto :reprompt1
if %input%==1 goto Start2
if %input%==2 goto Exit
goto :reprompt1

:Exit

set "pw1="
set "pw2="
set "pw3="
set "pw4="
set "pw5="
set "pw6="
set "pw7="
set "pw8="
set "pw9="
set "pw10="
set "pwcount="
set "pwindex="
set "pwmessage="
set "pwresult="

goto :EOF
rem exit



:B
cls
call :getpws 5
call :showpws 5

:reprompt5
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%."=="." goto :reprompt5
if %input%==1 goto Start2
if %input%==2 goto Exit
goto :reprompt5



:C
cls
call :getpws 10
call :showpws 10

:reprompt10
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%."=="." goto :reprompt10
if %input%==1 goto Start2
if %input%==2 goto Exit
goto :reprompt10



:getpws

set pwcount=%~1

rem      this step (clear) not totally necessary...
call :clearallpws
for /L %%f in (1,1,%pwcount%) do call :get1pw %%f
goto :EOF



:clearallpws
for /L %%f in (1,1,10) do call :clearpw %%f
goto :EOF



:clearpw
rem clear password numbered by %1

set pwindex=%~1
for /F "usebackq delims=" %%g in (`echo set "pw%pwindex%="`) do %%g
goto :EOF



:get1pw

set pwindex=%~1

rem     get a random number, add a leading 0 to make sure it is at least 2 digits long
set pwresult=0%random%

rem     keep the last two digits
set "pwresult=%pwresult:~-2,2%"

rem     this will now be something from 00 to 99
rem     now add 1
set /a pwresult+=1

rem     this will now be a number from 1 to 100. Add leading "0" for numbers below 10
if %pwresult% LEQ 9 set "pwresult=0%pwresult%"

rem     this will now be a number from 01 to 100. Insert the prefix
set "pwresult=%prefix%%pwresult%

rem     now assign it to pw(n) variable. and return
for /F "usebackq delims=" %%g in (`echo set "pw%pwindex%=%pwresult%"`) do %%g
goto :EOF


:showpws

set pwcount=%~1

set "pwmessage=Your %pwcount% passwords are: %pw1%"
if %pwcount% EQU 1 set "pwmessage=Your password is: %pw1%"

for /L %%g in (2,1,%pwcount%) do call :append1pw %%g
echo %pwmessage%
goto :EOF


:append1pw

set pwindex=%~1
for /F "usebackq delims=" %%h in (`echo set "pwmessage=%pwmessage%, %%pw%pwindex%%%"`) do %%h
goto :EOF
0

Namespace: System.Web.Security; GeneratePassword Method

You need to enable .Net 4 in powershell:

  1. View powershell home path:

    powershell $pshome

  2. Go to powershell home directory:

    cd %windir%\system32\WindowsPowerShell\v1.0

  3. Edit powershell.exe.config and powershell_ise.exe.config:

 <?xml version="1.0"?> 
 <configuration> 
     <startup useLegacyV2RuntimeActivationPolicy="true"> 
         <supportedRuntime version="v4.0.30319"/> 
         <supportedRuntime version="v2.0.50727"/> 
     </startup> 
 </configuration>

command-line:

powershell [Reflection.Assembly]::LoadWithPartialName('System.Web')^|Out-Null;1..12^|%{[System.Web.Security.Membership]::GeneratePassword(16,3)}

Output:

}=+TW7Nsq?W(7Pr=
H@^*WKxH{S._79-d
y5Ls@ii+[P;3&P{3
^B%3eBHo|2V!Q{UW
2$(F8;s9prwURA#c
dZ*k(e(F_C%XJv-}
#SP9La)sWBhVh][Z
u7^d3U36N@66vSe+
Z.hH0e$Z/Jdb4CHs
XxkZTxJr.t(|QHn&
C+qOCz2G(MpGZF)W
06ZY&q^E/z9K5cKD

simple password, command-line:

powershell 1..12^|%{$i='';1..7^|%{$i+=[char](Get-Random -min 65 -max 90)};1..2^|%{$i+=(Get-Random -min 10 -max 99)};echo $i}

Output:

VCXJNCA7249
MUIFGHV8240
YOVGWPH8556
FGOEEFR7862
SWOOAQS8714
OLHYSFW4985
WOEHCRN8249
VHPDMLR1991
TKOEMBO7344
FFMJGSJ8325
KLUVAEF8960
LKYVHQH2212

simple password, variant 2, command-line:

powershell 1..3^|%{$i='LONDON-';1..4^|%{$i+=[char](Get-Random -min 65 -max 90)};$i+='-'+(Get-Random -min 1001 -max 9999);echo $i}

Output:

LONDON-UCUT-3323
LONDON-CCCB-5950
LONDON-SNFX-9316

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.