0% found this document useful (0 votes)
172 views

Generating Userid and Password in Java

This document discusses generating passwords and one-time passwords (OTPs) in Java. It provides code examples to generate random passwords of a specified length using uppercase letters, lowercase letters, numbers, and symbols. A similar code example is given to generate random numeric OTPs of a specified length. The random() method from Java's util class is used to randomly select characters for the passwords and OTPs, ensuring they are different each time.

Uploaded by

Radheshyam Nayak
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
172 views

Generating Userid and Password in Java

This document discusses generating passwords and one-time passwords (OTPs) in Java. It provides code examples to generate random passwords of a specified length using uppercase letters, lowercase letters, numbers, and symbols. A similar code example is given to generate random numeric OTPs of a specified length. The random() method from Java's util class is used to randomly select characters for the passwords and OTPs, ensuring they are different each time.

Uploaded by

Radheshyam Nayak
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Hi Team,

#Generating Password and OTP in Java

Many a times we forget our passwords and we opt for Forget password option and
within no time we get a new password at our registered email-ID or phone no. to
login our account. And every time we get a different password.
Sometime we access our bank accounts while shopping from an online store or many
more ways, in order to verify our transition from the bank account they send us
OTP(One Time Password) on our registered phone no. or our email-ID, within no time.
The following code explains how to generate such Passwords and OTP within no time
and what code we can use if in case we need to do so.
Java program explaining the generation of Password

// Java code to explain how to generate random


// password
// Here we are 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(geek_Password(length));
}
// This our Password generating method
// We have use static here, so that we not to
// make any object for it
static char[] geek_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;
}
}
Note : The password we are generating will change every time. As we have used
random() method to generate the password.
Output :
Generating password using random() :
Your new password is : KHeCZBTM;-

Java program explaining the generation of OTP(One Time Password)

// Java code to explain how to generate OTP


// Here we are using random() method of util
// class in Java
import java.util.*;
public class NewClass
{
static char[] OTP(int len)
{
System.out.println("Generating OTP using random() : ");
System.out.print("You OTP is : ");
// Using numeric values
String numbers = "0123456789";
// Using random method
Random rndm_method = new Random();
char[] otp = 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
otp[i] =
numbers.charAt(rndm_method.nextInt(numbers.length()));
}
return otp;
}
public static void main(String[] args)
{
int length = 4;
System.out.println(OTP(length));
}
}
Note :
The OTP we are generating will change every time. As we have used random() method
to generate the OTP.
Output :
Generating OTP using random() :
You OTP is : 5291

You might also like