C 编程中的密码验证或检查器

发布于 2025-01-11 22:09:03 字数 1492 浏览 4 评论 0原文

我用 C 编程创建了一个密码检查器,但它不起作用,任何人都可以检查它并说出其中的问题。‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎ 如果我使用 int,我应该使用 int 还是 char 来存储密码如果 char 也可以,那么该部分也可以工作,但有时不会 工作,

#include<stdio.h>
#include<stdbool.h>

int main() {
    int otp[4];   //array for storing the true password entered by user at first
    int pto[4];   //array for storing password for login
    int count = 4,i;
    bool pass = true;

    printf("enter a new password:  ");

    for (int i = 0; i < count; i++) {
        scanf("%d", & otp[i]);  //for storing the true password
    }

    printf("\n\n --- Login page --- ");
    printf("\nenter your password : ");

    for (i = 0; i < count; i++) {
        scanf(" %d", & pto[i]);   //asking for password for login
    }

    for (i = 0; i < count; i++) {   //check for password
        if (otp[i] == pto[i]) {
            pass = true;
        } else {
            pass = false;
        }
    }

    while (pass == false) {     //if password is wrong
        printf("\n---- password din't match ----\nenter your password again : ");

        for (i = 0; i < count; i++) {
            scanf(" %d", & pto[i]);
        }

        for (i = 0; i < count; i++) {
            if (otp[i] == pto[i]) {
                pass = true;
            } else {
                pass = false;
            }
        }
    }


    printf("\n Your password is correct!");

    return 0;
}

I have created a password checker in c programming but it is not working can anyone please check it and say what is wrong in this.‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎

#include<stdio.h>
#include<stdbool.h>

int main() {
    int otp[4];   //array for storing the true password entered by user at first
    int pto[4];   //array for storing password for login
    int count = 4,i;
    bool pass = true;

    printf("enter a new password:  ");

    for (int i = 0; i < count; i++) {
        scanf("%d", & otp[i]);  //for storing the true password
    }

    printf("\n\n --- Login page --- ");
    printf("\nenter your password : ");

    for (i = 0; i < count; i++) {
        scanf(" %d", & pto[i]);   //asking for password for login
    }

    for (i = 0; i < count; i++) {   //check for password
        if (otp[i] == pto[i]) {
            pass = true;
        } else {
            pass = false;
        }
    }

    while (pass == false) {     //if password is wrong
        printf("\n---- password din't match ----\nenter your password again : ");

        for (i = 0; i < count; i++) {
            scanf(" %d", & pto[i]);
        }

        for (i = 0; i < count; i++) {
            if (otp[i] == pto[i]) {
                pass = true;
            } else {
                pass = false;
            }
        }
    }


    printf("\n Your password is correct!");

    return 0;
}

And should I use int or char to store passwords,if i use int also that part works if char also it works but sometimes it wont work,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞慾 2025-01-18 22:09:03

该循环最终只关心每个数组中的最后一个值是否匹配。

for (i = 0; i < count; i++) {
    if (otp[i] == pto[i]) {
        pass = true;
    } else {
        pass = false;
    }
}

例如,比较 { 1, 2, 3, 4 }{ 4, 4, 4, 4 } 将导致 pass 为 < code>true 在循环之后,尽管存在明显的差异。

相反,请将标志设置为 false,并在发生不匹配时立即从循环中break

bool matching = true;

for (size_t i = 0; i < length; i++) {
    if (array_one[i] != array_two[i]) {
        matching = false;
        break;
    }
}

如果从未发生不匹配,则此后该标志将保持 true


通常密码是在存储之前经过哈希处理的文本(使用 salt)。密码验证是通过比较哈希值来完成的。例如,查看 man 3 crypt库函数。

使用固定长度的一系列普通整数作为“密码”是不典型的,但对于玩具程序来说这是很好的。


这是一个可供研究的示例程序。

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#define KEY_LENGTH 4

void get_key(int *key, size_t length) {
    for (size_t i = 0; i < length; i++) {
        if (1 != scanf("%d", &key[i])) {
            fprintf(stderr, "Could not read integer input.\n");
            exit(EXIT_FAILURE);
        }
    }
}

bool match_key(int *one, int *two, size_t length) {
    for (size_t i = 0; i < length; i++)
        if (one[i] != two[i])
            return false;

    return true;
}

int main(void) {
    int key[KEY_LENGTH];
    int user_key[KEY_LENGTH];

    printf("Set the key (%d integers): ", KEY_LENGTH);
    get_key(key, KEY_LENGTH);

    puts("--- LOGIN ---");

    while (1) {
        printf("Enter the key (%d integers): ", KEY_LENGTH);
        get_key(user_key, KEY_LENGTH);

        if (match_key(key, user_key, KEY_LENGTH))
            break;

        puts("Key mismatch. Retrying...");
    }

    puts("Welcome to the system.");

}

This loop ultimately only cares if the last value in each array match or not.

for (i = 0; i < count; i++) {
    if (otp[i] == pto[i]) {
        pass = true;
    } else {
        pass = false;
    }
}

For example, comparing { 1, 2, 3, 4 } and { 4, 4, 4, 4 } would result in pass being true after the loop, despite the obvious differences.

Instead, set the flag to false, and break from your loop as soon as a mismatch occurs.

bool matching = true;

for (size_t i = 0; i < length; i++) {
    if (array_one[i] != array_two[i]) {
        matching = false;
        break;
    }
}

If a mismatch never occurs, the flag will remain true afterwards.


Usually passwords are text that is hashed (with a salt) before being stored. Password verification is done by comparing hashes. For example, take a look at the man 3 crypt library function.

The use of a fixed-length series of plain integers for a 'password' is atypical, but for a toy program it is fine.


Here is an example program to study.

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#define KEY_LENGTH 4

void get_key(int *key, size_t length) {
    for (size_t i = 0; i < length; i++) {
        if (1 != scanf("%d", &key[i])) {
            fprintf(stderr, "Could not read integer input.\n");
            exit(EXIT_FAILURE);
        }
    }
}

bool match_key(int *one, int *two, size_t length) {
    for (size_t i = 0; i < length; i++)
        if (one[i] != two[i])
            return false;

    return true;
}

int main(void) {
    int key[KEY_LENGTH];
    int user_key[KEY_LENGTH];

    printf("Set the key (%d integers): ", KEY_LENGTH);
    get_key(key, KEY_LENGTH);

    puts("--- LOGIN ---");

    while (1) {
        printf("Enter the key (%d integers): ", KEY_LENGTH);
        get_key(user_key, KEY_LENGTH);

        if (match_key(key, user_key, KEY_LENGTH))
            break;

        puts("Key mismatch. Retrying...");
    }

    puts("Welcome to the system.");

}
迟到的我 2025-01-18 22:09:03

由于您没有具体说明您的问题(除了“它不起作用”),我将尽力列出所有可能的问题。

读取整数

scanf("%d", & otp[i]);

将单个十进制整数读取到otp中的某个位置。如果密码是 1024,第一次循环(迭代)会将 1024 读入 otp[0]。在第二次迭代中,scanf() 将等待,直到标准输入上有另一个数字可用。一旦可用,它会将其读入otp[1],依此类推。这个 scanf() 循环实际上读取了 4 个不同的整数,并用换行符分隔。对于一个整数只执行一次 scanf() 会容易得多,如下所示:

int main() {
    int otp;
    int pto;
    bool pass = true;
    
    printf("enter a new password:  ");
    
    scanf("%d", &otp);

您还可以使用 char 数组扫描 4 个字符的字符串:

int main() {
    char otp[5]; //4 digits and 1 NUL-terminator
    char pto[5];
    bool pass = true;
    
    printf("enter a new password:  ");
    
    scanf("%4s", otp);

密码检查逻辑错误

正如@Oka 所解释的,你的检查器有一个逻辑错误。如果使用整数,您可以简单地检查

if (opt == pto) {
    //correct
} else {
    //incorrect
}

如果使用char数组(字符串),您可以使用

if (!strcmp(otp, pto)) {
    //correct
} else {
    //incorrect
}

您必须#include for strcmp()

标准输出缓冲区

在刷新 stdout 缓冲区之前,不会打印“输入新密码:”提示。这通常仅在打印换行符时发生。 则必须在打印提示后立即执行。

fflush(stdout);

如果您希望显示提示,

Since you didn’t specify your problem (besides “it’s not working”), I’ll do my best to list all the possible issues.

Reading integers

scanf("%d", & otp[i]);

Will read a single decimal integer into a position in otp. If the password is 1024, the first time through the loop (iteration) will read 1024 into otp[0]. In the second iteration, scanf() will wait until another number is available on standard input. Once it’s available, it will read it into otp[1], and so on. This scanf() loop really reads in 4 different integers, separated by newlines. It would be much easier to do only one scanf() for one integer, like this:

int main() {
    int otp;
    int pto;
    bool pass = true;
    
    printf("enter a new password:  ");
    
    scanf("%d", &otp);

You could also scan a 4-character string by using char arrays:

int main() {
    char otp[5]; //4 digits and 1 NUL-terminator
    char pto[5];
    bool pass = true;
    
    printf("enter a new password:  ");
    
    scanf("%4s", otp);

Password-checking logic error

As @Oka explained, your checker has a logic error. If using an integer, you could simply check

if (opt == pto) {
    //correct
} else {
    //incorrect
}

If using a char array (string), you could use

if (!strcmp(otp, pto)) {
    //correct
} else {
    //incorrect
}

You would have to #include <string.h> for strcmp().

Standard output buffer

The “enter a new password: ” prompt is not printed until the stdout buffer is flushed. This usually only happens when a newline is printed. You have to

fflush(stdout);

right after printing the prompt if you want it to appear.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文