Generating Password and OTP in Java
You may go through Generate a One Time Password or Unique Identification URL article before this for better understanding.
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. Sometimes we access our bank accounts while shopping from an online store or many more ways, in order to verify our transactions 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.
Method 1:
Java program explaining the generation of Password
Java
import java.util.*;
public class NewClass
{
public static void main(String[] args)
{
int length = 10 ;
System.out.println(geek_Password(length));
}
static char [] geek_Password( int len)
{
System.out.println("Generating password using random() : ");
System.out.print("Your new password is : ");
String Capital_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String Small_chars = "abcdefghijklmnopqrstuvwxyz";
String numbers = " 0123456789 ";
String symbols = "!@#$%^&*_=+-/.?<>)";
String values = Capital_chars + Small_chars +
numbers + symbols;
Random rndm_method = new Random();
char [] password = new char [len];
for ( int i = 0 ; i < len; i++)
{
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
import java.util.*;
public class NewClass
{
static char [] OTP( int len)
{
System.out.println("Generating OTP using random() : ");
System.out.print("You OTP is : ");
String numbers = " 0123456789 ";
Random rndm_method = new Random();
char [] otp = new char [len];
for ( int i = 0 ; i < len; i++)
{
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
Method 2:
Java program explaining the generation of Password
Java
class uniquePassword
{
public static long Code()
{
long code =( long )((Math.random()* 9 *Math.pow( 10 , 15 ))+Math.pow( 10 , 15 ));
return code;
}
public static void main(String args[])
{
long code=Code();
String unique_password="";
for ( long i=code;i!= 0 ;i/= 100 )
{
long digit=i% 100 ;
if (digit<= 90 )
digit=digit+ 32 ;
char ch=( char ) digit;
unique_password=ch+unique_password;
}
System.out.println("unique password ="+unique_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 Math.random() and ascii code:
Your new password is : KHe%ZBT$
ascii code table:
Java program explaining the generation of OTP(One Time Password)
Java
public class GenerateOTP {
public static String generateOTP()
{
int randomPin =( int ) (Math.random()* 9000 )+ 1000 ;
String otp = String.valueOf(randomPin);
return otp;
}
public static void main(String args[])
{
String otpSting =generateOTP();
System.out.println("OTP : "+otpSting);
}
}
|
Note : The OTP we are generating will change every time. As we have used Math.random() function to generate the OTP.
Output :
Generating OTP using random() :
You OTP is : 5291
Last Updated :
28 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...