所以我一直在研究这个密码强度检查器,并在输入密码时向用户提供分数分解的视觉反馈,我使用 KeyTyped 事件,然后分析字符串并最终开始给出分数达到最小长度。以下是分析的一部分:
if (in.matches("[a-z]+")){
lowerPenalty = -15;
}
if (in.matches("[0-9]+")){
numPenalty = -15;
}
for(int i=0;i<inLen;i++){
if ((in.charAt(i) + "").matches("[A-Z]")){
upperCounter++;
upperBonus = upperBonus + 4;
}
但是,当我运行程序时,它不考虑用户输入的密码的最后一个字符,因此相应的计数器不会增加。这是屏幕截图:
正如您在上面的屏幕截图中看到的,Number Bonus 行中的 numCounter 仍然是 '1' 而不是 '2'。我试过使用 KeyPressed 事件,但问题仍然存在。
请帮忙。
根据要求,这是 keyListener 代码:
input.addKeyListener(new KeyAdapter(){
@Override
public void keyTyped(KeyEvent e1){
if ((int)e1.getKeyChar() == 8){
count = -1;
baseScore = 0;
lenBonus = 0;
upperBonus = 0;
upperCounter = 0;
numBonus = 0;
numCounter = 0;
symBonus = 0;
symCounter = 0;
comBonus = 0;
lowerPenalty = 0;
numPenalty = 0;
comBonus = 0;
totalScore = 0;
input.setText("");
strength_lbl.setText("Enter a random password");
strength_lbl.setBackground(Color.LIGHT_GRAY);
}
count++;
Counter.setText(count+"");
analyzeStr(input.getText());
baseScore_txt.setText(baseScore+"" );
lowerPen_txt.setText(lowerPenalty+"");
numonlyPen_txt.setText(numPenalty+"");
upperBonus_txt.setText(upperBonus+" [" + (upperCounter) + "x4]");
numBonus_txt.setText(numBonus+" [" + numCounter + "x5]");
symBonus_txt.setText(symBonus+" [" + symCounter + "x5]");
comBonus_txt.setText(comBonus+"");
totalScore = baseScore + lenBonus + upperBonus + numBonus + symBonus + comBonus + lowerPenalty + numPenalty;
totalScore_txt.setText(totalScore+"");
if (totalScore>=1 && totalScore<50){
strength_lbl.setText("Weak!");
strength_lbl.setBackground(Color.red);
}
if (totalScore>=50 && totalScore<75){
strength_lbl.setText("Average!");
strength_lbl.setBackground(Color.orange);
}
if (totalScore>=75 && totalScore<100 ){
strength_lbl.setText("Strong!");
strength_lbl.setBackground(Color.cyan);
}
if (totalScore>=100){
strength_lbl.setText("Secure!");
strength_lbl.setBackground(Color.green);
}
}
});
根据要求,这是 analyzeString 方法:
public void analyzeStr(String str){
String in = input.getText();
int inLen = input.getText().length();
if (count == 1){
strength_lbl.setBackground(Color.RED);
strength_lbl.setText("At least 8 characters please!");
}
if (input.getText().length()<8){
lengthBonus_txt.setText("0");
}
else{
lengthBonus_txt.setText(lenBonus +" [" + (count-8) + "x3]");
}
if (count==8){
baseScore = 50;
if (in.matches("[a-z]+")){
lowerPenalty = -15;
}
if (in.matches("[0-9]+")){
numPenalty = -15;
}
for(int i=0;i<inLen;i++){
if ((in.charAt(i) + "").matches("[A-Z]")){
upperCounter++;
upperBonus = upperBonus + 4;
}
if ((in.charAt(i) + "").matches("[0-9]")){
numCounter++;
numBonus = numBonus + 5;
}
if ((in.charAt(i) + "").matches("[!,@,#,$,%,^,&,*,?,_,~]")){
symCounter++;
symBonus = symBonus + 5;
}
}
}
if (count>8){
lenBonus = lenBonus + 3;
lengthBonus_txt.setText(lenBonus+" [" + (inLen-7) + "x3]");
if ((in.charAt(inLen-1) + "").matches("[A-Z]")){
upperCounter++;
upperBonus = upperBonus + 4;
}
if ((in.charAt(inLen-1) + "").matches("[0-9]")){
numCounter++;
numBonus = numBonus + 5;
}
if ((in.charAt(inLen-1) + "").matches("[!,@,#,$,%,^,&,*,?,_,~]")){
symCounter++;
symBonus = symBonus + 5;
}
}
if (count>=8){
if (in.matches("[A-Z][0-9][!,@,#,$,%,^,&,*,?,_,~]")){
comBonus = 25;
}
if (in.matches("[0-9][A-Z][!,@,#,$,%,^,&,*,?,_,~]")){
comBonus = 25;
}
if (in.matches("[!,@,#,$,%,^,&,*,?,_,~][0-9][A-Z]")){
comBonus = 25;
}
if (in.matches("[!,@,#,$,%,^,&,*,?,_,~][A-Z][0-9]")){
comBonus = 25;
}
if (in.matches("[!,@,#,$,%,^,&,*,?,_,~][A-Z][0-9]")){
comBonus = 25;
}
if (in.matches("[A-Z][!,@,#,$,%,^,&,*,?,_,~][0-9]")){
comBonus = 25;
}
if (in.matches("[0-9][!,@,#,$,%,^,&,*,?,_,~][A-Z]")){
comBonus = 25;
}
}
}