+ 2
How to encrypt\hide password from un-authorized?
I am working on a project where I need to protect the password from unauthorized how do I do that? For example; If user entering password like alibaba$ how to protect it and how to hide\encrypt it in the asteric * form like *******
13 Respuestas
+ 2
i'm not sure how we can show * while typing the input using this method, but basicly i have an idea on how to do this.
by using the answer from 'To Seek Glory In Battle Is Glorious' from this post ( https://www.sololearn.com/Discuss/1863352/?ref=app )
by catch all the input from cin.get then create a new string from all of that character
but downside is the commandline wont have any feedback while the user is typing
+ 1
Taste what do next after copy a string into the new string?
+ 1
treat them as the password input
string password = "";
while(n=cin.get()){
if(n=='\n') break;
else password.append(1,n);
}
//do something with password
+ 1
I don't get the idea because I don't know what append function do
+ 1
it'll add a new character to a string.
for example
string s = "hell"
then append the string with o
s will now be "hello"
+ 1
Okay, but this code will append a character until the user press ENTER key.
+ 1
yes
+ 1
But this is not useful because we have a function getline (cin, name); that take input from user until user press enter key
+ 1
but getline doesbt hide the input
+ 1
Oh! I see
+ 1
I think this code can help I will try this code
+ 1
The language and the standard library have no facility to hide the password while entering by the user, instead, you should look for the facilities provided by the operating system. depending upon the operating system in which the code is to be executed, there are two general approaches[1] to "disable the command-line echo".
1) Windows-based OSes:
#include <windows.h>
using namespace std;
int main()
{
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode = 0;
GetConsoleMode(hStdin, &mode);
SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT));
// the program logic to get the input
}
2) Unix-based OSes:
#include <termios.h>
#include <unistd.h>
using namespace std;
int main()
{
termios oldt;
tcgetattr(STDIN_FILENO, &oldt);
termios newt = oldt;
newt.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
// the program logic to get the input
}
Note 1: While typing you wouldn't see anything on the console screen. Something like when inserting the super user's password in Linux.
Note 2: I personally tested it on Windows under Visual Studio and it worked
[1] www.cplusplus.com/forum/beginner/43683/
+ 1
Taste your above mention code was not do what we expect. It's not fall on the requirements. It's not hid the characters