-
Declare the ifstream outside of either Password() or ConfirmCode() and pass it to both functions as a parameter by reference. That way the stream will remain open and the pointer that reads the file will remain in place between function calls.
Code:
ifstream fin("sample.txt");
if(!fin)
cerr << "unable to open file" << endl;
int x;
x = Password(fin);
if(x == 1)
ConfirmCode(fin);
where Password() and ConfirmCode() are declared with a reference to an ifstream object as a parameter like this:
int Password(ifstream &);
When you're done pass fin around, you can close it.
-
I got it now in int main() like this:
Code:
int main()
{
ifstream A ( "Startup.txt" );
if ( A.is_open() ){
A.getline(ThePassword, 100);
A.getline(TheConfirmCode, 100);
A.close();
}
Password();
}
This work 50 % it reads the ThePassword and the TheConfirmCode well but I can't put other letters and numbers in front of it :(. I didn't use (or understand) your method because I don't know pointers and references, I'm still learning. But it would usefull for me if I know how he starts reading after a number of characters and stop reading after the size of the ThePassword and the TheConfirmCode I allready tried much but I think I need to use something with strlen, thx anyway.
-
or keep it basic with:
Code:
char ThePassword[256];
cout << "The Current Password Is: ";
ifstream B ("Startup.txt");
cout << ThePassword << endl;
B.close();
char PasswordChange[2];
cout << "Would you like to change your password? Y/N: ";
cin.getline ( PasswordChange, 2, '\n' );
if ( strcmp ( PasswordChange, "Y" ) == 0 ) {
cout << "Enter New Password: ";
cin.getline ( ThePassword, 256, '\n' );
ofstream NewPass ("Startup.txt");
NewPass << ThePassword;
NewPass.close();
ifstream SeePass ("Startup.txt");
SeePass.close();
cout << "Your Password Has Been Changed To: " << ThePassword << endl;
cin.get();
}
else {
cout << "Your Password Is The Same!" << endl;
cin.get();
There may be flaws, i know it doesnt save the new password (After you reatart the program, it says your password is password), but you can figure that out, this is a 5 minute code written to help you, i dont give all the answers out you know...
-
Code:
int Password(char * p) //confirms user password with password read from file
{
cout << "please enter your password" << endl;
char pw[101];
cin >> pw;
if password entered same as password read in
return 1;
else
return 0;
}
int ConfirmCode(char * c) //confirms confirmation code same as read from file
{
cout << "enter confirmation code" << endl;
char cc[101];
cin >> cc;
if confirmation code entered is same as confirmation code read in
return 1;
else
return 0;
}
int main()
{
char ThePassword[101];
char TheConfirmCode[101];
ifstream A ( "Startup.txt" ); //open A to read data from file Startup.txt
if ( A.is_open() ){ //if Startup.txt open for reading
A.getline(ThePassword, 100); //read into a variable called ThePassword
A.getline(TheConfirmCode, 100); //read into a variable called TheConfirmCode
A.close(); //close A
}
int x;
x = Password(ThePassWord); //call Password.
if(x == 1)
x = ConfirmCode(TheConfirmCode); //call ConfirmCode
if(x == 1)
//run rest of program
else
cout << "incorrect confirmation code entered" << endl;
else
cout << "incorrect password entered" << endl;
}
-
>> But it would usefull for me if I know how he starts reading after a number of characters and stop reading after the size of the ThePassword and the TheConfirmCode I allready tried much but I think I need to use something with strlen, thx anyway.
Why do you need to know that? It looks like your password and confirmation were each written on separate lines (the endl adds a new line when you write them out). When you read them in you just read in a whole line, it doesn't matter how long the password is.
-
It's just to easy to read the ThePassword from the Startup.txt so I thought to add some characters in to the file so that you wont really notice just the ThePassword and the TheConfirmCode, but with only getline() it wont really works, I need to skip characters that he wont read all characters on that line.
-
I see. If you want to add extra characters then there are several ways you could do it. It is a basic form of encryption. You could write out the number of characters in the password to the file, so that you know how many characters in the line belong to the password and how many are extra garbledness you added. You write out/read in the integer first, then the rest of the line is the password and extra characters. You could also intersperse the extra characters into the password, as long as you remember where you did that. For example, you could insert an extra character between every password letter when you write out the password, and when you read it in, you remove every other letter.
As long as the method you use to write it out follows the same format as how you read it in, you will be fine, you just have to make sure that you write all the information you need to the file.
-
Yeah that's was my idee but I don't have any idee how to do that, I tried somethings but till now on nothing worked.
-
One of the simplest forms of encryption is 'xor'.
It is simple to implement and reasonably secure. If you are using it for use on your home computer it is probably your best option.
Here is a snippet courtesy of major_small.
Code:
/*******************************************************************************
XOR.cpp
This function takes in two strings (value,key) and uses XOR encryption to
encrypt (and decrypt) the value using the key. it returns the encrypted
value as a C++ string.
John Shao
For more on XOR encryption: http://www.cprogramming.com/tutorial/xor.html
********************************************************************************
This work is hereby released into the Public Domain. To view a copy of the
public domain dedication, visit
http://creativecommons.org/licenses/publicdomain/
or send a letter to
Creative Commons
559 Nathan Abbott Way
Stanford, California 94305
USA.
*******************************************************************************/
#include <iostream>
using std::string;
string XOR(string value,string key)
{
string retval(value);
short unsigned int klen=key.length();
short unsigned int vlen=value.length();
short unsigned int k=0;
short unsigned int v=0;
for(v;v<vlen;v++)
{
retval[v]=value[v]^key[k];
k=(++k<klen?k:0);
}
return retval;
}
/******************************************************************************/
/* Here's a Test Program */
/******************************************************************************/
#include<iostream>
int main()
{
std::string value("what is your name");
std::string key("bonebreaker");
std::cout<<"Plain text: "<<value<<"\n\n";
value=XOR(value,key);
std::cout<<"Cipher text: "<<value<<"\n\n";
value=XOR(value,key);
std::cout<<"Decrypted text: "<<value<<std::endl;
std::cin.get();
return 0;
}
Of course, should you require more information on encryption you can always google for it.
:confused:
-
First, you should have a working program without the "encryption". If it doesn't work by simply writing out the password and reading it in, then fix that first. Then, add the shell of a function called Encrypt that takes a string and returns a string. Start out with it just returning the string exactly as is. Make a similar function called Decrypt that does the same thing. Once those functions compile, add code to the part of your program that writes out the password. This code should get the new password and confirmation from the user the same way, but before it writes them out to the file it should modify them by calling encrypt, for example:
Code:
ThePassword = Encrypt(ThePassword);
TheConfirmCode = Encrypt(TheConfirmCode);
In the beginning, that call won't actually change anything, but once you implement the Encrypt function it will.
Do the same for the code that reads in the password and confirm code from the file, excet call Decrypt on them after you read them in. Again, the Decrypt function won't do anything yet, but you want to set it up so that it will.
Once you have all that compiling and running, you can start on the encryption and decryption. Start with something simple. For example, to "encrypt" the password, you add a question mark to the end. To "decrypt" the password, you remove the last character (which should be a question mark). Each time you add something, make sure it works, and next thing you know, you'll have password encryption working in your program.
-
Ok thx, I didn't know of XOR, I gonna try some now.
-
You posted just after me, so be sure to read my post just above yours. You could/should use the XOR encryption once you get to the last part.
-
Ok thx again but when I encrypt it with XOR, (Q: ) Isn't that enough, I mean should I add the characters or is only encrypthing enough?
-
It depends on what you mean by enough. Virtually any encryption can be broken given the time and resources. Which type of encryption you use depends on how safe you want the data to be. You don't need to mix the XOR encryption with your simple character adding version. You could if you wanted to, but it would probably not be necessary.
Assuming you are just doing this project for fun, and you don't work for the CIA or NASA or something, I would try doing your own simple encryptions first for practice, then switch to the XOR version to see if you can get that to work.
-
I think the program treenef posted is usefull because (the value of) key needs to be the (at least) as long as ThePassword (to encrypt) and ThePassword is a string that you can give a value (value = value not value = value) and that value can be different in size, so ThePassword (you gave a value (because you want to change the password)) can be different in size so you can use something to measure the size of the entered string ThePassword and give key the same size with some random characters. Ok, now I don't know how or what, I can't really understand treenef's post but I thought he is doing what I explained (measure the entered string, give key the same or longer value and encrypt it with that) now I like to use char ThePassword; as a string not string; if someone can give me an explaination of treenef's post or tell me how to do that it would be great, thx.
***EDIT***
I think I found a way: use something like strlen to know how long the string ThePassword, make a loop as for and use in for something like strcal to add string and let it stop when the int (i or x, whatever) is equel to the strlen. Still need to test this so I could be wrong.