2012-08-22 3 views
4

가능한 중복 :
Hide input on command line스캐너 입력 텍스트를 모호하게 만드는 방법은 무엇입니까?

내가 암호 보안 검사 프로그램을 만들고있어 내 문제는 내 프로그램이 잘 실행되는 홀수이다. 내가 궁금해하는 점은 암호 필드에서와 같이 콘솔에 입력 된 텍스트를 표시하는 방법이 있는지 여부입니다. 즉, 사용자가 리턴 키를 누르기 전에 입력 된 단어는 "****"로 표시됩니다.

저는 JFrameJPasswordField 메서드가 있지만, 단지 Scanner을 사용할 때 도움이된다고 생각합니다.

import java.util.Scanner; 

public class SecurityCheckerMain { 

    static String enteredPassword; 

    public static void main(String[] args) { 

     Scanner input = new Scanner(System.in); 
     System.out.println("Please enter your password: "); 
     enteredPassword = input.nextLine(); 
     Checker tc = new Checker(enteredPassword); 
     tc.checkSecurity(); 

    } 

} 

public class Checker { 

    //number of chars : the Higher the Better("password must be greater than 8 chars"){done} 
    //combination of uppercase and lowercase{done} 
    //contains numbers{done} 
    //non repeated characters (every char is different ascii char){done} 
    //is not a consecutive password ie 123456789 or 987654321{done} 
    //is not blank ("[space]"){done} 


    int pLength; 
    final int MAX_STRENGTH = 10; 
    int pStrength = 0; 
    String pass; 

    public Checker(String pwd){ 
     pass = pwd; 
     pLength = pwd.length(); 

    } 

    public void checkSecurity(){ 
     if(pass.isEmpty()){ 
      System.out.println("Password Field is Empty! Password is Very Insecure."); 
     } 
     if(pLength >= 8){ 
      pStrength++; 
      if(pLength >= 12){ 
       pStrength++; 
       if(pLength >= 16){ 
        pStrength++; 
       } 
      } 
     } 
     if(hasUpperCase(pass) && hasLowerCase(pass)){ 
      pStrength+=2; 
     } 
     if(containsNumbers(pass)){ 
      pStrength+=2; 
     } 
     if(hasNoRepeats(pass)){ 
      pStrength+=2; 
     } 
     if(!containsConsecutiveNums(pass)){ 
      pStrength++; 
     } 

     System.out.println("Your password strength is rated at " + pStrength +"/" + MAX_STRENGTH); 

    } 


    //Component Methods 

    public boolean hasUpperCase(String str){ 
     for(int i = 0; i<pLength; i++){ 
      if(Character.isUpperCase(str.charAt(i))){ 
       return true; 
      } 
     } 
     return false; 

    } 

    public boolean hasLowerCase(String str){ 
     for(int i = 0; i<pLength; i++){ 
      if(Character.isUpperCase(str.charAt(i))){ 
       return true; 
      } 
     } 
     return false; 
    } 

    public boolean containsNumbers(String str){ 
     for(int i = 0; i<pLength; i++){ 
      if(Character.isDigit(str.charAt(i))){ 
       return true; 
      } 
     } 
     return false; 
    } 

    public boolean hasNoRepeats(String str){ 
     for(int i = 0; i<pLength; i++) 
      if(containsChar(str, str.charAt(i))){ 
       return false; 
      } 
     return true; 
    } 


    public boolean containsChar(String s, char search) { 
     if (s.length() == 0) 
      return false; 
     else 
      return s.charAt(0) == search || containsChar(s.substring(1), search); 
    } 

    public boolean containsConsecutiveNums(String str){ 
     for(int i = 0; i<pLength; i++){ 
      if(Character.isDigit(str.charAt(i))){ 
       if(str.charAt(i)-1 == str.charAt(i-1) || str.charAt(i)+1 == str.charAt(i+1)){ 
        return true; 
       } 
      } 
     } 
     return false; 
    } 
} 
+0

을 메아리로 콘솔로부터 패스워드 또는 패스 프레이즈를 읽어하는 프롬프트 포맷 제공 ? Id = 79 – jdevelop

+0

Java에서 문자별로 문자를 읽을 수는 없으며 줄 단위로만 읽을 수 있습니다. 당신이 할 수있는 일은 검정색 바탕에 검정색으로 텍스트의 색깔을 바꾸는 것입니다. –

+1

다음은 예입니다. http://stackoverflow.com/questions/10819469/hide-input-on-command-line – axcdnt

답변

9

대신 Console.readPassword를 사용할 수 있습니다

여기 내 코드입니다.

readPassword(String fmt, Object... args) 

는 http://www.javapractices.com/topic/TopicAction.do 장애인

public class SecurityCheckerMain { 

    static String enteredPassword; 

    public static void main(String[] args) { 

     Console console = System.console(); 

     enteredPassword = 
      new String(console.readPassword("Please enter your password: ")); 
     Checker tc = new Checker(enteredPassword); 
     tc.checkSecurity(); 

    } 
+1

답장을 보내 주셔서 감사합니다! 하지만이 코드를 사용하여 프로그램을 실행하면 14 행의 널 포인터 예외가 발생합니다 ... 어떤 아이디어입니까? –

+1

업데이트 : 분명히 이것은 (Eclipse)를 사용하고있는 IDE의 알려진 버그입니다. 추측 병이 그냥 함께 살거나 스윙을 사용하여 –

+0

나는이 코드를 Windows 터미널에서 실행하려고합니다. 하지만 콘솔의 가치는 null입니다! –

관련 문제