• Java随机获取32位密码且必须包含大小写字母、数字和特殊字符,四种的任意三种


    Java随机获取32位密码且必须包含大小写字母、数字和特殊字符,四种的任意三种

    Java随机获取32位密码且必须包含大小写字母、数字和特殊字符,四种的任意三种,代码如下:

    import java.util.Random;
    
    public class GetRandomPwd{
        
        /**
         * @Title: getRandomPwd
         * @Description:获取制定长度的密码,包含大小写字母、数字和特殊字符,四种的任意三种
         * @param len
         * @return String
         * @throws
         */
        public static String getRandomPwd(int len) {
            String result = null;
            while(len==32){
                result = makeRandomPwd(len);
                if (result.matches(".*[a-z]{1,}.*") && result.matches(".*[A-Z]{1,}.*") && result.matches(".*\d{1,}.*") && result.matches(".*[~;<@#:>%^]{1,}.*")) {
                    return result;
                } 
                result = makeRandomPwd(len);
            }
            return "长度不得少于32位!";
        }
        
        /**
         * @Title: makeRandomPwd
         * @Description:随机密码生成
         * @param len
         * @return String
         * @throws
         */
        public static String makeRandomPwd(int len) {
            char charr[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890~;<@#:>%^".toCharArray();
            StringBuilder sb = new StringBuilder();
            Random r = new Random();
            for (int x = 0; x < len; ++x) {
                sb.append(charr[r.nextInt(charr.length)]);
            }
            return sb.toString();
        }
        
        
        public static void main(String[] args) {
            String password = getRandomPwd(32);
            System.out.println(">>>:"+password);
        }
    }
  • 相关阅读:
    java堆溢出和栈溢出
    一张图看懂JVM
    java学习书籍
    Linux内核、mysql内核、Tcp/Ip内核、java等知识书籍
    mac 下安装mysql8.0
    软件各种系统架构图
    架构师必看:软件架构图的艺术
    JVM
    docker 镜像整理
    自定义ResultMap查询,这里的关联写法只能用于不分页
  • 原文地址:https://www.cnblogs.com/lizm166/p/7833503.html
Copyright © 2020-2023  润新知