抄了一个 PasswordGenerator.java

来源: 2010-01-21 19:08:25 [旧帖] [给我悄悄话] 本文已被阅读:

/*
* Random Password Generator Written By Wicked at roob.us
*
* Compile: javac PasswordGenerator.java
*
* Usage: java PasswordGenerator [option] length
*
* Options:
* -l For only lowercase
* -u For only uppercase
* -n For only numbers
* -a For only alphanumeric
* -p For printable ASCII
*
*/

import java.util.Random;
// for jdk 6 you can use java.securety.SecureRandom
//import java.security.SecureRandom;

public class PasswordGenerator {

public PasswordGenerator (){int i =0;}


public static void main(String[] args) throws Exception {

int passLength = 0;
//SecureRandom wheel = SecureRandom.getInstance("SHA1PRNG");
Random wheel = new Random();

char[] lowerCase = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char[] upperCase = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
char[] numeric = new char[]{'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};

char[] printableAscii = new char[]{'!', '"', '#', '$', '%', '(', ')', '*', '+', '-', '.', '/', ''',
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', ':', '', '?', '@',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'[', '\', ']', '^', '_', '`', '{', '|', '}', '~',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

char[] alphaNumberic = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};


if (args.length == 2) {
if (args[0].equals("-l")) {
passLength = Integer.parseInt(args[1]);
for (int i = 0; i int random = wheel.nextInt(lowerCase.length);
System.out.print(lowerCase[random]);
}
} else if (args[0].equals("-u")) {
passLength = Integer.parseInt(args[1]);
for (int i = 0; i int random = wheel.nextInt(upperCase.length);
System.out.print(upperCase[random]);
}
} else if (args[0].equals("-n")) {
passLength = Integer.parseInt(args[1]);
for (int i = 0; i int random = wheel.nextInt(numeric.length);
System.out.print(numeric[random]);
}
} else if (args[0].equals("-a")) {
passLength = Integer.parseInt(args[1]);
for (int i = 0; i int random = wheel.nextInt(alphaNumberic.length);
System.out.print(alphaNumberic[random]);
}
} else if (args[0].equals("-p")) {
passLength = Integer.parseInt(args[1]);
for (int i = 0; i int random = wheel.nextInt(printableAscii.length);
System.out.print(printableAscii[random]);
}
}
System.out.println();
} else {
System.out.println("Usage: java PasswordGenerator [option] length");
System.out.println(" -l For only lowercase");
System.out.println(" -u For only uppercase");
System.out.println(" -n For only numbers");
System.out.println(" -a For only alphanumeric");
System.out.println(" -p For printable ASCII");
}

}
}