logo

Java でのパスワードと OTP の生成

を通過する可能性があります ワンタイムパスワードまたは一意の識別URLを生成します Java でパスワードと OTP を生成する方法をよりよく理解するには、この前の記事を参照してください。 Java でのパスワードと OTP の生成' src='//techcodeview.com/img/java/97/generating-password-and-otp-in-java.webp' title= 

「パスワードを忘れた場合」をクリックすると、電子メールまたは電話で新しいパスワードまたは OTP を即座に受け取ったことがありますか?このプロセスでは、動的に生成されたパスワードとワンタイム パスワード (OTP) を使用してセキュリティを強化します。この記事では、簡単な手法を使用して Java で安全なパスワードと OTP を生成する方法を学びます。

パスワードとOTPとは何ですか?

パスワード: ログイン時に身元を確認するために使用される静的な秘密文字列。



OTP (ワンタイム パスワード): 検証のために 1 回使用される、一時的にランダムに生成されたコード (多くの場合 2FA)。

日付形式.形式

方法 1:

パスワードの生成を説明する Java プログラム  

Android で YouTube 広告をブロックする
Java
// 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;  } } 

注記 : 生成するパスワードは毎回変わります。パスワードの生成にはrandom()メソッドを使用しました。 

出力: 

Generating password using random() :   
Your new password is : KHeCZBTM;-

OTP(ワンタイムパスワード)の生成を説明するJavaプログラム

Java
// 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));  } } 

注記 : 生成される OTP は毎回変わります。 OTP の生成には、random() メソッドを使用しました。 

出力: 

Generating OTP using random() :   
You OTP is : 5291

方法 2:

パスワードの生成を説明する Java プログラム  

Java
// Java code to explain how to generate random // password class uniquePassword { public static long Code() //this code returns the unique 16 digit code  { //creating a 16 digit code using Math.random function  long code =(long)((Math.random()*9*Math.pow(1015))+Math.pow(1015));  return code; //returning the code }  //method to generate the password  //by converting every two digits as an ascii value of a character public static void main(String args[])  { long code=Code();//function calling String unique_password=""; for (long i=code;i!=0;i/=100)//a loop extracting 2 digits from the code   {  long digit=i%100;//extracting two digits  if (digit<=90)  digit=digit+32;   //converting those two digits(ascii value) to its character value  char ch=(char) digit;  // adding 32 so that our least value be a valid character   unique_password=ch+unique_password;//adding the character to the string  }  System.out.println("unique password ="+unique_password);  } } // Here we are using random() method of util // class in Java   

注記 : 生成するパスワードは毎回変わります。パスワードの生成にはrandom()メソッドを使用しました。 

文字列連結Java

出力: 

Generating password using Math.random() and ascii code:   
Your new password is : KHe%ZBT$

アスキーコード表:

Java でのパスワードと OTP の生成

OTP(ワンタイムパスワード)の生成を説明するJavaプログラム

PDマージ
Java
// Java code to explain how to generate OTP public class GenerateOTP { //declaring a of return type String //which on calling provides the otp  public static String generateOTP()   { //int randomPin declared to store the otp  //since we using Math.random() hence we have to type cast it int  //because Math.random() returns decimal value  int randomPin =(int) (Math.random()*9000)+1000;  String otp = String.valueOf(randomPin);  return otp; //returning value of otp  }  public static void main(String args[])//method to call and print otp  {  String otpSting =generateOTP();//function calling  System.out.println("OTP : "+otpSting);  } }// Here we are using Math.random() function. // class in Java 

注記 : 生成される OTP は毎回変わります。 Math.random() 関数を使用して OTP を生成しました。 

出力: 

Generating OTP using random() :   
You OTP is : 5291

クイズの作成