Java密码随机生成工具

 这是一个随机生成,自定义长度,自定义内容,自定义组合的Java密码随机生成工具!


import java.security.SecureRandom;
        import java.util.ArrayList;
        import java.util.List;
/**
 * @Description TODO 密码随机生成工具
 * @Classname pw
 * @Author qingjian.kong
 * @Date 2023/4/12 16:45
 * @Version V1.0
 */

public class PwUtil {

    //随机长度
    private static final int l = 16;
    //是否需要符号
    private static final String Y = "y";
    private static final String N = "n";


    public static void main(String[] args) {
        SecureRandom secureRandom = new SecureRandom();
        List<String> list = new ArrayList<String>();
        StringBuffer sb = new StringBuffer();
        // 循环下找个看的顺眼的
        for (int i = 0; i < 50; i++) {
            orderlyList(list,secureRandom,N,l);
            System.out.println(disorderList(list,secureRandom,sb));
            list.clear();
            sb.setLength(0);
        }
    }

    /**
     * 有序: 数字+小写字母+大写字母+特殊符号
     * @param list
     * @param secureRandom
     */
    private static void orderlyList(List<String> list, SecureRandom secureRandom,String flag,int length) {
//        String[] a = {"0","1","2","3","4","5","6","7","8","9"};
//        String[] b = {"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"};
//        String[] c = {"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"};
//        String[] d = {"~","!","@","#","$","^","&","*","_","+","\\","/","`","|",".","=","-",",","?",":","'","<",">","(",")","%",";","\""};
        String[] f = {"0","1","2","3","4","5","6","7","8","9","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"};
        String[] g = {"0","1","2","3","4","5","6","7","8","9","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","~","!","@","#","$","^","&","*","_","+","\\","/","`","|",".","=","-",",","?",":","'","<",">","(",")","%",";","\""};
        int n = 0;
        if (flag.equals("y")){
            // 数字、小写字母、+大写字母、+特殊符号各四位
            for (int i = 0; i < length; i++) {
//                // 随机的范围
//                String a1 = a[secureRandom.nextInt(a.length)];
//                String b1 = b[secureRandom.nextInt(b.length)];
//                String c1 = c[secureRandom.nextInt(c.length)];
//                String d1 = d[secureRandom.nextInt(d.length)];
//                list.add(i+n,a1);
//                n++;
//                list.add(i+n,b1);
//                n++;
//                list.add(i+n,c1);
//                n++;
//                list.add(i+n,d1);
                // 随机的范围
                String g1 = g[secureRandom.nextInt(g.length)];
                list.add(i+n,g1);
            }
        }else {
            // 数字、小写字母、+大写字母、+特殊符号各四位
            for (int i = 0; i < length; i++) {
                // 随机的范围
                String f1 = f[secureRandom.nextInt(f.length)];
                list.add(i+n,f1);
            }
        }

    }

    /**
     * 无序
     * @param list
     * @param secureRandom
     * @param sb
     * @return
     */
    private static StringBuffer disorderList(List<String> list, SecureRandom secureRandom, StringBuffer sb) {
        int size = list.size();
        for (int i = 0; i < size; i++) {
            int index = secureRandom.nextInt(list.size());
            String str = list.get(index);
            sb.append(str);
            list.remove(index);
        }
        return sb;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花言巧语大佩奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值