C/C++ Creating a simple password program in C++

AI Thread Summary
The discussion revolves around a user attempting to create a password verification program in C++. The user encounters issues with a while loop that prevents the program from allowing multiple attempts to enter the password. Feedback from other users highlights the need for proper loop structure and the use of braces to ensure the code functions as intended. Suggestions include using a do-while loop for better flow and emphasizing the importance of indentation for clarity. Additionally, the conversation shifts to the topic of password security, with mentions of hashing algorithms and the necessity of salting passwords to enhance security. Users discuss various hashing methods, including CRC32 and SHA512, and the risks associated with password storage. The importance of using established libraries for authentication and hashing is emphasized, particularly for novice programmers. Overall, the thread serves as a learning resource for both coding practices and security considerations in password management.
The_Inventor
Messages
9
Reaction score
2
So I'm trying to teach myself C++ programming and I'm having trouble writing a password program.
Essentially I want to program to ask the user to input a password, and then compare that input with the correct password. If the user input matches the password I want the program to output a message saying "password accepted", if they do not match then I want the program to output a message saying "password invalid... Try again:" and from there the user continues to try a password until they input the correct password. This seemed simple at first however the problem arises when I try to introduce a while loop. If the password is incorrect after the second try they program continues to loop the message "password invalid...Try again:" without giving the user a chance to type in the password. Below is my source code, I'm using visual studio by the way. Can anyone help me out?
C:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <math.h>

using namespace std;

string Admission()
{
    string userinput;                    // this is the user's guess
    string password = "TonyStark";        // this is the actual password
    cout << "Enter password: ";
    cin >> userinput;
    if (userinput != password)
    {
        cout << "Password Invalid... Try Again: ";
        cin >> userinput;
        while (userinput != password)
            cout << "Password Invalid... Try Again" << endl;
            cin >> userinput;
    }
    else
        cout << "Password Accepted... ";
 
    return "Welcome";
}

int main()
{
    cout << Admission() << endl;

    system("pause>nul");
    return 0;
}
 
Last edited by a moderator:
  • Like
Likes Telemachus
Technology news on Phys.org
Your loop is in the wrong place. Try this:
Code:
  ...
do
  cout << "Enter password: ";
  cin >> userinput;
  if (userinput != password)
    cout << "Password Invalid... Try Again: ";
  while (userinput != password);
cout << "Password Accepted... ";
  ...
 
The_Inventor said:
while (userinput != password)
cout << "Password Invalid... Try Again" << endl;
cin >> userinput;
I see the problem now. The third statement above is not in the loop. You need {} braces.
(It would have been obvious with the right indentation.)
 
  • Like
Likes The_Inventor
haruspex said:
I see the problem now. The third statement above is not in the loop. You need {} braces.
(It would have been obvious with the right indentation.)
Wow, such a simple mistake. It works now, thanks a lot!
 
Also real password algorithms use hashing so that the password is always hidden. They hash the user input and compare it to the hashed password with matching hashes meaning passwords match.
 
jedishrfu said:
Also real password algorithms use hashing so that the password is always hidden. They hash the user input and compare it to the hashed password with matching hashes meaning passwords match.

Interesting, Is there a way to do this in C++? I've been learning this stuff on my own so there's many C++ commands, and tricks that I haven't been exposed to yet.
 
The_Inventor said:
Is there a way to do this in C++?
Of course, but some hashing algorithms are better than others. First, you have to decide what level of risk you will tolerate for an accidental match. Say you have a good algorithm with a 32 bit hash. Then the risk of an accidental match is one in 232.
CRC32 is very good as an algorithm; there is a choice of polynomials.
 
The_Inventor said:
Wow, such a simple mistake. It works now, thanks a lot!

Sometimes the error creep in because of a misplaced semicolon:

C:
for(int i=0; i<5; i++) ;       // the FOR loop runs 5 times executing the  empty semicolon statement
{
    printf("Hello World!");  // finally after the FOR loop completes then the print block prints Hello World
}

because I've done this in the past, I use the following brace formatting in my code:

C:
for(int i=0; i<5; i++)  {
    printf("Hello World!");     // the FOR loop runs 5 times and prints Hello World 5 times as expected...
}

because seeing "; {" reminds me of a sad face ie it looks wrong when you see it.
 
  • Like
Likes jim mcnamara
  • #10
haruspex said:
Of course, but some hashing algorithms are better than others. First, you have to decide what level of risk you will tolerate for an accidental match. Say you have a good algorithm with a 32 bit hash. Then the risk of an accidental match is one in 232.
CRC32 is very good as an algorithm; there is a choice of polynomials.

In real world programming, you want to use an algorithm like SHA512, and you also want to have a random salt generated. You also need to set a minimum complexity required of the password itself.

The main factor in selecting an algorithm isn't accidental matches per say; instead, it is how fast someone can crack it using say rainbow tables. These days it's good practice to have a secondary means of authentication, i.e., two factor.
 
  • #11
@SixNein - it is really common for inexperienced (and some who know better ) programmers to come up with methods or algorithms for tasks they have no good understanding (about). You know - The ones best handled by the libraries in the OS/language implementation - Passwords, authentication in general, date/time manipulations, hashing, etc.

I view the OP as purely a learner and assume nothing important will be behind the authentication. Maybe it is a way to block little brother/kid out of the system.
 
  • #12
Don't forget to "salt" your hashes!

Hackers have rainbow tables that have the hashes for most well used passwords - if you salt the users password before performing the hash, then store the salt and the hash you'll be much safer and it doesn't add too much overhead
 
  • #13
jim mcnamara said:
I view the OP as purely a learner and assume nothing important will be behind the authentication.
Indeed, this reads like something that I might have given as a simple exercise during the first few weeks of a programming course, after covering the basics of console I/O, if-statements and loops. I would of course have told students that real password algorithms also involve encryption and are more sophisticated than this.

Crawling before walking before running before marathons. :oldwink:
 
  • #14
jim mcnamara said:
@SixNein - it is really common for inexperienced (and some who know better ) programmers to come up with methods or algorithms for tasks they have no good understanding (about). You know - The ones best handled by the libraries in the OS/language implementation - Passwords, authentication in general, date/time manipulations, hashing, etc.

I view the OP as purely a learner and assume nothing important will be behind the authentication. Maybe it is a way to block little brother/kid out of the system.

My response was mostly geared towards another poster of the thread although the OP may benefit from being aware of the existence of these ideas.
 
  • #15
I have not got a lot to hide., it makes stuff too complicated.
 

Similar threads

Back
Top