가능한 중복 :
Hide input on command line스캐너 입력 텍스트를 모호하게 만드는 방법은 무엇입니까?
내가 암호 보안 검사 프로그램을 만들고있어 내 문제는 내 프로그램이 잘 실행되는 홀수이다. 내가 궁금해하는 점은 암호 필드에서와 같이 콘솔에 입력 된 텍스트를 표시하는 방법이 있는지 여부입니다. 즉, 사용자가 리턴 키를 누르기 전에 입력 된 단어는 "****"로 표시됩니다.
저는 JFrame
에 JPasswordField
메서드가 있지만, 단지 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;
}
}
을 메아리로 콘솔로부터 패스워드 또는 패스 프레이즈를 읽어하는 프롬프트 포맷 제공 ? Id = 79 – jdevelop
Java에서 문자별로 문자를 읽을 수는 없으며 줄 단위로만 읽을 수 있습니다. 당신이 할 수있는 일은 검정색 바탕에 검정색으로 텍스트의 색깔을 바꾸는 것입니다. –
다음은 예입니다. http://stackoverflow.com/questions/10819469/hide-input-on-command-line – axcdnt