簡體   English   中英

c編程中的密碼驗證或校驗器

[英]password verification or checker in c programming

我在 c 編程中創建了一個密碼檢查器,但它不工作,任何人都可以檢查它並說出這里有什么問題。

#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;
}

我應該使用int還是char來存儲密碼,如果我也使用 int 那部分工作如果 char 也可以工作但有時它不會工作,

這個循環最終只關心每個數組中的最后一個值是否匹配。

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在循環后為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庫 function。

使用固定長度的純整數系列作為“密碼”是不典型的,但對於玩具程序來說這很好。


這是一個要研究的示例程序。

#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.");

}

由於您沒有具體說明您的問題(除了“它不工作”),我會盡力列出所有可能的問題。

讀取整數

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

將單個小數 integer讀入otp中的 position。 如果密碼是1024 ,第一次通過循環(迭代)會將1024讀入otp[0] 在第二次迭代中, scanf()將等待,直到另一個數字在標准輸入上可用。 一旦可用,它會將其讀入otp[1] ,依此類推。 這個scanf()循環實際上讀取了 4 個不同的整數,用換行符分隔。 對一個 integer 只執行一個scanf()會容易得多,如下所示:

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

您還可以使用char arrays 掃描 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 所解釋的,您的檢查器存在邏輯錯誤。 如果使用 integer,您可以簡單地檢查

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

如果使用char數組(字符串),您可以使用

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

您必須為strcmp() #include <string.h>

標准 output 緩沖器

在刷新stdout緩沖區之前,不會打印“輸入新密碼:”提示。 這通常僅在打印換行符時發生。 你必須

fflush(stdout);

如果您希望它出現,請在打印提示后立即執行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM