How to encrypt\hide password from un-authorized? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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 *******

28th Jun 2019, 2:52 PM
Nouman Bin Sami
Nouman Bin Sami - avatar
13 Answers
+ 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
28th Jun 2019, 3:03 PM
Taste
Taste - avatar
+ 1
Taste what do next after copy a string into the new string?
28th Jun 2019, 3:13 PM
Nouman Bin Sami
Nouman Bin Sami - avatar
+ 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
28th Jun 2019, 3:18 PM
Taste
Taste - avatar
+ 1
I don't get the idea because I don't know what append function do
28th Jun 2019, 3:23 PM
Nouman Bin Sami
Nouman Bin Sami - avatar
+ 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"
28th Jun 2019, 3:27 PM
Taste
Taste - avatar
+ 1
Okay, but this code will append a character until the user press ENTER key.
28th Jun 2019, 3:37 PM
Nouman Bin Sami
Nouman Bin Sami - avatar
+ 1
yes
28th Jun 2019, 3:40 PM
Taste
Taste - avatar
+ 1
But this is not useful because we have a function getline (cin, name); that take input from user until user press enter key
28th Jun 2019, 3:42 PM
Nouman Bin Sami
Nouman Bin Sami - avatar
+ 1
but getline doesbt hide the input
28th Jun 2019, 3:43 PM
Taste
Taste - avatar
+ 1
Oh! I see
28th Jun 2019, 3:46 PM
Nouman Bin Sami
Nouman Bin Sami - avatar
+ 1
I think this code can help I will try this code
28th Jun 2019, 3:47 PM
Nouman Bin Sami
Nouman Bin Sami - avatar
+ 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/
28th Jun 2019, 4:16 PM
To Seek Glory in Battle is Glorious
To Seek Glory in Battle is Glorious - avatar
+ 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
29th Jun 2019, 5:11 AM
Nouman Bin Sami
Nouman Bin Sami - avatar