java如何获取32位密码呢?
下文笔者讲述java获取32位密码,并且密码包含(大小写字母、数字和特殊字符)的方法分享,如下所示
随机密码的实现思路
实现思路: 1.定义一个密码库字符 2.使用随机数---从密码库中获取随机密码 然后使用多次获取密码组合为一个新的密码例
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[] = "abcdefghijklmnopqrstuvwxyzABC DEFGHIJKLMNOPQRSTUVWXYZ1234567890~;<@#:>%^".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) { //生成32位随机密码 String pwd = getRandomPwd(32); System.out.println("pwd:"+pwd); } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。