📜  检查密码强度的程序

📅  最后修改于: 2022-05-13 01:57:07.645000             🧑  作者: Mango

检查密码强度的程序

如果满足以下标准,则称密码为密码:

  1. 它至少包含一个小写英文字符。
  2. 它至少包含一个大写英文字符。
  3. 它至少包含一个特殊字符。特殊字符为: !@#$%^&*()-+
  4. 它的长度至少为 8。
  5. 它至少包含一位数字。

给定一个字符串,求它的强度。让强密码满足上述所有条件。中等密码是满足前三个条件且长度至少为 6 的密码。否则密码为周。

例子 :

C++
// C++ program to check if a given password is
// strong or not.
#include 
using namespace std;
 
void printStrongNess(string& input)
{
    int n = input.length();
 
    // Checking lower alphabet in string
    bool hasLower = false, hasUpper = false;
    bool hasDigit = false, specialChar = false;
    string normalChars = "abcdefghijklmnopqrstu"
        "vwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ";
 
    for (int i = 0; i < n; i++) {
        if (islower(input[i]))
            hasLower = true;
        if (isupper(input[i]))
            hasUpper = true;
        if (isdigit(input[i]))
            hasDigit = true;
 
        size_t special = input.find_first_not_of(normalChars);
        if (special != string::npos)
            specialChar = true;
    }
 
    // Strength of password
    cout << "Strength of password:-";
    if (hasLower && hasUpper && hasDigit &&
        specialChar && (n >= 8))
        cout << "Strong" << endl;
    else if ((hasLower || hasUpper) &&
             specialChar && (n >= 6))
        cout << "Moderate" << endl;
    else
        cout << "Weak" << endl;
}
 
// Driver code
int main()
{
    string input = "GeeksforGeeks!@12";
    printStrongNess(input);
    return 0;
}


Java
// Java implementation for the above approach
import java.io.*;
import java.util.*;
 
class GFG {
   
   
    public static void printStrongNess(String input)
    {
        // Checking lower alphabet in string
        int n = input.length();
        boolean hasLower = false, hasUpper = false,
                hasDigit = false, specialChar = false;
        Set set = new HashSet(
            Arrays.asList('!', '@', '#', '$', '%', '^', '&',
                          '*', '(', ')', '-', '+'));
        for (char i : input.toCharArray())
        {
            if (Character.isLowerCase(i))
                hasLower = true;
            if (Character.isUpperCase(i))
                hasUpper = true;
            if (Character.isDigit(i))
                hasDigit = true;
            if (set.contains(i))
                specialChar = true;
        }
       
        // Strength of password
        System.out.print("Strength of password:- ");
        if (hasDigit && hasLower && hasUpper && specialChar
            && (n >= 8))
            System.out.print(" Strong");
        else if ((hasLower || hasUpper || specialChar)
                 && (n >= 6))
            System.out.print(" Moderate");
        else
            System.out.print(" Weak");
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String input = "GeeksforGeeks!@12";
        printStrongNess(input);
    }
 
     
}
// contributed by Ashish Chhabra


Python3
# Python3 program to check if a given
# password is strong or not.
def printStrongNess(input_string):
     
    n = len(input_string)
 
    # Checking lower alphabet in string
    hasLower = False
    hasUpper = False
    hasDigit = False
    specialChar = False
    normalChars = "abcdefghijklmnopqrstu"
    "vwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 "
     
    for i in range(n):
        if input_string[i].islower():
            hasLower = True
        if input_string[i].isupper():
            hasUpper = True
        if input_string[i].isdigit():
            hasDigit = True
        if input_string[i] not in normalChars:
            specialChar = True
 
    # Strength of password
    print("Strength of password:-", end = "")
    if (hasLower and hasUpper and
        hasDigit and specialChar and n >= 8):
        print("Strong")
         
    elif ((hasLower or hasUpper) and
          specialChar and n >= 6):
        print("Moderate")
    else:
        print("Weak")
 
# Driver code
if __name__=="__main__":
     
    input_string = "GeeksforGeeks!@12"
     
    printStrongNess(input_string)
 
# This code is contributed by Yash_R


输出
Strength of password:-Strong