📜  在C ++中以密码作为输入

📅  最后修改于: 2021-05-31 23:21:11             🧑  作者: Mango

有两种方法可以更安全地进行输入:

  • 不显示任何内容。
  • 显示特殊字符(例如星号)而不是实际内容。

在这种方法中,输入内容将是不可见的。这可以通过两种方式实现:

使用

程序1:

下面是将控制台模式设置为启用,回显输入和重置控制台模式的程序:

C++
// C++ program to take the input
// invisibly
#include 
#include 
using namespace std;
  
// Function take password and
// reset to console mode
std::string takePasswdFromUser()
{
    HANDLE hStdInput
        = GetStdHandle(STD_INPUT_HANDLE);
    DWORD mode = 0;
  
    // Create a restore point Mode
    // is know 503
    GetConsoleMode(hStdInput, &mode);
  
    // Enable echo input
    // set to 499
    SetConsoleMode(
        hStdInput,
        mode & (~ENABLE_ECHO_INPUT));
  
    // Take input
    string ipt;
    getline(cin, ipt);
  
    // Otherwise next cout will print
    // into the same line
    cout << endl;
  
    // Restore the mode
    SetConsoleMode(hStdInput, mode);
  
    return ipt;
}
  
// Driver Code
int main()
{
    string input;
    cout << "@root>>> ";
  
    // Function Call
    input = takePasswdFromUser();
  
    // Print the input
    cout << input << endl;
}


C++
// C++ program to demonstrate the
// use of getch()
#include 
#include 
using namespace std;
  
// Function using getch()
std::string takePasswdFromUser()
{
    string ipt = "";
    char ipt_ch;
    while (true) {
        ipt_ch = getch();
  
        // Check whether user enters
        // a special non-printable
        // character
        if (ipt_ch < 32) {
            cout << endl;
            return ipt;
        }
        ipt.push_back(ipt_ch);
    }
}
  
// Driver Code
int main()
{
    string input;
    cout << "@root>>> ";
  
    // Funciton call
    input = takePasswdFromUser();
    cout << input << endl;
}


C++
// C++ program to demonstrate the
// solution of above drawback
#include 
#include 
using namespace std;
  
// Enumerator
enum TT_Input {
  
    // ASCII code of backspace is 8
    BACKSPACE = 8,
    RETURN = 32
};
  
// Function accepting password
std::string takePasswdFromUser()
{
    string ipt = "";
    char ipt_ch;
    while (true) {
        ipt_ch = getch();
  
        if (ipt_ch < TT_Input::RETURN
            && ipt_ch != TT_Input::BACKSPACE) {
            cout << endl;
            return ipt;
        }
  
        // Check whether the user
        // pressed backspace
        if (ipt_ch == TT_Input::BACKSPACE) {
  
            // Check if ipt is empty or not
            if (ipt.length() == 0)
                continue;
            else {
  
                // Removes last character
                ipt.pop_back();
  
                continue;
            }
        }
        ipt.push_back(ipt_ch);
    }
}
  
// Driver Code
int main()
{
    string input;
    cout << "@root>>> ";
  
    // Function call
    input = takePasswdFromUser();
    cout << input << endl;
}


C++
// C++ program to hide the password
// using *(asterik)
#include 
#include 
using namespace std;
  
// Enumerator
enum IN {
  
    // 13 is ASCII for carriage
    // return
    IN_BACK = 8,
    IN_RET = 13
  
};
  
// Function that accepts the password
std::string takePasswdFromUser(
    char sp = '*')
{
    // Stores the password
    string passwd = "";
    char ch_ipt;
  
    // Until condition is true
    while (true) {
  
        ch_ipt = getch();
  
        // if the ch_ipt
        if (ch_ipt == IN::IN_RET) {
            cout << endl;
            return passwd;
        }
        else if (ch_ipt == IN::IN_BACK
                 && passwd.length() != 0) {
            passwd.pop_back();
  
            // Cout statement is very
            // important as it will erase
            // previously printed character
            cout << "\b \b";
  
            continue;
        }
  
        // Without using this, program
        // will crash as \b can't be
        // print in beginning of line
        else if (ch_ipt == IN::IN_BACK
                 && passwd.length() == 0) {
            continue;
        }
  
        passwd.push_back(ch_ipt);
        cout << sp;
    }
}
  
// Driver Code
int main()
{
    string input;
    cout << "@root>>> ";
  
    // Function call
    input = takePasswdFromUser();
    cout << input << endl;
}


输出:

使用< conio.h > 

为此,使用了getch()。该函数从用户那里输入没有缓冲的字符,并且不等待用户按下“返回”键。

程式2:

下面是C++程序,用于演示conio.h中getch()的用法:

C++

// C++ program to demonstrate the
// use of getch()
#include 
#include 
using namespace std;
  
// Function using getch()
std::string takePasswdFromUser()
{
    string ipt = "";
    char ipt_ch;
    while (true) {
        ipt_ch = getch();
  
        // Check whether user enters
        // a special non-printable
        // character
        if (ipt_ch < 32) {
            cout << endl;
            return ipt;
        }
        ipt.push_back(ipt_ch);
    }
}
  
// Driver Code
int main()
{
    string input;
    cout << "@root>>> ";
  
    // Funciton call
    input = takePasswdFromUser();
    cout << input << endl;
}

输出:

缺点:用户无法清除之前做出的响应。当按退格键时,将返回输入。

程序3:

下面是C++程序,用于演示针对上述缺陷的解决方案:

C++

// C++ program to demonstrate the
// solution of above drawback
#include 
#include 
using namespace std;
  
// Enumerator
enum TT_Input {
  
    // ASCII code of backspace is 8
    BACKSPACE = 8,
    RETURN = 32
};
  
// Function accepting password
std::string takePasswdFromUser()
{
    string ipt = "";
    char ipt_ch;
    while (true) {
        ipt_ch = getch();
  
        if (ipt_ch < TT_Input::RETURN
            && ipt_ch != TT_Input::BACKSPACE) {
            cout << endl;
            return ipt;
        }
  
        // Check whether the user
        // pressed backspace
        if (ipt_ch == TT_Input::BACKSPACE) {
  
            // Check if ipt is empty or not
            if (ipt.length() == 0)
                continue;
            else {
  
                // Removes last character
                ipt.pop_back();
  
                continue;
            }
        }
        ipt.push_back(ipt_ch);
    }
}
  
// Driver Code
int main()
{
    string input;
    cout << "@root>>> ";
  
    // Function call
    input = takePasswdFromUser();
    cout << input << endl;
}

用特殊字符(*)隐藏密码

这个想法是在这里使用库来隐藏带有星号(*)的密码。以下是使用conio.h来使用*隐藏密码的C++程序:

计划4:

C++

// C++ program to hide the password
// using *(asterik)
#include 
#include 
using namespace std;
  
// Enumerator
enum IN {
  
    // 13 is ASCII for carriage
    // return
    IN_BACK = 8,
    IN_RET = 13
  
};
  
// Function that accepts the password
std::string takePasswdFromUser(
    char sp = '*')
{
    // Stores the password
    string passwd = "";
    char ch_ipt;
  
    // Until condition is true
    while (true) {
  
        ch_ipt = getch();
  
        // if the ch_ipt
        if (ch_ipt == IN::IN_RET) {
            cout << endl;
            return passwd;
        }
        else if (ch_ipt == IN::IN_BACK
                 && passwd.length() != 0) {
            passwd.pop_back();
  
            // Cout statement is very
            // important as it will erase
            // previously printed character
            cout << "\b \b";
  
            continue;
        }
  
        // Without using this, program
        // will crash as \b can't be
        // print in beginning of line
        else if (ch_ipt == IN::IN_BACK
                 && passwd.length() == 0) {
            continue;
        }
  
        passwd.push_back(ch_ipt);
        cout << sp;
    }
}
  
// Driver Code
int main()
{
    string input;
    cout << "@root>>> ";
  
    // Function call
    input = takePasswdFromUser();
    cout << input << endl;
}

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”