如何在 C++ 中删除密码屏蔽
How To Delete Password Masking in C++
我正在创建用户名和密码。在此之前,当我输入 backspace,而不是擦除密码时,backspace 输入了一些东西,我想到了使用 ASCII,但我不知道不知道删除密码的脚本。
int a=0, i=0;
char uname[10], c=' ';
char pword[10], code[10];
char user[10]="user";
char pass[10]="pass";
a:
system("cls");
printf("\n\n\t\t\t======================================");
printf("\n\n\t\t\t| STUDENT REPORTING SCORE SYSTEM |");
printf("\n\n\t\t\t======================================");
printf("\n\n\t\t\t=========== LOGIN FIRST ============");
printf("\n\n\n\t\t\t\tENTER USERNAME : ");
scanf("%s", &uname);
printf("\n\n\t\t\t\tENTER PASSWORD : ");
while(i<10)
{
pword[i]=getch();
c=pword[i];
if(c==13) break;
else if(c==8)
//here is the blank
else printf("*");
i++;
}
pword[i]='[=10=]';
i=0;
if(strcmp(uname,"user")==0 && strcmp(pword,"pass")==0)
{
printf("\n\n\n\t\t\tWELCOME TO STUDENT REPORTING SCORE SYSTEM\n\t\t\t\t LOGIN IS SUCCESSFUL");
printf("\n\n\n\t\t\t Press any key to continue...");
getch();
}
else
{
printf("\n\n\n\t\t\t SORRY !!!! LOGIN IS UNSUCESSFUL");
getch();
goto a;
}
我不知道我应该在那写什么//here is the blank
。所以当我使用 a-=2
时,它不想删除 *
,也不输入任何内容。
我稍微修改了你的代码。
在我的系统上,getch()
被删除了,所以我使用 _getch()
代替它。在 printf 之后,我在下一个 _getch()
处得到了一个 '[=13=]'
字符。所以我必须用 if (c == '[=15=]') continue;
行忽略它。
要删除 '*'
字符,您必须打印 "\b \b"
(\b
后退一个字符。' '
覆盖 '*'
和 '\b'
再次后退)。
如果用户键入 10 个字符作为密码,您的代码中会出现缓冲区溢出。您必须为最后 '\n'
个字符分配 10 + 1
长度数组。
我认为你应该使用 std::string
而不是 char*
并从代码中删除未使用的变量。
希望对您有所帮助。
bool login(std::string& username, std::string& password)
{
username = "";
password = "";
char c = ' ';
system("cls");
printf("\n\n\t\t\t======================================");
printf("\n\n\t\t\t| STUDENT REPORTING SCORE SYSTEM |");
printf("\n\n\t\t\t======================================");
printf("\n\n\t\t\t=========== LOGIN FIRST ============");
printf("\n\n\n\t\t\t\tENTER USERNAME : ");
std::cin >> username;
printf("\n\n\t\t\t\tENTER PASSWORD : ");
while (true)
{
c = _getch();
if (c == '\r') break;
if (c == '[=10=]') continue;
if (c == '\b')
{
if (password.length() > 0)
{
password.pop_back();
printf("\b \b");
}
}
else
{
printf("*");
password.push_back(c);
}
}
_getch(); // Read the extra '[=10=]'
if (username == "user" && password == "pass")
{
printf("\n\n\n\t\t\tWELCOME TO STUDENT REPORTING SCORE SYSTEM\n\t\t\t\t LOGIN IS SUCCESSFUL");
return true;
}
else
{
printf("\n\n\n\t\t\t SORRY !!!! LOGIN IS UNSUCESSFUL");
return false;
}
}
int main()
{
while (true)
{
std::string username = "";
std::string password = "";
auto successLogin = login(username, password);
if (successLogin)
break;
_getch(); // Press any key to continue...
}
return 0;
}
void login()
c:
system("cls");
char choice='Y', input, passChar[100],userChar[100];
for (int i=0; i<100; userChar[i++]=0);
while(choice=='Y'){
system ("cls");
cout<<"\n\n\t\t\t======================================";
cout<<"\n\n\t\t\t| STUDENT SCORE REPORTING SYSTEM |";
cout<<"\n\n\t\t\t======================================";
cout<<"\n\n\t\t\t=========== LOGIN FIRST ============";
cout<<"\n\n\n\t\t\t\tENTER USERNAME : ";
cin.getline(userChar,100);
system("cls");
cout<<"\n\n\t\t\t======================================";
cout<<"\n\n\t\t\t| STUDENT SCORE REPORTING SYSTEM |";
cout<<"\n\n\t\t\t======================================";
cout<<"\n\n\t\t\t=========== LOGIN FIRST ============";
cout<<"\n\n\n\t\t\t\tENTER PASSWORD: ";
for (int i=0;;i++)
{
input=getch();
if ((input!=8)&&(input!=13))
{
passChar[i]=input;
}
else if (input==8)
i-=2;
else if (input==13)
break;
system("cls");
cout<<"\n\n\t\t\t======================================";
cout<<"\n\n\t\t\t| STUDENT SCORE REPORTING SYSTEM |";
cout<<"\n\n\t\t\t======================================";
cout<<"\n\n\t\t\t=========== LOGIN FIRST ============";
cout<<"\n\n\n\t\t\t\tENTER PASSWORD: ";
for (int j=0; j<i+1; j++)
cout<<"*";
}
cout<<endl;
if (!strcmp(userChar, "admin"))
{
cout<<"UserName correct!\n";
}
else
cout<<"Username incorrect!\n";
if (!strcmp(passChar, "pass"))
{
cout<<"Password correct!\n";
}
else
cout<<"Password incorrect!\n";
if ((!strcmp(userChar,"admin"))&&(!strcmp(passChar, "pass")))
{
cout<<"Access Granted!";
getch();
return;
}
else
cout<<"Acess denied!\n";
cout<<"Do you want to try again? Y/N : ";
cin>>choice;
choice=toupper(choice);
cin.ignore(100,'\n');
for (int i=0; i<100; userChar[i++]=0);
for (int i=0; i<100; passChar[i++]=0);
}}
我正在创建用户名和密码。在此之前,当我输入 backspace,而不是擦除密码时,backspace 输入了一些东西,我想到了使用 ASCII,但我不知道不知道删除密码的脚本。
int a=0, i=0;
char uname[10], c=' ';
char pword[10], code[10];
char user[10]="user";
char pass[10]="pass";
a:
system("cls");
printf("\n\n\t\t\t======================================");
printf("\n\n\t\t\t| STUDENT REPORTING SCORE SYSTEM |");
printf("\n\n\t\t\t======================================");
printf("\n\n\t\t\t=========== LOGIN FIRST ============");
printf("\n\n\n\t\t\t\tENTER USERNAME : ");
scanf("%s", &uname);
printf("\n\n\t\t\t\tENTER PASSWORD : ");
while(i<10)
{
pword[i]=getch();
c=pword[i];
if(c==13) break;
else if(c==8)
//here is the blank
else printf("*");
i++;
}
pword[i]='[=10=]';
i=0;
if(strcmp(uname,"user")==0 && strcmp(pword,"pass")==0)
{
printf("\n\n\n\t\t\tWELCOME TO STUDENT REPORTING SCORE SYSTEM\n\t\t\t\t LOGIN IS SUCCESSFUL");
printf("\n\n\n\t\t\t Press any key to continue...");
getch();
}
else
{
printf("\n\n\n\t\t\t SORRY !!!! LOGIN IS UNSUCESSFUL");
getch();
goto a;
}
我不知道我应该在那写什么//here is the blank
。所以当我使用 a-=2
时,它不想删除 *
,也不输入任何内容。
我稍微修改了你的代码。
在我的系统上,getch()
被删除了,所以我使用 _getch()
代替它。在 printf 之后,我在下一个 _getch()
处得到了一个 '[=13=]'
字符。所以我必须用 if (c == '[=15=]') continue;
行忽略它。
要删除 '*'
字符,您必须打印 "\b \b"
(\b
后退一个字符。' '
覆盖 '*'
和 '\b'
再次后退)。
如果用户键入 10 个字符作为密码,您的代码中会出现缓冲区溢出。您必须为最后 '\n'
个字符分配 10 + 1
长度数组。
我认为你应该使用 std::string
而不是 char*
并从代码中删除未使用的变量。
希望对您有所帮助。
bool login(std::string& username, std::string& password)
{
username = "";
password = "";
char c = ' ';
system("cls");
printf("\n\n\t\t\t======================================");
printf("\n\n\t\t\t| STUDENT REPORTING SCORE SYSTEM |");
printf("\n\n\t\t\t======================================");
printf("\n\n\t\t\t=========== LOGIN FIRST ============");
printf("\n\n\n\t\t\t\tENTER USERNAME : ");
std::cin >> username;
printf("\n\n\t\t\t\tENTER PASSWORD : ");
while (true)
{
c = _getch();
if (c == '\r') break;
if (c == '[=10=]') continue;
if (c == '\b')
{
if (password.length() > 0)
{
password.pop_back();
printf("\b \b");
}
}
else
{
printf("*");
password.push_back(c);
}
}
_getch(); // Read the extra '[=10=]'
if (username == "user" && password == "pass")
{
printf("\n\n\n\t\t\tWELCOME TO STUDENT REPORTING SCORE SYSTEM\n\t\t\t\t LOGIN IS SUCCESSFUL");
return true;
}
else
{
printf("\n\n\n\t\t\t SORRY !!!! LOGIN IS UNSUCESSFUL");
return false;
}
}
int main()
{
while (true)
{
std::string username = "";
std::string password = "";
auto successLogin = login(username, password);
if (successLogin)
break;
_getch(); // Press any key to continue...
}
return 0;
}
void login()
c:
system("cls");
char choice='Y', input, passChar[100],userChar[100];
for (int i=0; i<100; userChar[i++]=0);
while(choice=='Y'){
system ("cls");
cout<<"\n\n\t\t\t======================================";
cout<<"\n\n\t\t\t| STUDENT SCORE REPORTING SYSTEM |";
cout<<"\n\n\t\t\t======================================";
cout<<"\n\n\t\t\t=========== LOGIN FIRST ============";
cout<<"\n\n\n\t\t\t\tENTER USERNAME : ";
cin.getline(userChar,100);
system("cls");
cout<<"\n\n\t\t\t======================================";
cout<<"\n\n\t\t\t| STUDENT SCORE REPORTING SYSTEM |";
cout<<"\n\n\t\t\t======================================";
cout<<"\n\n\t\t\t=========== LOGIN FIRST ============";
cout<<"\n\n\n\t\t\t\tENTER PASSWORD: ";
for (int i=0;;i++)
{
input=getch();
if ((input!=8)&&(input!=13))
{
passChar[i]=input;
}
else if (input==8)
i-=2;
else if (input==13)
break;
system("cls");
cout<<"\n\n\t\t\t======================================";
cout<<"\n\n\t\t\t| STUDENT SCORE REPORTING SYSTEM |";
cout<<"\n\n\t\t\t======================================";
cout<<"\n\n\t\t\t=========== LOGIN FIRST ============";
cout<<"\n\n\n\t\t\t\tENTER PASSWORD: ";
for (int j=0; j<i+1; j++)
cout<<"*";
}
cout<<endl;
if (!strcmp(userChar, "admin"))
{
cout<<"UserName correct!\n";
}
else
cout<<"Username incorrect!\n";
if (!strcmp(passChar, "pass"))
{
cout<<"Password correct!\n";
}
else
cout<<"Password incorrect!\n";
if ((!strcmp(userChar,"admin"))&&(!strcmp(passChar, "pass")))
{
cout<<"Access Granted!";
getch();
return;
}
else
cout<<"Acess denied!\n";
cout<<"Do you want to try again? Y/N : ";
cin>>choice;
choice=toupper(choice);
cin.ignore(100,'\n');
for (int i=0; i<100; userChar[i++]=0);
for (int i=0; i<100; passChar[i++]=0);
}}