Generate a strong password in Java

To generate a strong password in Java, you can use a combination of random characters, numbers, and symbols. 


Here's an example program that generates a strong password:

import java.security.SecureRandom;

public class StrongPasswordGenerator {
    private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    private static final String NUMBERS = "0123456789";
    private static final String SYMBOLS = "!@#$%^&*()-_=+[]{}<>?";

    public static void main(String[] args) {
        int length = 10; // Length of the password

        String password = generateStrongPassword(length);
        System.out.println("Generated Password: " + password);
    }

    public static String generateStrongPassword(int length) {
        StringBuilder password = new StringBuilder();
        SecureRandom random = new SecureRandom();

        // Generate random characters
        for (int i = 0; i < length / 3; i++) {
            int randomIndex = random.nextInt(CHARACTERS.length());
            password.append(CHARACTERS.charAt(randomIndex));
        }

        // Generate random numbers
        for (int i = 0; i < length / 3; i++) {
            int randomIndex = random.nextInt(NUMBERS.length());
            password.append(NUMBERS.charAt(randomIndex));
        }

        // Generate random symbols
        for (int i = 0; i < length / 3; i++) {
            int randomIndex = random.nextInt(SYMBOLS.length());
            password.append(SYMBOLS.charAt(randomIndex));
        }

        // Shuffle the password characters
        for (int i = 0; i < length; i++) {
            int randomIndex = random.nextInt(password.length());
            int swapIndex = random.nextInt(password.length());
            char temp = password.charAt(randomIndex);
            password.setCharAt(randomIndex, password.charAt(swapIndex));
            password.setCharAt(swapIndex, temp);
        }

        return password.toString();
    }
}


In this example, the generateStrongPassword method takes an input length and generates a strong password of that length. The password is built by randomly selecting characters, numbers, and symbols from predefined strings. The password is then shuffled to provide better randomness.


You can modify the length variable in the main method to specify the desired length of the generated password.


Please note that this example provides a basic approach to generating a strong password. Depending on your specific requirements, you may need to incorporate additional rules such as minimum length, inclusion of uppercase and lowercase letters, and avoidance of easily guessable patterns.