C 编程中的密码验证或检查器
我用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该循环最终只关心每个数组中的最后一个值是否匹配。
例如,比较
{ 1, 2, 3, 4 }
和{ 4, 4, 4, 4 }
将导致pass
为 < code>true 在循环之后,尽管存在明显的差异。相反,请将标志设置为
false
,并在发生不匹配时立即从循环中break
。如果从未发生不匹配,则此后该标志将保持
true
。通常密码是在存储之前经过哈希处理的文本(使用 salt)。密码验证是通过比较哈希值来完成的。例如,查看
man 3 crypt
库函数。
使用固定长度的一系列普通整数作为“密码”是不典型的,但对于玩具程序来说这是很好的。
这是一个可供研究的示例程序。
This loop ultimately only cares if the last value in each array match or not.
For example, comparing
{ 1, 2, 3, 4 }
and{ 4, 4, 4, 4 }
would result inpass
beingtrue
after the loop, despite the obvious differences.Instead, set the flag to
false
, andbreak
from your loop as soon as a mismatch occurs.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.
由于您没有具体说明您的问题(除了“它不起作用”),我将尽力列出所有可能的问题。
读取整数
将单个十进制整数读取到
otp
中的某个位置。如果密码是1024
,第一次循环(迭代)会将1024
读入otp[0]
。在第二次迭代中,scanf()
将等待,直到标准输入上有另一个数字可用。一旦可用,它会将其读入otp[1]
,依此类推。这个scanf()
循环实际上读取了 4 个不同的整数,并用换行符分隔。对于一个整数只执行一次scanf()
会容易得多,如下所示:您还可以使用
char
数组扫描 4 个字符的字符串:密码检查逻辑错误
正如@Oka 所解释的,你的检查器有一个逻辑错误。如果使用整数,您可以简单地检查
如果使用
char
数组(字符串),您可以使用您必须
#include
forstrcmp()
。标准输出缓冲区
在刷新
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
Will read a single decimal integer into a position in
otp
. If the password is1024
, the first time through the loop (iteration) will read1024
intootp[0]
. In the second iteration,scanf()
will wait until another number is available on standard input. Once it’s available, it will read it intootp[1]
, and so on. Thisscanf()
loop really reads in 4 different integers, separated by newlines. It would be much easier to do only onescanf()
for one integer, like this:You could also scan a 4-character string by using
char
arrays:Password-checking logic error
As @Oka explained, your checker has a logic error. If using an integer, you could simply check
If using a
char
array (string), you could useYou would have to
#include <string.h>
forstrcmp()
.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 toright after printing the prompt if you want it to appear.