java生成随机密码

import java.security.SecureRandom;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class RandomPasswordGenerator {
    private static final String LOWERCASE_CHARACTERS = "abcdefghijklmnopqrstuvwxyz";
    private static final String UPPERCASE_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String NUMBERS = "0123456789";
    private static final String SPECIAL_CHARACTERS = "!@#$%^&*()-_=+[]{}|;:,.<>?";

    public static String generatePassword(int length) {
        List<String> characterSets = Arrays.asList(
                LOWERCASE_CHARACTERS,
                UPPERCASE_CHARACTERS,
                NUMBERS,
                SPECIAL_CHARACTERS
        );

        StringBuilder password = new StringBuilder();
        Random random = new SecureRandom();

        for (int i = 0; i < length; i++) {
            String characterSet = characterSets.get(random.nextInt(characterSets.size()));
            char randomCharacter = characterSet.charAt(random.nextInt(characterSet.length()));
            password.append(randomCharacter);
        }

        return password.toString();
    }

    public static void main(String[] args) {
        int passwordLength = 10;
        String generatedPassword = generatePassword(passwordLength);
        System.out.println("Generated Password: " + generatedPassword);
    }
}

你可能感兴趣的:(java,随机密码)