I'm having trouble making a random password generator.

March 22, 2012 at 13:25:27
Specs: Windows 7
I'm having trouble making a random password generator. Here's what I have so far:

@echo off
title = Random password generator
:start
cls
echo Welcome to random password generator.
echo.
echo 1) Generate a case-sensitive password
echo 2) Generate a non-case-sensitive password
echo 3) Close
choice /C:123 /n
set choice = %errorlevel%
if %choice% == 1 set op = 1
if %choice% == 2 set op = 2
if %choice% == 3 exit
cls
echo 1) Numbers only
echo 2) Numbers and letters
echo 3) Letters only
choice /C:123 /n
set choice = %errorlevel%
if %choice% == 1 set opp = 1
if %choice% == 2 set opp = 2
if %choice% == 3 set opp = 3
cls
echo.How many characters?
echo (suggested: 10-25 for max security)
set /p length = ""

First it's gotta ensure that the last part is a number and that number is less than 50 and greater then 5.
Then it has to generate a random number between 1 and 62 (number varies)
Then it needs to make it so 10 = 0, 11 = A... ...37 = a........ ect:
1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
then it would do that a bunch of times and generate a string. ex strings:
(case sensitive)
ss54Hd564DF0
or (non-case sensitive)
ds879rfds8f7d
or (numbers only)
109273934874
or (letters only)
kadshdflkhdkhi
or (letters only + case sensitive)
dsDfgrFgDFGdf

But if I use %random% it will give 1 - 32776 (or some strange number) So I need to generate 1 - 36 or 1 - 62

I can prob write most of it if you give me the random-number thing and the translate-over-to-letters thing.


See More: Im having trouble making a random password generator.

Report •


#1
March 22, 2012 at 13:47:49
I also want to know how to loop through all files in your computer and add 1 to a var each time so that I can figure out how many files are in your computer.

Report •

#2
March 23, 2012 at 19:56:13
set op = 1

I know this looks like good whitespace usage, and in other languages it often is, but in batch this sets a variable named "op "(including space) to the value of " 1"(including space). It doesn't ignore the spaces so you shouldn't add them unless you want them.

C:\Documents and Settings\Judago>set op = 1

C:\Documents and Settings\Judago>echo %op%
%op%

C:\Documents and Settings\Judago>echo %op %
 1

C:\Documents and Settings\Judago>

But if I use %random% it will give 1 - 32776 (or some strange number) So I need to generate 1 - 36 or 1 - 62

set /a num=(%random% %% 36) + 1

To get at it you could use a variable as a list and the random number to take a single character. For this you will actually want 0-35 or 0-61 because the first character is at position 0.

Here is a quick example:

@echo off
SetLocal EnableDelayedExpansion

set list=abcdefghijklmnopqrstuvwxyz
set total=26
set length=10

set string=

for /l %%a in (1 1 %length%) do (
    set /a position=!random! %% total
    for %%b in (!position!) do (
        set string=!string!!list:~%%b,1!
    )
)
echo !string!
pause  

It's easy enough to just change the list/total/length values according to user input.


Report •
Related Solutions


Ask Question