package passwordgenerate;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Random;
/**
* 密码生成器
* @author Jason jasonwei06@hotmail.com
* @version 1.0
*/
public class Main {
int intPassLength = 0; //密码长度
String arrayString[] = new String[62]; //候选字符数组
/**
* 生成密码
* @param intPassLength 密码长度
* @return
*/
public String doGenerate(final int intPassLength) {
int intTemp;
String strPassword = "";
Random rand = new Random();
for(int i = 0; i < intPassLength; i++) {
intTemp = rand.nextInt(63);
strPassword += this.arrayString[intTemp];
}
return strPassword;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
final Main Main = new Main();
//确定密码长度
while(true) {
System.out.print("请输入密码长度[8-64]:");
final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String inputThreadAmount = null;
try {
inputThreadAmount = br.readLine();
if(inputThreadAmount.matches("^[0-9]+$")) {
Main.intPassLength = Integer.parseInt(inputThreadAmount);
if(Main.intPassLength >= 8 && Main.intPassLength <= 64) {
break;
} else {
System.out.println("长度超出范围,请输入8至64之间的数字!");
}
} else {
System.out.println("输入格式有误!请输入8至64之间的数字!");
}
} catch (final Exception e) {
e.getMessage();
}
}
//数字
Main.arrayString[0] = "0";
Main.arrayString[1] = "1";
Main.arrayString[2] = "2";
Main.arrayString[3] = "3";
Main.arrayString[4] = "4";
Main.arrayString[5] = "5";
Main.arrayString[6] = "6";
Main.arrayString[7] = "7";
Main.arrayString[8] = "8";
Main.arrayString[9] = "9";
//小写字母
Main.arrayString[10] = "a";
Main.arrayString[11] = "b";
Main.arrayString[12] = "c";
Main.arrayString[13] = "d";
Main.arrayString[14] = "e";
Main.arrayString[15] = "f";
Main.arrayString[16] = "g";
Main.arrayString[17] = "h";
Main.arrayString[18] = "i";
Main.arrayString[19] = "j";
Main.arrayString[20] = "k";
Main.arrayString[21] = "l";
Main.arrayString[22] = "m";
Main.arrayString[23] = "n";
Main.arrayString[24] = "o";
Main.arrayString[25] = "p";
Main.arrayString[26] = "q";
Main.arrayString[27] = "r";
Main.arrayString[28] = "s";
Main.arrayString[29] = "t";
Main.arrayString[30] = "u";
Main.arrayString[31] = "v";
Main.arrayString[32] = "w";
Main.arrayString[33] = "x";
Main.arrayString[34] = "y";
Main.arrayString[35] = "z";
//大写字母
Main.arrayString[36] = "A";
Main.arrayString[37] = "B";
Main.arrayString[38] = "C";
Main.arrayString[39] = "D";
Main.arrayString[40] = "E";
Main.arrayString[41] = "F";
Main.arrayString[42] = "G";
Main.arrayString[43] = "H";
Main.arrayString[44] = "I";
Main.arrayString[45] = "J";
Main.arrayString[46] = "K";
Main.arrayString[47] = "L";
Main.arrayString[48] = "M";
Main.arrayString[49] = "N";
Main.arrayString[50] = "O";
Main.arrayString[51] = "P";
Main.arrayString[52] = "Q";
Main.arrayString[53] = "R";
Main.arrayString[54] = "S";
Main.arrayString[55] = "T";
Main.arrayString[56] = "U";
Main.arrayString[57] = "V";
Main.arrayString[58] = "W";
Main.arrayString[59] = "X";
Main.arrayString[60] = "Y";
Main.arrayString[61] = "Z";
String strPassword = Main.doGenerate(Main.intPassLength);
System.out.print("本次生成的密码为:" + strPassword);
}
}