All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Generate Random Password Java

Last Updated : Mar 11, 2024

Generate Random Password Java

In this article we will show you the solution of generate random password java, creating a secure password out of a random and pseudorandom sequence of characters for user authentication and other security needs is known as "generating a password" in Java.

During this procedure, the generated password may be subject to a number of criteria or restrictions, including a minimum/maximum length, specified character types (such as uppercase, lowercase, numerals, and special characters), as well as other security concerns.

Java frequently uses built-in functions or libraries for password generation that have the capabilities needed to generate secure, strong passwords.

Step By Step Guide On Generate Random Password Java :-

import org.passay.CharacterRule;
import org.passay.EnglishCharacterData;
import org.passay.PasswordGenerator;
public class GeneratePasswordExample1 {
    public static void main(String args[]) {
        String pass = generateSecurePassword();
        System.out.println("The created password via Passay is:"+pass);
    }
    public static String generateSecurePassword() {
        CharacterRule LCR = new CharacterRule(EnglishCharacterData.LowerCase);
        LCR.setNumberOfCharacters(2);
        CharacterRule UCR = new CharacterRule(EnglishCharacterData.UpperCase);
        UCR.setNumberOfCharacters(2);
        CharacterRule DR = new CharacterRule(EnglishCharacterData.Digit);
        DR.setNumberOfCharacters(2);
        CharacterRule SR = new CharacterRule(EnglishCharacterData.Special);
        SR.setNumberOfCharacters(2);
        PasswordGenerator passGen = new PasswordGenerator();
        String password = passGen.generatePassword(8, SR, LCR, UCR, DR);
        return password;
    }
}
  1. Here, you can see how we create a Java programme that creates a strong password with the Passay library, a well-liked Java framework for password generation and validation.
  2. Three classes from the Passay library are imported at the beginning of the code: CharacterRule, EnglishCharacterData, & PasswordGenerator.
  3. These classes offer the features required for creating strong passwords.
  4. The Main Method is then defined. The program's starting point is the main method.
  5. When the programme is run, it is called.
  6. The generateSecurePassword() method is used to create a secure password in this method, and System.out.println() is used to print the produced password to the console.
  7. The generateSecurePassword() Method is then defined.
  8. The safe password is generated using this technique. LCR for lowercase characters, UCR for uppercase characters, DR for numbers, and SR for special characters are the first four CharacterRule objects that are defined.
  9. The amount of characters was then set.
  10. Each CharacterRule object has a setNumberOfCharacters() function that may be used to indicate the number of characters from each character type that should be used in the generated password.
  11. The generated password will have two lowercase letters, two uppercase characters, two numerals, and two special characters.
  12. Next, a PasswordGenerator Object is created.
  13. Using new PasswordGenerator(), a PasswordGenerator class object is produced.
  14. The password will be created using this object.
  15. The password is then generated.
  16. Four CharacterRule objects that were previously specified are passed as arguments along with the required password length (in this case, 8), when the generatePassword() function on the PasswordGenerator object is invoked.
  17. It creates a password and returns it as a string based on the rules specified by the CharacterRule objects.
  18. The generateSecurePassword() method then returns the produced password as a string.
  19. In the main method, System.out.println() is used to print the created password to the console.

Conclusion :-

As a result, we were able to understand the concept of generating random passwords in Java.

A secure password can be generated using the Passay library by specifying rules for the character types that must be used in the password and utilising a PasswordGenerator object to create the password in accordance with those rules.

After that, the console is printed with the generated password.

I hope this article on generate random password java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪