VOOZH about

URL: https://www.geeksforgeeks.org/java/generating-password-otp-java/

⇱ Generating Password and OTP in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Generating Password and OTP in Java

Last Updated : 21 Jun, 2025

You may go through the generate a one time password or unique identification url article before this for a better understanding of how to generate passwords and OTP in Java.👁 Generating Password and OTP in Java

Have you ever clicked on "Forgot Password" and received a new password or OTP instantly on your email or phone? This process uses dynamically generated passwords and one-time passwords (OTPs) to enhance security. In this article, you'll learn how to generate secure passwords and OTP's in Java using simple techniques.

What are Passwords and OTPs?

Password: A static secret string used to verify identity during login.

OTP (One-Time Password): A temporary, randomly generated code used once for verification (often in 2FA).

Method 1:

Java program explaining the generation of 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)

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

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:

👁 Image

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

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
Comment
Article Tags:
Article Tags: