2017-11-17 4 views
0

암호와 사용자 이름을 두 개의 개별 파일 (그에 따라 이름 지정)에 저장하고 설정할 수있는 로그인 프로그램을 코딩하고 있습니다. 내 문제는 사용자 이름을 암호로 설정하는 방법을 알 수 없다는 것입니다. 예 : 사용자 이름은 Bob이고 암호는 cat이며 사용자는 Bob을 사용자 이름으로 입력하고 cat은 암호로 입력해야합니다. 그렇지 않으면 오류가 발생합니다 (사용자가 bill이라는 사용자 이름과 cat을 암호로 입력하면 cat 사용자 이름은 각각의 암호와 함께 전달되지 않습니다). 어떤 도움이라도 좋을 것입니다. * 사용자 이름 부분을 아직 추가하지 않았습니다. 암호 부분과 동일하게 구성되었으므로 암호와 일치하는 사용자 이름이 필요하므로 사용자가 동일한 암호로 다른 사용자 이름을 사용할 수 없습니다.JAVA 사용자 이름과 로그인 암호를 일치 시키십시오.

여기 내 내가 참조를 위해 지금까지 가지고 코드 :

import java.io.File; 
import java.io.PrintStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.nio.file.Files; 
import java.nio.file.Paths; 
import java.io.FileReader; 
import java.io.*; 
import java.util.*; 
import javax.swing.JOptionPane; 
public class PassCode 
{ 
    static String adminPassword = "Change Password"; 
    public static void fileMaker() throws IOException 
    { 
     PrintStream standard = System.out; 
     File f = new File("Password.txt"); 
     FileOutputStream fs= new FileOutputStream(f); 
     PrintStream ps = new PrintStream(fs); 
     System.setOut(ps); 
     String theMessage = "Set New Password"; 
     String userInput = JOptionPane.showInputDialog(theMessage); 
     System.out.println(userInput); 
     ps.close(); 
     System.setOut(standard); 
    } 
public static void Checker() throws IOException 
    { 
     Scanner inputStream = new Scanner(new FileReader("Password.txt")); //Scans declared file for text on the first line 
     String fileChecker = inputStream.nextLine(); //Sets scanned line into a string variable 
     if(fileChecker.isEmpty()) 
     { 
      fileMaker(); 
     } 
     else 
     { 
     int reply = JOptionPane.showConfirmDialog(null, "Would you like to change the current password", "Warning!", JOptionPane.YES_NO_OPTION); 
     if (reply == JOptionPane.YES_OPTION) 
      { 
       String inquire = "Type Administrator Password to Proceed"; 
       boolean flag = true; 
       while(flag == true) 
      { 
       String confirm = JOptionPane.showInputDialog(inquire); 
      if(confirm.equals(adminPassword)) 
      { 
       fileMaker(); 
       flag = false; 
      } 
      else 
      { 
       inquire = "Incorrect!" + "\n" + "Retry"; 
      } 
      } 
     } 
     } 
    } 
    public static void main(String[] args) throws IOException 
    { 
     Checker(); 
     Scanner inputStreamThree = new Scanner(new FileReader("Password.txt")); 
     String line = inputStreamThree.nextLine(); 
     String question = "Password Please"; 
     Boolean right = true; 
     while(right == true) 
     { 
      String ask = JOptionPane.showInputDialog(question); //Asks for user to input password 
     if(ask.equals(adminPassword)) //Checks if user input the admin password 
     { 
      fileMaker(); 
      Scanner inputStreamAdmin = new Scanner(new FileReader("Password.txt")); //Scans for admin password 
      String adminChecker = inputStreamAdmin.nextLine(); //Sets scanned line as a new string variable 
      line = adminChecker; 
     } 
     else if(line.equals(ask)) //Checks if user password is correct 
     { 
      System.out.println("Welcome Fellow Programmer to the Now Functioning Password Checker!" + 
       "\n" + "Date Today: 10/31/2017" + 
       "\n\n\n\n\n\n\n" + "Did you figure out the Admin password yet?"); 
      right = false; 
     } 
     else if(ask != line) //Checks if user password is incorrect 
      { 
       question = "Incorrect Password!"; 
      } 
     } 
    } 
} 
+0

데 두 개의 서로 다른 파일 요구 사항 : 여기

내가 추가 한 코드합니다 (LNR 그냥 방법)은? –

+0

선호되지만, 다른 방법이 있다면 괜찮을 것입니다. 또한 두 개의 서로 다른 파일을 사용하여 비밀번호와 사용자 이름을 따로 저장하려고했습니다. –

답변

0

모두 사용자 이름과 암호를 각각의 파일에 동일한 행 번호에있는 경우는, 다음은 특정 사용자에 대한 올바른 암호를 공제 할 수 있어야한다.

예 :

Username.txt      Password.txt 
bob        cat 
alice        rabbit 
victor        salmon 

사용자 이름을 검색하면 사용자 이름을 찾을 때까지 읽을 줄을 계산합니다. 그런 다음 암호 파일에서 줄 수를 읽고 암호 값을 검색하십시오. 그럼 비교해라!

비밀 번호를 파일에 저장하는 것이 중요한 보안 허점이므로 전문적인 프로젝트에서 허용해서는 안되기 때문에 학업이라고 가정합니다.

+0

나는 그것을 시도 할 것이다! 그게 내가 찾는 대답 일거야. 추신 이것은 학업입니다. 저는 기본 로그인 프로그램이 어떻게 작동하는지 이해하고 결함에 따라 최적화하는 간단한 프로그램을 만들고 싶었습니다 (이 프로그램은 가르치는 프로그램이므로 매우 분명합니다). –

0

단일 로그인을 사용하지만 Password.txt와 Username.txt의 두 파일에 저장되어 있다고 가정합니다.

아래 10 분이 내 코드입니다. 오타 또는 오류가있는 경우 수정해야 할 수도 있습니다.

import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.util.Scanner; 

import javax.swing.JOptionPane; 

public class PassCodeA { 

    public static void main(String[] args) 
    { 
     Scanner usernameFile = null; 
     try { 
      usernameFile = new Scanner(new FileReader("Username.txt")); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
      JOptionPane.showMessageDialog(null, "Username File Missing", "File Missing", JOptionPane.CLOSED_OPTION); 
      return; 
     } 

     Scanner passwordFile = null; 
     try { 
      passwordFile = new Scanner(new FileReader("Password.txt")); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
      JOptionPane.showMessageDialog(null, "Password File Missing", "File Missing", JOptionPane.CLOSED_OPTION); 
      return; 
     } 

     String usernameInput = JOptionPane.showInputDialog(null,"Please Enter Username", "Username", JOptionPane.OK_CANCEL_OPTION); 
     String passwordInput = JOptionPane.showInputDialog(null,"Please Enter Password", "Username", JOptionPane.OK_CANCEL_OPTION); 
     System.out.println(usernameInput); 

     boolean usernameFound = false; 
     while(usernameFile.hasNextLine()){ 
      final String lineFromFile = usernameFile.nextLine(); 
      if(lineFromFile.equals(usernameInput)){ 
       usernameFound = true; 
       break; 
      } 
     } 
     if (usernameFound == false) { 
      JOptionPane.showMessageDialog(null, "Username not found", "Username Not Found", JOptionPane.CLOSED_OPTION); 
      return; 
     } 

     boolean passwordFound = false; 
     while(passwordFile.hasNextLine()){ 
      final String lineFromFile = passwordFile.nextLine(); 
      if(lineFromFile.equals(passwordInput)){ 
       passwordFound = true; 
       break; 
      } 
     } 

     if (passwordFound == false) { 
      JOptionPane.showMessageDialog(null, "Password not found", "Password Not Found", JOptionPane.CLOSED_OPTION); 
      return; 
     } 

     JOptionPane.showMessageDialog(null, "Thank you for login", "Success", JOptionPane.CLOSED_OPTION); 


    } 



} 

두 파일에 여러 개의 로그인 인증 정보가있는 경우 scanner 대신 LineNumberReader를 사용해야합니다. 두 파일의 줄 번호와 일치시킬 수 있습니다.

+0

나는 또한 그것을 시험해보고, 그것이 결과를 얻는지를 볼 것이다. 귀하의 의견을 보내 주셔서 감사합니다. –

0

LineNumberReader (LNR)을 구현하는 방법을 찾았으며 프로그램에서 사용자 이름과 암호의 줄 번호가 같은지 확인하십시오. 나는 분리 된 방법을 사용하여 main 메소드에서 호출했다. 나는 코드를 테스트했고 두 파일 모두에 여러 사용자 이름 정보와 암호 정보를 사용했습니다. 나는 이것이 엉망인 것을 안다. 그러나 나는 기능성을 먼저 생각하고있다. 그럼 나는 그것을 적절하게 최적화 할 것이다. 나는 주어진 공간을 위해 큰 방법이기 때문에 전체 코드를 업로드 할 수 없다.

public static void reader() throws IOException 
    { 
     JTextField username = new JTextField(); 
     JTextField password = new JPasswordField(); 
     Object[] message = { 
      "Username:", username, 
      "Password:", password 
      }; 

     int option = JOptionPane.showConfirmDialog(null, message, "Login", JOptionPane.OK_CANCEL_OPTION); 


     boolean right = true; 

     while(right == true) 
     { 
     int u = 0; 
     String userCheck; 
     FileReader ur = null; 
     LineNumberReader lnru = null; 

     try { 

     ur = new FileReader("username.txt"); 
     lnru = new LineNumberReader(ur); 

     while ((userCheck = lnru.readLine()) != null) 
     { 
      if (userCheck.equals(username.getText())) 
      { 
       break; 
      } 
      else 
      { 
       u = lnru.getLineNumber(); 
      } 
     } 
     } catch(Exception e) 
     { 
      e.printStackTrace(); 
     } finally { 
       if(ur!=null) 
        ur.close(); 
       if(lnru!=null) 
        lnru.close(); 
      } 

     int p = 0; 
     String passCheck; 
     FileReader pr = null; 
     LineNumberReader lnrp = null; 

     try { 

     pr = new FileReader("Password.txt"); 
     lnrp = new LineNumberReader(pr); 

     while ((passCheck = lnrp.readLine()) != null) 
     { 
      if (passCheck.equals(password.getText())) 
      { 
       break; 
      } 
      else 
      { 
       p = lnrp.getLineNumber(); 
      } 
     } 
    } catch(Exception e) 
     { 
      e.printStackTrace(); 
     } finally { 

      if(pr!=null) 
       pr.close(); 
      if(lnrp!=null) 
       lnrp.close(); 
     } 

     if (option == JOptionPane.OK_OPTION) 
     { 
     if(password.getText().equals(adminPassword)) //Checks if user input the admin password 
     { 
      passMaker(); 
      Scanner inputStreamAdmin = new Scanner(new FileReader("Password.txt")); //Scans for admin password 
      String adminChecker = inputStreamAdmin.nextLine(); //Sets scanned line as a new string variable 
      //lineFromFile = adminChecker; 
     } 
     else if(p == u) //Checks if username and password are correct 
     { 
      System.out.println("Welcome Fellow Programmer to the Now Functioning login program!" + "\n" + "Date Today: 10/31/2017" + "\n\n\n\n\n\n\n" + "Did you figure out the Admin password yet?"); 
      right = false; 
     } 
     else //Checks if user password is incorrect 
      { 
       option = JOptionPane.showConfirmDialog(null, message, "Login Failed Try Again", JOptionPane.OK_CANCEL_OPTION); 
      } 
     } 
    } 
    } 
관련 문제