I am trying to make a password checker to see whether the password is between 8 – 20 characters, & has an uppercase, but cant seem to figure it out

huangapple go评论105阅读模式
英文:

I am trying to make a password checker to see whether the password is between 8 - 20 characters, & has an uppercase, but cant seem to figure it out

问题

这是你的代码 - 程序运行良好,但是当创建用户时,如果密码仍然只有2个字符,例如,仍然显示为成功注册。 不太确定接下来该怎么做(对编程不太熟悉,这是我第一个独立完成的项目,寻求指导)。(如果代码有点混乱,抱歉)。

void registration() {

    system("cls");
    bool valid = false;
    int minLength = 8;
    int maxLength = 20;

    string regUsername, regPassword, regUser, regPass;

    std::cout << "欢迎来到注册页面!\n";
    std::cout << "请输入用户名:\n";
    std::cin >> regUsername;
    std::cout << "请输入一个8-20字符的密码,其中包含大写和小写字母:\n";
    std::cin >> regPassword;

    if (regPassword.length() >= 8 && regPassword.length() <= 20) {

        for (int k = 0; k < regPassword.length(); k++)
        {
            if (isupper(regPassword[k]))
            {
                valid = true;
            }
        }
    }

    if (valid == true) {
        std::ofstream registerUser("records.txt", std::ios::app); // 打开records.txt以写入用户名和密码。
        registerUser << regUsername << " " << regPassword << std::endl;

        system("cls");
        int choice;
        std::cout << "欢迎" << regUsername << ",您已成功注册!\n";
        std::cout << "如果要登录,请按1,如果要返回主菜单,请按2。\n";
        std::cin >> choice;
        switch (choice)
        {
        case 1: login();
            break;
        case 2: main();
            break;
        default:
            std::cout << "无效的输入,返回主菜单。\n";
            main();
            break;
        }
    }
    else if (valid == false) {
        std::cout << "无效的密码,请再次尝试创建帐户。\n";
        registration();
    }
}

我期望密码会被验证,并检查是否为8-20个字符,并且密码中是否有大写字母。我尝试将 && 更改为 ||,但我不认为问题出在那里。

英文:

This is the code - the programme runs fine but when creating a user, if the password is still 2 characters long for example, it still shows as successful registration. Not exactly sure where to go from here (new to coding and its my first project solo, looking for guidance.) (sorry if its a clusterbomb of code its abit all over the place)

void registration() {

	system(&quot;cls&quot;);
	bool valid = false;
	int minLength = 8;
	int maxLength = 20;

	string regUsername, regPassword, regUser, regPass;

	std::cout &lt;&lt; &quot;Welcome to the registration page!&quot; &lt;&lt; &#39;\n&#39;;
	std::cout &lt;&lt; &quot;Please enter a username: &quot; &lt;&lt; &#39;\n&#39;;
	std::cin &gt;&gt; regUsername;
	std::cout &lt;&lt; &quot;Enter a password that is 8-20 characters long, with an uppercase and lowercase \n&quot;;
	std::cin &gt;&gt; regPassword;


	if (regPassword.length() &gt;= 8 || regPassword.length() &lt;= 20) {

		for (int k = 0; k &lt; regPassword.length(); k++)
		{
			if (isupper(regPassword[k]))
			{
				valid = true;

			}

		}
	}

	if (valid = true) {
		std::ofstream registerUser(&quot;records.txt&quot;, std::ios::app); // open records.txt to write username and password inside.
		registerUser &lt;&lt; regUsername &lt;&lt; &quot; &quot; &lt;&lt; regPassword &lt;&lt; std::endl;


		system(&quot;cls&quot;);
		int choice;
		std::cout &lt;&lt; &quot;Welcome aboard &quot; &lt;&lt; regUsername &lt;&lt; &quot;, your registration has been successful!&quot; &lt;&lt; std::endl;
		std::cout &lt;&lt; &quot;Press 1 if you would like to login, and 2 if you would like to return to the main menu.&quot; &lt;&lt; std::endl;
		std::cin &gt;&gt; choice;
		switch (choice)
		{
		case 1: login();
			break;
		case 2: main();
			break;
		default:
			std::cout &lt;&lt; &quot;Invalid input, returning to main menu.&quot; &lt;&lt; std::endl;
			main();
			break;
		}
	}
	else if (valid = false) {
		std::cout &lt;&lt; &quot;Invalid password, please attempt to create an account again.&quot; &lt;&lt; &#39;\n&#39;;
		registration();
	}
}

I expected the password to be validated and checked for 8-20 characters along with if there is an uppercase within the password. i have tried changing the && to || but i dont think thats where the problem is.

答案1

得分: 1

我已经为您实现了一个处理C++中密码复杂性的简单类。请看一下。

#include <iostream>
#include <string>
#include <regex>

class PasswordConstraint {
private:
    int minLength = 8;  // minLength的默认值
    int maxLength = 20; // maxLength的默认值
    bool requireLowercase = false; // requireLowercase的默认值
    bool requireUppercase = false; // requireUppercase的默认值
    bool requireNumbers = false;   // requireNumbers的默认值
    bool requireSpecialChars = false; // requireSpecialChars的默认值

public:
    int getMinLength() const {
        return minLength;
    }

    void setMinLength(int length) {
        minLength = length;
    }

    int getMaxLength() const {
        return maxLength;
    }

    void setMaxLength(int length) {
        maxLength = length;
    }

    bool isRequireLowercase() const {
        return requireLowercase;
    }

    void setRequireLowercase(bool value) {
        requireLowercase = value;
    }

    bool isRequireUppercase() const {
        return requireUppercase;
    }

    void setRequireUppercase(bool value) {
        requireUppercase = value;
    }

    bool isRequireNumbers() const {
        return requireNumbers;
    }

    void setRequireNumbers(bool value) {
        requireNumbers = value;
    }

    bool isRequireSpecialChars() const {
        return requireSpecialChars;
    }

    void setRequireSpecialChars(bool value) {
        requireSpecialChars = value;
    }

    bool isLengthValid(const std::string& value) const {
        return value.length() >= minLength && value.length() <= maxLength;
    }

    bool hasLowercase(const std::string& value) const {
        return std::regex_search(value, std::regex("[a-z]"));
    }

    bool hasUppercase(const std::string& value) const {
        return std::regex_search(value, std::regex("[A-Z]"));
    }

    bool hasNumbers(const std::string& value) const {
        return std::regex_search(value, std::regex("[0-9]"));
    }

    bool hasSpecialChars(const std::string& value) const {
        return std::regex_search(value, std::regex("[!@#%^&*(),.?\":{}|<>]"));
    }
};

void validatePassword(const std::string& value, const PasswordConstraint& constraint) {
    if (!constraint.isLengthValid(value)) {
        std::cout << "Error: 密码长度应在 " << constraint.getMinLength()
                  << " 到 " << constraint.getMaxLength() << " 之间" << std::endl;
    }

    if (constraint.isRequireLowercase() && !constraint.hasLowercase(value)) {
        std::cout << "Error: 密码应包含至少一个小写字母" << std::endl;
    }

    if (constraint.isRequireUppercase() && !constraint.hasUppercase(value)) {
        std::cout << "Error: 密码应包含至少一个大写字母" << std::endl;
    }

    if (constraint.isRequireNumbers() && !constraint.hasNumbers(value)) {
        std::cout << "Error: 密码应包含至少一个数字" << std::endl;
    }

    if (constraint.isRequireSpecialChars() && !constraint.hasSpecialChars(value)) {
        std::cout << "Error: 密码应包含至少一个特殊字符" << std::endl;
    }
}

int main() {
    std::string password;
    std::cout << "请输入密码: ";
    std::cin >> password;

    PasswordConstraint constraint;
    constraint.setMinLength(8);
    constraint.setMaxLength(20);
    constraint.setRequireLowercase(true);
    constraint.setRequireUppercase(true);
    constraint.setRequireNumbers(true);
    constraint.setRequireSpecialChars(true);

    validatePassword(password, constraint);

    return 0;
}

您可以自定义validatePassword函数的输出格式。它可以返回布尔值而不是简单地输出到控制台,或者尝试抛出异常,并在稍后的地方捕获异常。

英文:

I've implemented for you a simple class that deals with password complexity in c++. Please take a look.

#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;regex&gt;

class PasswordConstraint {
private:
    int minLength = 8;  // Default value for minLength
    int maxLength = 20; // Default value for maxLength
    bool requireLowercase = false; // Default value for requireLowercase
    bool requireUppercase = false; // Default value for requireUppercase
    bool requireNumbers = false;   // Default value for requireNumbers
    bool requireSpecialChars = false; // Default value for requireSpecialChars

public:
    int getMinLength() const {
        return minLength;
    }

    void setMinLength(int length) {
        minLength = length;
    }

    int getMaxLength() const {
        return maxLength;
    }

    void setMaxLength(int length) {
        maxLength = length;
    }

    bool isRequireLowercase() const {
        return requireLowercase;
    }

    void setRequireLowercase(bool value) {
        requireLowercase = value;
    }

    bool isRequireUppercase() const {
        return requireUppercase;
    }

    void setRequireUppercase(bool value) {
        requireUppercase = value;
    }

    bool isRequireNumbers() const {
        return requireNumbers;
    }

    void setRequireNumbers(bool value) {
        requireNumbers = value;
    }

    bool isRequireSpecialChars() const {
        return requireSpecialChars;
    }

    void setRequireSpecialChars(bool value) {
        requireSpecialChars = value;
    }

    bool isLengthValid(const std::string&amp; value) const {
        return value.length() &gt;= minLength &amp;&amp; value.length() &lt;= maxLength;
    }

    bool hasLowercase(const std::string&amp; value) const {
        return std::regex_search(value, std::regex(&quot;[a-z]&quot;));
    }

    bool hasUppercase(const std::string&amp; value) const {
        return std::regex_search(value, std::regex(&quot;[A-Z]&quot;));
    }

    bool hasNumbers(const std::string&amp; value) const {
        return std::regex_search(value, std::regex(&quot;[0-9]&quot;));
    }

    bool hasSpecialChars(const std::string&amp; value) const {
        return std::regex_search(value, std::regex(&quot;[!@#%^&amp;*(),.?\&quot;:{}|&lt;&gt;]&quot;));
    }
};

void validatePassword(const std::string&amp; value, const PasswordConstraint&amp; constraint) {
    if (!constraint.isLengthValid(value)) {
        std::cout &lt;&lt; &quot;Error: Password should have a minimum length of &quot; &lt;&lt; constraint.getMinLength()
                  &lt;&lt; &quot; and a maximum length of &quot; &lt;&lt; constraint.getMaxLength() &lt;&lt; std::endl;
    }

    if (constraint.isRequireLowercase() &amp;&amp; !constraint.hasLowercase(value)) {
        std::cout &lt;&lt; &quot;Error: Password should contain at least one lowercase letter&quot; &lt;&lt; std::endl;
    }

    if (constraint.isRequireUppercase() &amp;&amp; !constraint.hasUppercase(value)) {
        std::cout &lt;&lt; &quot;Error: Password should contain at least one uppercase letter&quot; &lt;&lt; std::endl;
    }

    if (constraint.isRequireNumbers() &amp;&amp; !constraint.hasNumbers(value)) {
        std::cout &lt;&lt; &quot;Error: Password should contain at least one number&quot; &lt;&lt; std::endl;
    }

    if (constraint.isRequireSpecialChars() &amp;&amp; !constraint.hasSpecialChars(value)) {
        std::cout &lt;&lt; &quot;Error: Password should contain at least one special character&quot; &lt;&lt; std::endl;
    }
}

int main() {
    std::string password;
    std::cout &lt;&lt; &quot;Enter a password: &quot;;
    std::cin &gt;&gt; password;

    PasswordConstraint constraint;
    constraint.setMinLength(8);
    constraint.setMaxLength(20);
    constraint.setRequireLowercase(true);
    constraint.setRequireUppercase(true);
    constraint.setRequireNumbers(true);
    constraint.setRequireSpecialChars(true);

    validatePassword(password, constraint);

    return 0;
}

You can customize the format of validatePassword function. It can return boolean instead of a simple output to the console or try to throw exception and catch it somewhere later.

huangapple
  • 本文由 发表于 2023年7月7日 02:11:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定