lundi 4 novembre 2019

Random password generator returning wrong characters - java

I am currently using the following code to generate a random password:

static public String randomPasswordGenerator(int n, String valid) 
{ 
      char[] pwd = new char[n];
      Random rnd = new Random();
      for (int i = 0; i < pwd.length; i++)
      {
        pwd[i] = valid.charAt(rnd.nextInt(valid.length()));
      }
      return new String(pwd);
}

The valid parameter is set currently as:

String valid = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ123456789#$&%£";

Sometimes when I generate a password I find strange characters being generated, as for an example I can get: n9iB8Qj6inRÂ (where the last char is not included in the valid set)

Any idea why would this happen?

Thanks a lot




Aucun commentaire:

Enregistrer un commentaire