【发布时间】:2016-04-01 15:52:02
【问题描述】:
void main()
{
Password();
}
int Password()
{
// Declare local variables//
char cPassCode[] = "String";
int iFlag, iComparison = 0;
// Run the code to check the password//
while (iFlag = 0)
{
printf("Please enter the password: ");
scanf("%s", cPassCode);
iComparison = strcmp(cPassCode, "A23bc5");
if (iComparison = 0)
{
ArrayPrinter(Array);
iFlag = 1;
}
else
{
printf("Wrong password");
iFlag = 0;
}
return(iFlag);
}
}
我编辑了这段代码,据我所知,当输入密码 A23bc5 时它应该可以正常运行。但是它总是返回错误的密码。有什么想法吗?
【问题讨论】:
-
打印出cPassCode每个char的值。
-
它正在打印正确的密码,但仍然说密码不正确
-
从不在检查密码时使用 strcmp(),因为这会使您的应用程序容易受到计时攻击!为防止这种情况,您可以创建输入密码和真实密码的 md5/sha 哈希值并进行比较..
-
注意:使用密码后,最好将缓冲区清零。然而,对于嵌入了
"A23bc5"的代码,这种安全性改进与@ensc 一样好主意。
标签: c string-comparison strcmp