Java 生成随机密码

利用Random方法生产随机密码。
将字母,数字,符号拆开成可取用的数组,按照密码长度做生成策略, 分别为小写字母占密码长度的1/2,大写字母和数字各占1/4,符号占剩余无法除尽的(最多两个符号)。
然后循环取值,每一次从数组中随机拿取字符,并把这个字符随机放入生产的密码字符串中的某个位置。


public final static String[] LOWER_CASES = { "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"};
public final static String[] UPPER_CASES = { "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"};
public final static String[] NUMS_LIST = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
public final static String[] SYMBOLS_ARRAY = {"!","~","^","_","*"};

/**
 * 生成随机密码
 * @param pwd_len 密码长度
 * @return 密码的字符串
 */
public static String genRandomPwd(int pwd_len) {
    if ( pwd_len < 6 || pwd_len >20 ) {
        return "";
    }
    int lower = pwd_len / 2 ;

    int upper = (pwd_len - lower) /2;

    int num =  (pwd_len - lower) / 2;

    int symbol = pwd_len - lower - upper - num ;
    
    StringBuffer pwd = new StringBuffer();
    Random random = new Random();
    int position = 0;
    while((lower + upper + num + symbol) > 0 ){
        if(lower > 0){
            position = random.nextInt(pwd.length() + 1);

            pwd.insert(position,LOWER_CASES[random.nextInt(LOWER_CASES.length)]);
            lower--;
        }
        if(upper > 0){
            position = random.nextInt(pwd.length()  + 1);

            pwd.insert(position,UPPER_CASES[random.nextInt(UPPER_CASES.length)]);
            upper--;
        }
        if(num > 0){
            position = random.nextInt(pwd.length()  + 1);

            pwd.insert(position,NUMS_LIST[random.nextInt(NUMS_LIST.length)]);
            num--;
        }
        if(symbol > 0){
            position = random.nextInt(pwd.length()  + 1);

            pwd.insert(position,SYMBOLS_ARRAY[random.nextInt(SYMBOLS_ARRAY.length)]);
            symbol--;
        }

        System.out.println(pwd.toString());
    }


    return pwd.toString();
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值