Java Password Generator
Generally, we must create a strong password for security reasons. In Java, there are numerous strategies for creating secure passwords. We will learn how to create a strong password in this part, one that has at least two lowercase, two uppercase, two numerals, and two special characters.
The methods for creating a password in Java are as follows:
Method 1:
RandomPassword.java
//this program is for creating the password in java
//importing the packages required
import java.util.*;
//A class RandomPassword is created for creating the password
public class RandomPassword
{
public static void main(String[] args)
{
// The length of the required password is mentioned
// by declaring it with the len variable
int len = 10;
System.out.println(random_Password(len));
}
// Now, the method is for password creation
// here, the method is used as static because we should want to create an object
static char[] random_Password(int len)
{
System.out.println("The password is creating by using the random() method : ");
System.out.print("The password generated is : ");
//To secure the password, the password must be long, and it should consist of
//capitals, numerics, and also some other special characters
String Capital_letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String Small_letters = "abcdefghijklmnopqrstuvwxyz";
String number = "0123456789";
String symbol = "!@#$%^&*_=+-/.?<>)";
String value = Capital_letters + Small_letters +
number + symbol;
// the method used is random() for random passwords
Random rndmmethod = new Random();
char[] passwords = new char[len];
for (int i = 0; i < len; i++)
{
//The charAt() method was used for getting the character list
// the method nextInt() is used for reading the interger values
passwords[i] =
value.charAt(rndmmethod.nextInt(value.length()));
}
return passwords;
}
}
Output:

Method 2:
Making use of SecureRandom. using StringBuilder and nextInt() Method
Making a string of the necessary length out of a random selection of characters from the chosen ASCII range is a straightforward approach. To create a random alphabetic password, the ASCII range must include numbers, uppercase, and lowercase characters.
An easy Java application to illustrate the concept is provided below. To provide a cryptographically robust random number generator, the SecureRandom class was required instead of the Random class.
Password.java
// this program is for generating random
//import section
import java.security.SecureRandom;
public class Password
{
//length of the password is declared
public static String RandomPassword(int length)
{
// A string of required characters is declared
final String characters ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
SecureRandom rand = new SecureRandom();
StringBuilder stringbuild = new StringBuilder();
// for every iteration, it will select the random character
// it will, according to the StringBuilder class
for (int i = 0; i < length; i++)
{
int randomIndex = rand.nextInt(characters.length());
stringbuild.append(characters.charAt(randomIndex));
}
return stringbuild.toString();
}
public static void main(String[] args)
{
int length=10;
System.out.println(RandomPassword(length));
}
}
Output

Method 3
Employing Stream and SecureRandom.ints
Java 8 and later versions include the SecureRandom.ints(...) method, which may be used to effectively produce a stream of pseudorandom numbers that fall inside the given range. Using the filter(...) Stream function, you may limit the produced pseudorandom numbers to alphanumeric values. Calling the limit(...) function will force the filtering alphabetic pseudorandom numbers to stay inside the predetermined length. After that, create a String by gathering all the data in the output stream.
Random.java
//This program is for generating the random password
import java.security.SecureRandom;
public class Random
{
// Method to generate a random alphanumeric password of a specific length
public static String generatePassword(int length, int randomOrigin, int randomBound)
{
SecureRandom ranm = new SecureRandom();
return ranm.ints(randomOrigin, randomBound + 1)
.filter(i -> Character.isAlphabetic(i) || Character.isDigit(i))
.limit(length)
.collect(StringBuilder::new, StringBuilder::appendCodePoint,
StringBuilder::append)
.toString();
}
public static void main(String[] args)
{
int length= 10;
int randomOrigin = 48, randomBound = 122;
System.out.println(generatePassword(length, randomOrigin, randomBound));
}
}
Output

Method 4
RandomPassword.java
//Java source code demonstrating how to create a random password
//import section
import java.io.*;
import java.util.*;
public class RandomPassword
{
public static long Codes()
{
long codes =(long)((Math.random()*9*Math.pow(10,15))+Math.pow(10,15));
return codes; // the code is then returned
}
//The method is used for declaration of the method
//The every string is then converted to the required ascii values
public static void main(String args[])
{
long codes=Codes();// the function is then called for the password
String uniquepassword="";
for (long i=codes;i!=0;i/=100)//loop is used for iterating for every two characters
{
long digits=i%100;// from the entire digits two digits are then extracted
if (digits<=90)
digits=digits+32;
// the passed two digits is then converted to ascii values
char character=(char) digits;
// adding the value (32) for the validation
uniquepassword=character+uniquepassword;//adding the character to the string
}
System.out.println("The suggested strong password is= "+uniquepassword);
}
}
Output
