How to set a password?

Pages: 12
Would this work, or would this make you have to set the password every time? I want to be able to save the password when the program is not in use. How would I go about doing that? Is there a way to make the program change the source code of the program?
1
2
3
4
5
6
7
8
9
string password;
cout << "Set the password";
cin >> password;
cout << "Enter the password";
cin >> password;
if (password == "(password user set earlier)"
{
(code)
}
ok, the code that you posted wouldent work because itd resets the password variable twice, to exspress the comparison between two passwords use this meathod:
1
2
3
4
5
6
7
8
9
10
string password1;
string password2;
cout << "set the password:\n";
cin >> password1;
cout<<"enter the password you intruter -_- :\n"
cin >> password2;
if(password1==password2)
{
cout << "hi not intruder ";
}

as for saving the password so that you can call the program a second time and it would already know the password. you should use fileIO functions to create a file somewhere and then get the password from that file and compare it to the entered password.search stdio.h in this site's c++ reference
If I were to make it get input from the text file, is there a way to store said input to a variable?
What do you mean? You can read the value of the string from a file and then place it into a variable.
I mean, like, if I get a line of text from a text file, could I set that = to the string called password?
Isn't that how you get a line of text in the first place?
Yes.

1
2
//password.txt
abc123


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <fstream>
#include <string>
#include <iostream>

int main()
{
     std::string password = "";
     
     std::ifstream inputFile("password.txt"); //should put the full path

     if( inputFile.is_open() )
     {
          std::getline( inputFile, password );

          inputFile.close();

          std::cout << "Password is: " << password << std::endl;
     }
     else
     {
          std::cerr << "Problem in opening the file" << std::endl;
     }

     return 0;
}
Last edited on
new question. Will
1
2
3
cin myfile;
//or 
cin filename;

write what the user enters to the text file?
Nevermind on that last question, I figured it out
Ithought I had it all figured out, but I got this error:

error: no matching function for call to 'std::basic_ifstream<char, std::char_traits<char> >::getline(std::string&)'|

With this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
   string pepper;
   string input;
   ifstream sauce("C:/users/youtube area/desktop/password.txt");
   sauce.getline(pepper);
   cout << "Enter the password " << endl;
   cin >> input;
   if (input == pepper)
   {
       //code
   }
   else
   {
       cout << "Intruder! Leave now!";
   }
   cin.get();
    return 0;
}

how do I fix that?
I think you need getline (sauce , pepper); not sauce.getline (pepper);
Okay I fixed it by following what shaktar did almost exactly. I have 2 exes now. One that sets the password that you enter, and one that asks you for the password, then executes the code. Now I have to think up some really cool code.
JSYK, you should have a one-way function that hashes or encrypts the password, and store the hash/encryption, so that users can't just look at the password file...

When the user enters his password, just hash or encrypt it and compare the result with what is in the file.
@Duoas
I.....don't know what that means, *Is still C++ noob* I guess I'm better than someone who knows NOTHING, but still....
What Duoas means is that you should encrypt your passwords. Encryption to my knowledge is when you make a different character for an other character. For instance lets say I have the password Weezer if I wanted to encrypt it I would have to make up characters for each Letter in the word "Weezer" so I could do something like this
! = W
% = E
# = R
^ = Z
So now Weezer looks like this !%%^%#
If you did not have the key then this makes no sense. So hackers can not get your password.

And do not think that you know nothing File I/O is not very easy. And you are getting it pretty fast.
Yay! I don't really know as little as I thought! And how would I go about actually doing the encryption process? And can someone look at the source code of my 2 projects to see if I can make any improvements?
File 1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Sets the password for the main program
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    string password;
    cout << "Set the password" << endl;
     ofstream myfile ("C:/users/youtube area/desktop/password.txt");
      if (myfile.is_open())
      {
          cin >> password;
          myfile << password;
          cout << "Your new password is " << password << endl;
      }
    return 0;
}

Thats the file to change the password.
File 2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    const int MAX_CHARS_PER_LINE = 1000;
   string pepper;
   ifstream sauce("C:/users/youtube area/desktop/password.txt");
   char charsFromFile[MAX_CHARS_PER_LINE];
          sauce.getline(charsFromFile, MAX_CHARS_PER_LINE);
   cout << "Enter the password " << endl;
   cin >> pepper;
   if (pepper == charsFromFile)
   {
       //code
   }
   else
   {
       cout << "Intruder! Leave now!";
   }
   cin.get();
    return 0;
}

Thats the main project that I have no code in yet
Last edited on
http://pastebin.com/j5YAzv1U
I know there is a lot of bad coding and I know for all the size_ts I could have just used one but I was getting confused using only one so I use one for every letter. I made this a long time ago so there might be bugs so please excuse me. Also there might be easier ways of doing this but I did not know better so again sorry. I also believe you can mess with the ASCII value but I do not know much about that.
300+ lines of code? The most i've EVER coded is the 27 lines I have here, and the SDL code that didn't work that was like 30 something. 300+ must have taken a LONG time
I should've done this with the code I posted before, but you can take a line from the file and put it into a std::string directly.

1
2
string stringFromFile = "";
getline(sauce, stringFromFile);

(I have edited my code above to use this method)

ziodice wrote:
And how would I go about actually doing the encryption process?

For a basic hash, a std::map could do the job.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <map>
#include <string>
#include <iostream>

typedef std::map<char, char> hashType;

std::string doHash(const std::string& input, const hashType& hash)
{
     std::string hashedString(input);

     for(size_t i = 0; i < hashedString.length(); i++)
     {
          hashedString[i] = hash.find( hashedString[i] )->second;
     }

     return hashedString;
}

int main()
{
     hashType hash;

     //you may only choose to support certain characters so that you
     //don't need to make a relation for every character
     hash['a'] = 'r';
     hash['b'] = 'x';
     hash['c'] = ';';
     //...
     hash['z'] = '7';
     hash['A'] = 'Q';
     //...
     hash[':'] = '/';
     hash['>'] = '<';
     //...

     std::string input = "abcdef";

     std::cout << doHash( input, hash ) << std::endl;

     return 0;
}


http://www.cplusplus.com/reference/stl/map/

ziodice wrote:
300+ lines of code?

That code can be thoroughly condensed using arrays (or a map).
Last edited on
Is there a way to make multiple passwords, so that different users could get in to the program and resume their last session? I was thinking copying and pasting the code, renaming the variables, and making more text files. Wait...is it possible to have a program MAKE a text file, then let you set the password, then let you enter the program?
Pages: 12