C++ 密码强度检查程序
给定一个包含密码字符的字符串输入,任务是检查密码的强度。
密码强度是指判断密码是否容易被猜测或破解。强度应从弱、中等和强三个级别区分。为了检查强度,我们必须检查以下几点:
- 密码必须至少 8 个字符长。
- 必须包含 1 个小写字母。
- 必须包含 1 个大写字母
- 必须包含一个数字
- 必须包含一个特殊字符,例如:!@#$%^&*()><,.+=-
例如,密码“tutorialspoint”很容易被猜测,因此我们可以说给定的密码是“弱”的,因为它只包含小写字符,而密码“Tutorialspoint@863!”是强的,因为它同时包含大小写字母、数字和特殊字符,并且长度超过 8 个字符,因此满足了使密码更强大的所有条件。
如果某个密码满足了强密码特征的一半以上,那么我们将该密码视为中等强度。例如,密码“tutorialspoint12”将被视为中等强度,因为它包含小写字母、数字,并且长度大于 8 个字符。
示例
Input: tutoriAlspOint!@12 Output: Strength of password:-Strong Explanation: Password has 1 lowercase, 1 uppercase, 1 special character, more than 8 characters long and a digit, hence the password is strong. Input: tutorialspoint Output: Strength of password:-Weak
**我们将使用的方法来解决给定的问题**:
- 获取密码的字符串输入。
- 检查密码是否满足所有影响密码强度的因素。
- 根据这些因素打印密码的强度。
算法
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 and i < n and i++ If (islower(input[i])) Set hasLower = true If (isupper(input[i])) Set hasUpper = true If (isdigit(input[i])) Set hasDigit = true Set size_t special = input.find_first_not_of(normalChars) If (special != string::npos) Set specialChar = true End Loop Print "Strength of password:-" If (hasLower && hasUpper && hasDigit && specialChar && (n >= 8)) Print "Strong" else if ((hasLower || hasUpper) && specialChar && (n >= 6)) Print "Moderate" else print "Weak" Step 2 ⇒ In function int main() Declare and initialize input = "tutorialspoint!@12" printStrongNess(input) Stop
示例
#include <iostream>
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;
}
int main() {
string input = "tutorialspoint!@12";
printStrongNess(input);
return 0;
}输出
Strength of password:-Moderate
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP