Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions aditya1abhinav_random_password.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Java code to explain how to generate random
// password

// Here I am using random() method of util
// class in Java
import java.util.*;

public class NewClass
{
public static void main(String[] args)
{
// Length of your password as I have choose
// here to be 8
int length = 10;
System.out.println(random_Password(length));
}

// This our Password generating method
// I have use static here, so that we not to
// make any object for it
static char[] random_Password(int len)
{
System.out.println("Generating password using random() : ");
System.out.print("Your new password is : ");

// A strong password has Cap_chars, Lower_chars,
// numeric value and symbols. So we are using all of
// them to generate our password
String Capital_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String Small_chars = "abcdefghijklmnopqrstuvwxyz";
String numbers = "0123456789";
String symbols = "!@#$%^&*_=+-/.?<>)";


String values = Capital_chars + Small_chars +
numbers + symbols;

// Using random method
Random rndm_method = new Random();

char[] password = new char[len];

for (int i = 0; i < len; i++)
{
// Use of charAt() method : to get character value
// Use of nextInt() as it is scanning the value as int
password[i] =
values.charAt(rndm_method.nextInt(values.length()));

}
return password;
}
}