密码强度等级(HJ87)

C++代码如下:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s="";
    while (getline(cin, s))
    {
        int val = 0;
        int len = s.size();

        if (len <= 4)
        {
            val += 5;
        }
        else if (len <= 7)
        {
            val += 10;
        }
        else
        {
            val += 25;
        }

        int hasNum = 0;
        int hasAlphaS = 0;
        int hasAlphaL = 0;
        int hasCha = 0;

        for (char c : s)
        {
            if (c >= '0' && c <= '9')
                hasNum++;
            else if (c >= 'a' && c <= 'z')
                hasAlphaS++;
            else if (c >= 'A' && c <= 'Z')
                hasAlphaL++;
            else
                hasCha++;
        }

        if (hasAlphaS == len || hasAlphaL == len)
            val += 10;
        else if (hasAlphaS > 0 && hasAlphaL > 0)
            val += 20;

        if (hasNum == 1)
            val += 10;
        else if (hasNum > 1)
            val += 20;

        if (hasCha == 1)
            val += 10;
        else if (hasCha > 1)
            val += 25;

        if (hasAlphaS > 0 && hasAlphaL > 0 && hasNum > 0 && hasCha > 0)
            val += 5;
        else if ((hasAlphaS > 0 || hasAlphaL > 0) && hasNum > 0 && hasCha > 0)
            val += 3;
        else if ((hasAlphaL > 0 || hasAlphaS > 0) && hasCha > 0)
            val += 2;

        if (val >= 90)
            cout << "VERY_SECURE" << endl;
        else if (val >= 80)
            cout << "SECURE" << endl;
        else if (val >= 70)
            cout << "VERY_STRONG" << endl;
        else if (val >= 60)
            cout << "STRONG" << endl;
        else if (val >= 50)
            cout << "AVERAGE" << endl;
        else if (val >= 25)
            cout << "WEAK" << endl;
        else
            cout << "VERY_WEAK" << endl;
    }

    return 0;
}

 

posted @ 2020-07-30 15:43  repinkply  阅读(622)  评论(0)    收藏  举报