java+密码生成器_java做个密码生成器,嘿嘿

pwdGen.java

package j_bin;

import java.util.Random;

public class pwdGen {

private Random rdseed = new Random();

/*

*@param

*length        password length;

*@param

*letters       boolean non-capital letters combination control;

*@param

*letters_capi  boolean capital letters combination control;

*@param

*numbers       numbers control;

*/

public String getpwd(int length, boolean letters, boolean letters_capi, boolean numbers) {

StringBuilder sb = new StringBuilder();

Random rd = this.rdseed;

while (length-- > 0) {

char _1dgt = 0;

if (letters && letters_capi && numbers) {

int _key = rd.nextInt(3);

if (2 == _key) {

_1dgt = get_L();

} else if (1 == _key) {

_1dgt = get_L_C();

} else if (0 == _key) {

_1dgt = get_N();

}

} else if (letters && letters_capi && !numbers) {

int _key = rd.nextInt(2);

if (1 == _key) {

_1dgt = get_L();

} else if (0 == _key) {

_1dgt = get_L_C();

}

} else if (!letters && letters_capi && numbers) {

int _key = rd.nextInt(2);

if (1 == _key) {

_1dgt = get_N();

} else if (0 == _key) {

_1dgt = get_L_C();

}

} else if (letters && !letters_capi && numbers) {

int _key = rd.nextInt(2);

if (1 == _key) {

_1dgt = get_L();

} else if (0 == _key) {

_1dgt = get_N();

}

} else if (letters && !letters_capi && !numbers) {

_1dgt = get_L();

} else if (!letters && !letters_capi && numbers) {

_1dgt = get_N();

} else if (!letters && letters_capi && !numbers) {

_1dgt = get_L_C();

} else {

break;

}

sb.append(_1dgt);

}

return sb.toString();

}

private char get_L_C() {

Random rd = this.rdseed;

int _dgt = (rd.nextInt(26) + 65);

return (char) _dgt;

}

private char get_N() {

Random rd = this.rdseed;

int _dgt = (rd.nextInt(10) + 48);

return (char) _dgt;

}

private char get_L() {

Random rd = this.rdseed;

int _dgt = (rd.nextInt(26) + 97);

return (char) _dgt;

}

public static void main(String[] args) {

pwdGen test = new pwdGen();

int testct = 100;//测试次数

long start = System.nanoTime();

while (testct-- > 0) {

test.getpwd(10, true, true, true);

}

long end = System.nanoTime();

// System.out.println(((double) (end - start)) / (1000000000));

System.out.println(end-start);

System.out.println(test.getpwd(30, true, true, true));

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值