Java에서 비밀번호 및 OTP 생성
당신은 일회용 비밀번호 또는 고유 식별 URL 생성 Java에서 비밀번호와 OTP를 생성하는 방법을 더 잘 이해하려면 이 기사 이전에 기사를 읽어보세요.
'비밀번호 찾기'를 클릭하고 이메일이나 전화로 즉시 새 비밀번호나 OTP를 받은 적이 있습니까? 이 프로세스에서는 동적으로 생성된 비밀번호와 일회용 비밀번호(OTP)를 사용하여 보안을 강화합니다. 이 기사에서는 간단한 기술을 사용하여 Java에서 안전한 비밀번호와 OTP를 생성하는 방법을 배웁니다.
비밀번호와 OTP란 무엇입니까?
비밀번호: 로그인 중 신원을 확인하는 데 사용되는 정적 비밀 문자열입니다.
OTP(일회용 비밀번호): 확인을 위해 한 번 사용되는 무작위로 생성된 임시 코드입니다(종종 2FA에서).
방법 1:
비밀번호 생성을 설명하는 Java 프로그램
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 ( & quot ; Generating password using random () : & quot ;); System . out . print ( & quot ; Your new password is : & quot ;); // 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 = & quot ; ABCDEFGHIJKLMNOPQRSTUVWXYZ & quot ;; String Small_chars = & quot ; abcdefghijklmnopqrstuvwxyz & quot ;; String numbers = & quot ; 01234567 89 & quot ;; String symbols = & quot ; ! @# $ %^& amp ; * _ =+-/ . ?& lt ; & gt ;) & quot ;; 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 & lt ; 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(One Time Password) 생성을 설명하는 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 ( & quot ; Generating OTP using random () : & quot ;); System . out . print ( & quot ; You OTP is : & quot ;); // Using numeric values String numbers = & quot ; 01234567 89 & quot ;; // Using random method Random rndm_method = new Random (); char [] otp = new char [ len ] ; for ( int i = 0 ; i & lt ; 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 ( 10 15 )) + Math . pow ( 10 15 )); 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 =& quot ; & quot ;; 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 & lt ; = 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 ( & quot ; unique password =& quot ; + unique_password ); } } // Here we are using random() method of util // class in Java메모 : 우리가 생성하는 비밀번호는 매번 변경됩니다. 비밀번호를 생성하기 위해 random() 메소드를 사용했습니다.
출력 :
Generating password using Math.random() and ascii code:
Your new password is : KHe%ZBT$ASCII 코드 테이블:
![]()
OTP(One Time Password) 생성을 설명하는 Java 프로그램
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 ( & quot ; OTP : & quot ; + otpSting ); } } // Here we are using Math.random() function. // class in Java메모 : 우리가 생성하는 OTP는 매번 변경됩니다. Math.random() 함수를 사용하여 OTP를 생성했습니다.
출력 :
Generating OTP using random() :
You OTP is : 5291퀴즈 만들기