C 密码生成器显示一个不显示';不要在游泳池里游泳
我已经编写了一个密码生成器,它有时看起来行为笨拙。启动程序时,有时会得到如下输出: 密码:pyi>Sx2Z 我实际上排除了“大于”字符。我甚至打印了池中所有可用的字符,“大于”字符不会出现。我有点困惑。我感谢任何帮助或解释。提前谢谢。 这是我的密码:C 密码生成器显示一个不显示';不要在游泳池里游泳,c,C,我已经编写了一个密码生成器,它有时看起来行为笨拙。启动程序时,有时会得到如下输出: 密码:pyi>Sx2Z 我实际上排除了“大于”字符。我甚至打印了池中所有可用的字符,“大于”字符不会出现。我有点困惑。我感谢任何帮助或解释。提前谢谢。 这是我的密码: #include <stdio.h> #include <stdlib.h> #include <time.h> #define STARTNUMBER '0' #define ENDNUMBER
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define STARTNUMBER '0'
#define ENDNUMBER '9'
#define STARTUPLETTER 'A'
#define ENDUPLETTER 'z'
#define STARTLOWLETTER 'a'
#define ENDLOWLETTER 'z'
#define SIZE (2*26+10)
#define DEBUG
int main(int argc, char** argv)
{
srand(time(0));
int defaultLenght = 8;
if(argc == 2)
{
defaultLenght = atoi(argv[1]);
}
char pool[SIZE];
char password[defaultLenght];
char digitCount = ENDNUMBER - STARTNUMBER + 1;
for(int c = 0; c < digitCount; c++)
{
pool[c] = STARTNUMBER + c;
}
char upLetterCount = ENDLOWLETTER - STARTUPLETTER + 1;
for(int c = 0; c < upLetterCount; c++)
{
pool[digitCount + c] = STARTUPLETTER + c;
}
char lowLetterCount = ENDLOWLETTER - STARTLOWLETTER + 1;
for(int c = 0; c < lowLetterCount; c++)
{
pool[digitCount + lowLetterCount + c] = STARTLOWLETTER + c;
}
#ifdef DEBUG
for(int i = 0; i < SIZE; i++)
{
printf("%c", pool[i]);;
}
printf("\r\n");
#endif
printf("password: ");
for(int i = 0; i < defaultLenght; i++)
{
int index = rand() % SIZE + 1;
password[i] = pool[index];
pool[index] = pool[SIZE -i -1];
putchar(password[i]);
}
printf("\r\n");
return(0);
}
#包括
#包括
#包括
#定义起始编号“0”
#定义端号“9”
#定义字母“A”
#定义ENDUPLETTER“z”
#定义“a”
#定义ENDLOWLETTER“z”
#定义尺寸(2*26+10)
#定义调试
int main(int argc,字符**argv)
{
srand(时间(0));
int defaultLenght=8;
如果(argc==2)
{
defaultLenght=atoi(argv[1]);
}
字符池[大小];
字符密码[DefaultLength];
char digitCount=ENDNUMBER-STARTNUMBER+1;
对于(int c=0;c
使用将随机索引选择到池中时出错
int index = rand() % SIZE + 1;
这将为1..SIZE
范围内的数字赋值,但池需要0..(SIZE-1)
范围内的索引。这可能导致选择数组外的下一个字符。所以那条线应该是
int index = rand() % SIZE;
但您的密码选择还有另一个问题。您可以使用池数组中的另一个字符覆盖所选字符,可能是为了防止它被选中两次,但不会减小池的大小。我建议:
int poolsize = SIZE;
for(int i = 0; i < defaultLenght; i++)
{
int index = rand() % poolsize;
password[i] = pool[index];
pool[index] = pool[--poolsize];
putchar(password[i]);
}
int poolsize=SIZE;
对于(int i=0;i
'z'
对于ENDUPLETTER
来说是一个非常奇怪的值,这并不重要,因为您的大写范围似乎使用了ENDLOWLETTER-STARTUPLETTER+1
。在常规ASCII中,有六个非字母数字值夹在大小写范围之间。您的<代码>大小似乎无法解释这一点。我希望您不打算使用此IRL<代码>随机数不正确。真的非常糟糕。@BaummitAugen这只是为了练习。