Generate a Secure Random Password in Java with Minimum Special Character Requirements

后端 未结 4 1917
暖寄归人
暖寄归人 2021-02-04 10:35

How do I create a random password that meets the system\'s length and character set requirements in Java?

I have to create a random password that is 10-14 characters lo

4条回答
  •  春和景丽
    2021-02-04 11:01

    Using the random functionality of java.util package of rt.jar, we can create a random password of any length. below is the snippet for the same.

    public class GeneratePassword {
    
    public static void main(String[] args)
    {
            int length = 10;
            String symbol = "-/.^&*_!@%=+>)"; 
            String cap_letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
            String small_letter = "abcdefghijklmnopqrstuvwxyz"; 
            String numbers = "0123456789"; 
    
    
            String finalString = cap_letter + small_letter + 
                    numbers + symbol; 
    
            Random random = new Random(); 
    
            char[] password = new char[length]; 
    
            for (int i = 0; i < length; i++) 
            { 
                password[i] = 
                        finalString.charAt(random.nextInt(finalString.length())); 
    
            } 
            System.out.println(password);
    }
    

    }

提交回复
热议问题