用户名和密码检查 [英] username and password check

查看:153
本文介绍了用户名和密码检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个用于用户名+密码身份验证的程序(主要是出于教育目的,但是如果我喜欢它,则稍后可能会在更大的项目中使用该程序).到目前为止,我的作品有效",这意味着没有错误,但是它的行为确实有些怪异.在只打算执行一次操作之前,它在退出之前执行了STUFF()大约9次(我没有保存确切的输出).

I'm writting a program (mostly educational purpose but if I like it then I'll probably use it in a larger project later) for username + password authentication. What I have so far "works", meaning there are no errors, but it does behave a little weirdly. It did STUFF() about 9 times (I didn't save the exact output) before exiting when it's only meant to do it once.

如何使它仅执行一次STUFF()?如何使密码输入不可见?以及我如何才能总体上提高安全性/语法或使其更短?

How can I make it do STUFF() just once? How can I make the password input invisible? And how can I just generally improve security/syntax or make it shorter?

#include <iostream>
using namespace std;

void STUFF()
{
   cout << "Doing stuff..." << endl;
}

int CREDS;
void AUTH()
{
   cout << "Username: "; string USER; cin >> USER;
   cout << "Password: "; string PASS; cin >> PASS;
   if (USER == "josh" and PASS == "passwd")
   {
      CREDS = 0;
   }
   else 
   {
      CREDS = 1;
   };
}

void RETRY()
{
   cout << "Authentication failed! Try again? [Y/n]" << endl; char REPLY; cin >> REPLY;
   if (REPLY == 'Y' or REPLY == 'y')
   {
      AUTH();
   }
   else if (REPLY == 'N' or REPLY == 'n')
   {
      cout << "Exiting..." << endl;
   }
   else
   {
      RETRY();
   };
}

int main()
{
   AUTH();
   if (CREDS == 0)
   {
      STUFF();
      return 0;
   }
   else if (CREDS == 1)
   {
      RETRY();
   };

}

推荐答案

我放弃了最后一个程序,并从头开始编写了该程序. 对于任何想使用C ++的新手,它都是GNU GPLv2许可的,可以在".只需下载"Userpass.zip",因为无需克隆整个存储库.

I have abandoned that last program and wrote this from scratch. For anyone new to C++ that would like to use this, it is licensed under the GNU GPLv2 and can be found in Small-Projects-Cpp on Github. Just download "Userpass.zip" as it is not necessary to clone the entire repository.

#include <iostream>
#include <string>
#include <unistd.h>
#include <termios.h>

getpass()使密码输入显示星号.并非完全不可见,但它可能是最好的解决方案.

getpass() makes the password input show asterisks. Not exactly invisible, but it's probably the best solution for that.

using namespace std;
string PASSWD;
string getPASS()
{
    termios oldt;
    tcgetattr(STDIN_FILENO, &oldt);
    termios newt = oldt;
    newt.c_lflag &= ~ECHO;
    tcsetattr(STDIN_FILENO, TCSANOW, &newt);
    string PASS;
    cout << "Password: "; cin >> PASS; cout << endl;
    tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
    return PASS;
   }

MENU()有一个switch/case菜单,但它不相关,因此我跳过了它.您可以将其替换为所需的任何功能.

MENU() has a switch/case menu but it is unrelated so i'm skipping it in the asnwer. You can replace it with whatever funtions you want.

int attempts = 0;
int main()
{

   while (attempts == 3)
   {
      cout << "Too many attempts have been made! Exiting..." << endl; exit(0);
   };
   string USER;
   cout << "Username: "; cin >> USER;

   if (USER == "josh") 
       {   
           if (getPASS() == "hsoj")
       {
        cout << "\nAccess granted!\n" << endl;
        MENU();
       }
           else
       {
           cout << "\nAccess denied!\n" << endl; 
        attempts = attempts + 1;
        main();
       };
   }
   else
   {
      cout << "\nAccess denied!\n" << endl; 
      attempts = attempts + 1;
      main();
   };
   return 0;
}

这篇关于用户名和密码检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆