English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Программа на C ++ для проверки强度的 пароля

Given a string input containing password characters, the task is to check the strength of the password.

The strength of the password is when you tell you whether the password is easy to guess or cracked. The strength should vary from weak, moderate, and strong. To check the strength, we must check the following points:

  • The length of the password must be at least 8 characters.

  • It must contain 1 lowercase letter.

  • It must contain 1 uppercase letter

  • It must contain a digit

  • It must contain a special character, such as: !@#$%^&*()> <, .+ =-

Just like a password “w3codebox” is easy to guess, so we can conclude that the password “weak” given by him is because it only contains lowercase letters, while the password “w3codebox @ 863!” has both uppercase and lowercase letters, digits, and special characters, and is strong, and the length is greater than 8 characters, so it meets all the conditions to make the password stronger.

If some passwords meet more than half of the characteristics of strong passwords, then we will consider the password to be moderate. Like the password “w3codebox12”, it will be considered moderate because it contains lowercase letters, a digit, and its length is greater than 8 characters.

Пример

Input: w3codebox!@12
Output: password strength: -Strong
Explanation: The password has 1 lowercase, 1 uppercase, 1 special character, more than 8 characters long and a digit, hence the password is strong.
Input: w3codebox
Output: password strength: -Weak

We will use the method to solve the given problem-

  • Output the string as the password.

  • Check if all the factors can be used to judge the strength of the password in the password.

  • Print the strength of the password based on the factors.

Algorithm

Start
   Step 1 ⇒ In function void printStrongNess(string& input)
      Declare and initialize n = input.length()
      Declare bool hasLower = false, hasUpper = false
      Declare bool hasDigit = false, specialChar = false
      Declare string normalChars = "abcdefghijklmnopqrstu"
      "vwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 "
      Loop For i = 0 и i < n и i++
         Если (islower(input[i]))
            Set hasLower = true
         Если (isupper(input[i]))
            Set hasUpper = true
         Если (isdigit(input[i]))
            Set hasDigit = true
            Set size_t special = input.find_first_not_of(normalChars)
         Если (special != string::npos)
            Set specialChar = true
      End Loop
      Print "Сила пароля:-"
      Если (hasLower && hasUpper && hasDigit &&
         specialChar && (n >= 8)
         Print "Сильный"
      else if ((hasLower || hasUpper) &&
            specialChar && (n >= 6)
         Print "Средний"
      else
         print "Weak"
   Шаг 2 ⇒ В функции int main() Declare и initialize input = "w3codebox!@12"
      printStrength(input)
Stop

Пример

#include <iostream>
using namespace std;
void printStrength(string& input) {
   int n = input.length();
   // Проверка низких символов строки
   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;
   }
   // Сила пароля
   cout << "Сила пароля:-";
   if (hasLower && hasUpper && hasDigit &&
      specialChar && (n >= 8)
      cout << "Strong" << endl;
   else if ((hasLower || hasUpper) &&
      specialChar && (n >= 6)
      cout << "Moderate" << endl;
   else
      cout << "Weak" << endl;
}
int main() {
   string input = "w3codebox!@12";
   printStrongNess(input);
   return 0;
}

Результат вывода

Сила пароля: - Умеренная
Основной учебник
Рекомендуется к просмотру