Developer Tools for Random Data Generation // v2.5.1
root@generate-random:~/passwords/java$ _

Generate Random Passwords in Java

Complete code tutorial with examples and best practices

[ Code Example - Quick Summary ]

Language: Java

What: Generate secure random passwords in Java using <code>SecureRandom</code> for cryptographically secure random generation. Suitable for enterprise applications and security-critical systems.

Try it: Use our interactive Passwords generator or integrate this code into your Java application.

Generate secure random passwords in Java using SecureRandom for cryptographically secure random generation. Suitable for enterprise applications and security-critical systems. Looking for other languages? Check our code examples in PHP , JavaScript , Python , C# , C++ , Ruby and Go or use our interactive web generator.

Java Code Example

import java.security.SecureRandom;

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

    public static String generatePassword(int length, boolean includeSpecial) {
        SecureRandom random = new SecureRandom();
        String chars = LOWERCASE + UPPERCASE + NUMBERS;
        if (includeSpecial) {
            chars += SPECIAL;
        }

        StringBuilder password = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(chars.length());
            password.append(chars.charAt(index));
        }

        return password.toString();
    }

    public static void main(String[] args) {
        // Generate a 16-character password with special characters
        String password = generatePassword(16, true);
        System.out.println(password);  // Example: aB3$xY7!mN2@pQ9&

        // Generate a 12-character password without special characters
        String simplePassword = generatePassword(12, false);
        System.out.println(simplePassword);  // Example: aB3xY7mN2pQ9
    }
}

[EXPLANATION]

SecureRandom provides cryptographically strong random number generation using platform-specific sources of randomness. This implementation uses StringBuilder for efficient string construction.

Expected Output

aB3$xY7!mN2@pQ9&
aB3xY7mN2pQ9

Common Use Cases

  • Spring Boot authentication systems
  • Android app security features
  • Enterprise Java application user management
  • RESTful API security token generation
  • Banking and financial application security
  • OAuth and JWT token generation

Important Notes

  • Always use SecureRandom for password generation
  • StringBuilder is more efficient than string concatenation
  • Consider using Apache Commons Text RandomStringGenerator
  • Validate password complexity requirements

Try Our Interactive Generator

Don't want to write code? Use our free web-based Passwords generator with instant results.

TRY PASSWORDS GENERATOR →