How to generate password?

Dear all,Do anyone know how to generate the password?I don't know use which method to that aim...Can anyone give some advices?
[169 byte] By [tsangboy] at [2007-9-26 2:51:07]
# 1

I don't really understand your question but if you want to automate the generation of the password you can use the userid as password the first time the user id is created and next time the same user log on to your application he/she will be prompted to change the password according to the rules you set up.

Klint

saen at 2007-6-29 10:38:12 > top of Java-index,Archived Forums,Java Programming...
# 2
I want to generate the password.Can I use any function to get a random character?
tsangboy at 2007-6-29 10:38:12 > top of Java-index,Archived Forums,Java Programming...
# 3

Here's the code:

public class RandomPassword

{

int l = 0;

int h = 5;

String charr[] = {"A", "B", "C", "1", "2", "3"};

private String[] rarr = new String[6];

RandomPassword()

{

buildPassword();

printPassword();

}

private void buildPassword()

{

int n;

for (int i = 0; i <= 5; i++)

{

n = getRandom(l, h);

rarr = charr[n];

}

}

private void printPassword()

{

for (int i = 0; i <= 5; i++)

System.out.print(rarr);

System.out.println();

}

private int getRandom( int lower, int upper )

{

double nNum = Math.random();

int nDif,nResult;

while ( true )

{

nDif = (upper + 1) - lower;

nResult = lower + (int)( (double)nDif * nNum );

if ( ( nResult >= lower ) && ( nResult <= upper ) )

{

return nResult;

}

}

}

public static void main(String args[])

{

try

{

new RandomPassword ();

System.exit(0);

}

catch ( Exception e )

{

System.out.println ( "Can not start program");

}

}

}

Klint

saen at 2007-6-29 10:38:12 > top of Java-index,Archived Forums,Java Programming...
# 4
RTFM!Math.random()and java.util.RandomThese will give you random numbers. Convert these to chars
dewangs at 2007-6-29 10:38:12 > top of Java-index,Archived Forums,Java Programming...
# 5
Or if you'd like passwords with both numbers and characters, look at the createTempFile method in the File class. You could create a temp file and use the filename... (Not the best way to go about it, I know, but I think it'll work.)~H
hallyhaa at 2007-6-29 10:38:12 > top of Java-index,Archived Forums,Java Programming...
# 6

Tsang,

This one is even better.

public class RandomPassword2

{

private String base;

private int len;

private String pwd;

private char [] charr;

private char [] rarr;

public RandomPassword2(String b, int l)

{

base = b;

len = l;

}

public RandomPassword2()

{}

public String getRandomPassword()

{

buildPassword();

return pwd;

}

private void buildPassword()

{

charr = new char[base.length()];

charr = base.toCharArray();

rarr = new char[len];

int n;

for (int i = 0; i <= len - 1; i++)

{

n = getRandom(0, base.length() - 1);

rarr = charr[n];

}

pwd = new String(rarr);

}

private int getRandom( int lower, int upper )

{

double nNum = Math.random();

int nDif,nResult;

while ( true )

{

nDif = (upper + 1) - lower;

nResult = lower + (int)( (double)nDif * nNum );

if ( ( nResult >= lower ) && ( nResult <= upper ) )

{

return nResult;

}

}

}

}

Klint

saen at 2007-6-29 10:38:12 > top of Java-index,Archived Forums,Java Programming...
# 7
Don't use random passwords, use 'dog' as the password noone will guess it.
kaze0 at 2007-6-29 10:38:12 > top of Java-index,Archived Forums,Java Programming...
# 8

Michael,

I do agree but in some instances it might be required to generate a random password. In the environment I am woring in, AS400, you get by default the user id as password when you create the user. When the user log on the AS400 he/she is prompted to change the password according to the rules you set.

Klint

saen at 2007-6-29 10:38:12 > top of Java-index,Archived Forums,Java Programming...