密碼強度等級

題目描述

密碼按以下規則進行計分,並根據不一樣的得分爲密碼進行安全等級劃分。
1、密碼長度:
    5 分: 小於等於4 個字符
    10 分: 5 到7 字符
    25 分: 大於等於8 個字符
    2、字母:
    0 分: 沒有字母
    10 分: 全都是小(大)寫字母
    20 分: 大小寫混合字母
    3、數字:
    0 分: 沒有數字
    10 分: 1 個數字
    20 分: 大於1 個數字
    4、符號:
    0 分: 沒有符號
    10 分: 1 個符號
    25 分: 大於1 個符號
    5、獎勵:
    2 分: 字母和數字
    3 分: 字母、數字和符號
    5 分: 大小寫字母、數字和符號
    最後的評分標準:
    >= 90: 很是安全
    >= 80: 安全(Secure)
    >= 70: 很是強
    >= 60: 強(Strong)
    >= 50: 通常(Average)
    >= 25: 弱(Weak)
    >= 0:  很是弱

對應輸出爲:
    VERY_WEAK,
    WEAK,
    AVERAGE,
    STRONG,
    VERY_STRONG,
    SECURE,
    VERY_SECURE

請根據輸入的密碼字符串,進行安全評定。
    注:
    字母:a-z, A-Z
    數字:-9
    符號包含以下: (ASCII碼錶能夠在UltraEdit的菜單view->ASCII Table查看)
    !"#$%&'()*+,-./     (ASCII碼:x21~0x2F)
    :;<=>?@             (ASCII<=><=><=><=><=>碼:x3A~0x40)
    [\]^_`              (ASCII碼:x5B~0x60)
    {|}~                (ASCII碼:x7B~0x7E)

接口描述:

Input Param : String pPasswordStr:    密碼,以字符串方式存放。
Return Value : 根據規則評定的安全等級。
public static Safelevel GetPwdSecurityLevel(String pPasswordStr) {
    /*在這裏實現功能*/
    return null;
}

輸入描述

輸入一個string的密碼

輸出描述

輸出密碼等級

輸入例子

38$@NoNoNo

輸出例子

VERY_SECURE

算法實現

import java.util.Scanner;

/**
 * Declaration: All Rights Reserved !!!
 */
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
//        Scanner scanner = new Scanner(Main.class.getClassLoader().getResourceAsStream("data.txt"));
        while (scanner.hasNext()) {
            String s = scanner.nextLine();
            System.out.println(getPwdSecurityLevel(s));
        }

        scanner.close();
    }

    private static String getPwdSecurityLevel(String s) {
        int len = s.length();
        int num = 0;
        int lowerCase = 0;
        int upperCase = 0;
        int ch = 0;
        int score = 0;

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);

            if (c >= '0' && c <= '9') {
                num++;
            } else if (c >= 'A' && c <= 'Z') {
                upperCase++;
            } else if (c >= 'a' && c <= 'z') {
                lowerCase++;
            } else if (c >= 0x21 && c <= 0x2F || c >= 0x3A && c <= 0x40
                    || c >= 0X5B && c <= 0x60 || c >= 0x7B && c <= 0x7E) {
                ch++;
            }
        }

        // 1、密碼長度
        if (len <= 4) {
            score += 5;
        } else if (len <= 7) {
            score += 10;
        } else {
            score += 25;
        }

        // 2、字母
        if (lowerCase > 0) {
            score += 10;
        }

        if (upperCase > 0) {
            score += 10;
        }

        // 3、數字
        if (num == 1) {
            score += 10;
        } else if (num > 1) {
            score += 20;
        }

        // 4、符號
        if (ch == 1) {
            score += 10;
        } else if (ch > 1) {
            score += 25;
        }

        if (num > 0 && (upperCase > 0 || lowerCase > 0)) {
            score += 2;
            if (ch > 0) {
                score += 1;

                if (upperCase > 0 && lowerCase > 0) {
                    score += 2;
                }
            }
        }

        if (score >= 90) {
            return "VERY_SECURE";
        } else if (score >= 80) {
            return "SECURE";
        } else if (score >= 70) {
            return "VERY_STRONG";
        } else if (score > 60) {
            return "STRONG";
        } else if (score >= 50) {
            return "AVERAGE";
        } else if (score >= 25) {
            return "WEAK";
        } else {
            return "VERY_WEAK";
        }
    }
}
相關文章
相關標籤/搜索