所以我一直在研究这个密码强度检查器,并在输入密码时向用户提供点的细分的可视化反馈,我使用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;
}
}
}