0

I am trying to create a random WPA password generator using java to give me as secure of a Wifi password as possible, which can be changed when I desire.

Is there a way to quickly populate an array with all of the characters which would be allowed to be used in a WPA(2) password as opposed to manually entering the chars one by one?

John
  • 1,556
  • 4
  • 26
  • 42

4 Answers4

4

Shortest way I can think of would be:

String s = new String(IntStream.rangeClosed(32, 126).toArray(), 0, 95);
char[] ascii = s.toCharArray();

If you're just generating a sequence of characters, you don't need to make an array of all ASCII characters:

String s = new String(random.ints(length, 32, 127).toArray(), 0, length);
VGR
  • 40,506
  • 4
  • 48
  • 63
  • I didn't see that String had a int[] constructor, I also wanted to use the Stream version. – matt Sep 03 '15 at 20:37
3
int pwLength = 8;
Random numberGenerator = new Random();
char[] pw = new char[pwLength];
for(int i = 0; i<pwLength; i++){
    pw[i] = numberGenerator.nextInt(95) + 32;
}
matt
  • 10,892
  • 3
  • 22
  • 34
1

Well the range of valid ascii values are 32-126 (see https://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters). So you could simply generate a random number between those values for each character and then turn the ascii value to a string.

You do Character.toString ((char) i); or String.valueOf(Character.toChars(int)) to turn an ascii value to a string.

Put together 8-63 of these characters and you have yourself a random wpa2 passphrase.

Austin
  • 4,801
  • 6
  • 34
  • 54
  • 1
    Where are you getting 36 from? Also, you really should indicate that 176 is an octal value. Or, since hardly anyone uses octal anymore, use hex (but indicate that it's hex, such as with U+ or 0x). – VGR Sep 03 '15 at 20:21
  • @VGR Had a typo on the ranges there. And 32-126 should be the decimal value. – Austin Sep 03 '15 at 20:24
  • I was curious about that since I had actually referenced the same list originally – John Sep 03 '15 at 20:38
0
private void buildPassword() {
        for (int i = 33; i < 127; i++) {
            l.add(i);
        }

        l.remove(new Integer(34));
        l.remove(new Integer(47));
        l.remove(new Integer(92));

        for (int i = 0; i < 10; i++) {
            randInt = l.get(new SecureRandom().nextInt(91));
            sb.append((char) randInt);
        }

        str = sb.toString();
    }
Pastasaurus Rex
  • 112
  • 1
  • 2
  • 9