4

我应该怎么做才能让它继续循环,直到它至少有一个大写,小写和数字?

我卡住了,真的卡住了……

char password[100][15];
i=1;
     printf("Password [3..10]: ");
        gets(password[i]);
        while (strlen(password[i])>10 || strlen(password[i])<3 || ) {   
        do{
        printf("  Password must contain at least 1 uppercase, 1 lowercase, and 1 number\nPassword [3..10]: ");
        gets(password[i]);
        } while (strlen(password[i])>10 || strlen(password[i])<3 );
4

5 回答 5

10

这应该有效:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int is_legal(char *p) {
  int number = 0, lower = 0, upper = 0, length = 0;
  for( ; *p; p++) {
      number += isdigit(*p);   
      lower  += islower(*p);     
      upper  += isupper(*p);
      length++;
  }
  return number > 0 && lower > 0 && upper > 0  && length > 3 && length < 10;
}

char *my_gets(char *buf, int bufsize, FILE *file) {
    if(fgets(buf, bufsize, file) == 0) {
        return 0;
    }
    int n = strlen(buf);
    if(buf[n-1] == '\n') buf[n-1] = 0;
    return buf;
}

int get_password(char *buf, int bufsize, FILE *file) {
    printf("Password [3..10]: ");
    if(my_gets(buf, bufsize, file) == 0) {
        return -1;
    }
    while(is_legal(buf) == 0) {
       printf("  Password must contain at least 1 uppercase, 1 lowercase, and 1    umber\nPassword [3..10]: ");
       if(my_gets(buf, bufsize, file) == 0) {
           return -1;
       }
    }
    return 0;
}

int main(void) {
    char password[100][15];
    int i = 0;
    if(get_password(password[i], sizeof(password[i]), stdin) != 0) {
        fprintf(stderr, "Error getting password\n");
        exit(1);
   }
   return 0;
}
于 2013-10-29T15:03:22.433 回答
2

使用这个正则表达式:

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+$

它说你必须至少有一个小写字符,一个大写字符和至少一个数字加上它比ctype方法少打字。

例子:

#include <regex.h>

int main (int argc, char *argv[])
{
    regex_t regex;
    int regexResult = regcomp(&regex, "^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+$", 0);
    regexResult = regexec(&regex, passwordVariableHere, 0, NULL, 0);

    if (!regex)
    {
        // Match
    }
}
于 2013-10-29T14:59:29.273 回答
0

ctype.h标题。检查密码的每个字符,直到满足所有条件(大写、小写、数字)。如果您到达密码字符串的末尾并且任何条件不满足,则密码错误。

于 2013-10-29T15:03:59.190 回答
0

也许这是检查 ASCII 表中字符位置的简单方法。您可以将所有字符检查为 65 到 90 之间的数字,用于大写字符,同样用于小写。

对于数字,您可以使用标准 c 库中的 atoi() 函数。

或者另一种可能性是使用 ctype.h 中的函数:islower()、isupper() 或 isdigit()。

于 2013-10-29T15:05:29.640 回答
0

我不太明白您为什么要使用一组 15 个字符长的密码,但我想您的标准仅指其中一个密码,而不是其他密码:您想检查密码是否需要考虑一个“好”的密码;这是我的理解。然后...

该功能gets相当不安全。避免使用它。

这个想法是要求输入密码,检查它并在它不符合您的标准时循环。当然,没有一种方法可以做到这一点。

// include files for I/O funcs
#include <stdio.h>

for(;;)
{
   printf("insert pwd: ");
   gets(buffer); // argh I've said: don't use this
   if ( !pass_criteria(buffer) ) {
       printf("criteria are ....\n");
   } else break;
}

然后 pass_criteria 可能类似于

// include files for strlen and is* funcs
#include <ctype.h>
#include <string.h>

int pass_criteria(const char *buf)
{
    int upcount, lowcount, numcount;

    if (strlen(buf) < minimum_pass_len ||
        strlen(buf) > max_pass_len) return 0; // 0 being false

    for (int i = 0; i < strlen(buf); ++i) {
       if (isdigit(buf[i]) numcount++;
       if (isupper(buf[i]) upcount++;
       if (islower(buf[i]) lowcount++;
    }
    return numcount > 0 && upcount > 0 && lowcount > 0;
}

更改标准很容易,例如,如果您想要至少 2 个数字(数字)、放置numcount > 1等等。

而不是得到

获取缓冲区溢出是危险的。尝试像这样使用例如 fgets:

   fgets(buffer, buffer_size, stdin);

您的缓冲区的大小在哪里buffer_size(在您的情况下为 15,但避免使用文字常量;更喜欢适当的#define或使用sizeof,例如sizeof (password[0])。另请注意, fgets 不会丢弃最终的换行符。

于 2013-10-29T15:18:59.067 回答