GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Jan-2007, 10:14
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 167
dabigmooish will become famous soon enough

creating password protected files


Hi, I'm creating a program that will be updating/adding data to several files. I would like the program to prompt the users for a password/login before it allows them to make changes. Is there anyway I can do this without using a database to store the passwords/usernames? If anyone has any good links to a website that explains how to do this, that would be great too. Thanks for all the help
__________________
"To argue with a person who has renounced the use of reason is like administering medicine to the dead."
-Thomas Paine
www.sullivan-county.com/deism.htm
  #2  
Old 10-Jan-2007, 23:49
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: creating password protected files


The easiest way is to read and write a login file -- in binary. If you don't need the password to be extremely secure, you can encrypt the password using any simple algorithm that you can find using Google and output the values as long integers once encrypted.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #3  
Old 11-Jan-2007, 10:10
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 167
dabigmooish will become famous soon enough

Re: creating password protected files


Thanks for the suggestion Walt. I've now hit a different issue. I'm trying to create a function that will remove old users from the password file. The program prompts for who you would like to remove, and then searches the file for that user. Currently the only way I can think of removing the user it to dump all the users out of the file (into a linked list), find the user I want to remove, delete his node in the list, and then dump the list back into the file (overwrighting it in the process). Can anyone think of a better way to do this? My method seems a bit "bulky" for something this simple.
__________________
"To argue with a person who has renounced the use of reason is like administering medicine to the dead."
-Thomas Paine
www.sullivan-county.com/deism.htm
  #4  
Old 11-Jan-2007, 14:25
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 167
dabigmooish will become famous soon enough

Re: creating password protected files


okay, here is my latest problem. This is related to my original post so I'm not creating a new one. I have a little program that will add/remove new users to a binary file. It seems to be adding users fine. I say seems to, because I can't view the file to be sure. I created a view function but it seems to be printing junk if more then one entry is in the file. I haven't even touched the remove function because it just seems to crash the program and delete the file. Here is my code. Any help would be, well helpful:

CPP / C++ / C Code:
/********************************************************/
/* File Name: LogIn.cpp                                 */
/* Programmer: Dominic Perry                            */
/* Created on: 01/11/07                                 */
/* Version: 1.0                                         */
/*                                                      */
/* Purpose: Program will maintain a login "database"    */
/*          for use with autosys jill updater program   */
/*          This program will add/remove new users      */
/********************************************************/


#include <stdio.h>
#include <fstream.h>
#include <string.h>

using namespace std;

#define PATH "c:\\testdata" //path to all the files
#define MESSAGE1 "add a new user"
#define MESSAGE2 "remove a current user"
#define MAXUSERS 25

struct logInInfo //holds the login info for each person
{
	string userName;
	string password;

} strctArray[25];

bool AddUser(logInInfo *logIn);
bool Verify(string message);
bool SearchAndRemove();
void ViewUsers(logInInfo *logIn);

int main()
{

	logInInfo logIn; 
	char menuChoice; 
	bool works;
	bool con;

	do{
		cout <<endl<<endl<<"What would you like to do?"<<endl;
		cout <<"[A] Add a User."<<endl;
		cout <<"[R] Remove a User."<<endl;
		cout <<"[V] View current users."<<endl;
		cout <<"[Q] Quit."<<endl;
		cout <<"Which choice: ";
		cin >> menuChoice;

		switch(menuChoice)
		{
			case 'a':
			case 'A':
			case '1': 
				con = Verify(MESSAGE1);
				if(con == true)
				{
					works = AddUser(&logIn); 
				}
			break;

			case 'r':
			case 'R':
			case '2':
				con = Verify(MESSAGE2);
				if(con == true)
				{
					works = SearchAndRemove();
				}
			break;
			
			case 'v':
			case 'V':
			case '3':
				ViewUsers(&logIn);
				works = true;
			break;



			case 'q':
			case 'Q':
				works = true;
			break;

			default:
				cout <<endl<<"That is not a valid choice!"<<endl;
		}

	}while(menuChoice != 'Q' && menuChoice != 'q');

	if(works == true)
	{
		cout <<endl<<"All updates completed"<<endl;
	}
	else
	{
		cout <<endl<<"Some updates could not complete.  Please rerun the program."<<endl;
	}

	return(0);


}

bool Verify(string message)
{
	char answer;

	cout <<"Are you sure you want to "<< message << "?[Y or N] ";
	cin >> answer;

	if(answer == 'Y' || answer == 'y')
	{
		return(true);
	}
	else
	{
		return(false);
	}

}


bool AddUser(logInInfo *logIn)
{
	bool returnValue;
	ifstream::pos_type size;

	fstream outputFile(PATH"\\dwor", ios::out | ios::ate | ios::binary); //opens the file with the pointer
									     // positioned at the end	
	if(!outputFile.is_open())
	{
		returnValue = false;
	}
	else	
	{

		size = outputFile.tellg();	 //gets the size of the file
		
		/****Debug PRINT STATEMENTS****/
		cout <<endl<<"Size: "<< size <<endl;
		cout <<"sizeof(logInInfo): " << sizeof(logInInfo) <<endl;
		/******************************/

		size = size / sizeof(logInInfo); //this takes the size of the file and divides it by the size of 
						 //the struct in the file. This will tell you how many structs 
						 //are in the file		
		/****DEBUG PRINT STATEMENT****/
		cout <<"Adjusted size: " << size <<endl; //this tells me how many entries are in the file
		/*****************************/

		if(size >24) //if the file is too big do not add anymore users
		{
			returnValue = false;
		}
		else
		{
			cout << "Please enter the new user name: ";
			cin >> logIn->userName;
			cout <<endl<<"Please enter "<< logIn->userName <<"'s password: ";
			cin >> logIn->password; 
	
			outputFile.write((const char *)logIn, sizeof(logInInfo)); //write the new user to the file
			returnValue = true;
		}
		outputFile.close(); //close the file
	}

	return(returnValue);
}

bool SearchAndRemove()
{
	logInInfo *strctArray[MAXUSERS];	//create an array of logInInfo pointers
	ifstream::pos_type size;		//holds the size of the file
	fstream filePtr; 			//file pointer
	string user;			 	//user to search for


	cout <<"Which user would you like to remove? ";
	cin >> user;

	filePtr.open(PATH"\\dwor", ios::in | ios::binary | ios::ate); //open the file with the pointer at the end

	if(!filePtr.is_open()) //checks to see if the file opened
	{
		return(false);
	}
	else
	{

		size = filePtr.tellg();		 //gets the size of the file
		size = size / sizeof(logInInfo); //this takes the size of the file and divides it by the size of 
						 //the struct in the file. This will tell you how many structs 
						 //are in the file
		filePtr.seekg(0, ios::beg);	 //sets the file pointer back to the begining.
		
		for(int i = 0; i < size; i++) //fills your array
		{
			filePtr.read((char*)strctArray[i], sizeof(logInInfo));
		}
		
		filePtr.close(); //closes the file

		filePtr.open(PATH"\\dwor", ios::out | ios::trunc | ios::binary);  //recreates the file


		for(int i = 0; i < size; i++) //adds the users back omiting the "deleted" user
		{
			if(strctArray[i]->userName != user)
			{
				filePtr.write((char*)strctArray[i], sizeof(logInInfo));
			}
		}

		filePtr.close();
	}
		
	
	return(true);
}

void ViewUsers(logInInfo *logIn)
{
	ifstream::pos_type size; //holds the size of the file
	fstream inputFile;

	cout <<endl<<endl<<endl<<endl<<endl; //Prints blank lines for easier reading
	
	inputFile.open(PATH"\\dwor", ios::in | ios::binary | ios::ate); //opens the file wit the pointer at the end
	
	if(!inputFile.is_open()) //checks if the file is open or not.
	{			 
		cout <<"Could not open file"<<endl;
	}
	else
	{
		size = inputFile.tellg(); //gets the size of the file

		/****DEBUG PRINT STATEMENTS****/
		cout <<"Size: "<< size <<endl;
		cout <<"sizeof(logInInfo): "<< sizeof(logInInfo) <<endl;
		/******************************/

		size = size / sizeof(logInInfo); //this takes the size of the file and divides it by the size of 
						 //the struct in the file. This will tell you how many structs 
						 //are in the file
		/****DEBUG PRINT STATEMENT****/
		cout <<"Adjusted Size: " << size <<endl;
		/*****************************/

		inputFile.seekg(0, ios::beg); //puts the file pointer at the begining of the file again


		for(int i = 0; i < size; i++) //since the number of entries is know we only print that many
		{
			inputFile.read((char*)logIn, sizeof(logInInfo)); //fill the struct with the data
			cout <<"U: "<< logIn -> userName <<endl; //print what we just filled in
			cout <<"P: "<< logIn -> password <<endl;
			cout <<"--------------------------------"<<endl;

			/****DEBUG PRINT STATEMENT****/
			cout << i << endl; 
			/*****************************/
		}

		inputFile.close(); //close the file

	}

}
__________________
"To argue with a person who has renounced the use of reason is like administering medicine to the dead."
-Thomas Paine
www.sullivan-county.com/deism.htm
  #5  
Old 11-Jan-2007, 15:50
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,303
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: creating password protected files


Quote:
Originally Posted by dabigmooish
CPP / C++ / C Code:

struct logInInfo //holds the login info for each person
{
	string userName;
	string password;

} strctArray[25];

			outputFile.write((const char *)logIn, sizeof(logInInfo)); 

What is sizeof(logInInfo)? For a 32-bit system, I would expect it to be 8. The string objects themselves are stored somewhere else, and the struct elements point to them. So... you aren't actually writing the strings each time, you are writing two pointers. Note that the size of the struct is fixed, but the strings themselves can have any length. See footnote.

Everything else from here on has to be downhill because:

The next time that you run the program, when you read pointer values in from a file, they don't necessarily point to anything legal within that current program space. Then, when you dereference them (by trying to store the items in memory that they are supposed to be pointing to) the program can bomb. (So, don't do it: Don't write pointers and read them back and expect something Good to happen.)


If you want to write variable-length data (like names and/or passwords) to a binary file, then in order to read them back, you must have some way of knowing when one datum ends and the next one begins. If the data can be sequences of arbitrary binary byte values, it's not simple. (That's why we use text files; the files are structured into "lines" separated by "newline" chars. Reading the "strings" is OK as long as the strings themselves don't contain any zero-byte chars.)

I think a little rethink might be in order: Either make names and passwords fixed-length arrays of chars (or whatever) so that all records have the same size, or devise some way of delimiting names and passwords so that you can read back whatever you write. For example you could use text files with some encryption scheme that always maps sequences of ascii chars to sequences of printable ascii chars so that it's easy to tell where one starts and the other ends.

Regards,

Dave

Footnote:
In general, it is never possible to write C++ std::strings using sizeof() to designate the length. (That's right:Never.) The value of "sizeof(anything)" is constant, and it is evaluated at compile time. The "sizeof" a C++ std::string is equal to the size of a pointer.
  #6  
Old 12-Jan-2007, 06:43
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 167
dabigmooish will become famous soon enough

Re: creating password protected files


well damn. That's a day of wasted time. I like the ideal of the fixed length arrays. I'll start over and see what happens. Once again thanks for all the help.
__________________
"To argue with a person who has renounced the use of reason is like administering medicine to the dead."
-Thomas Paine
www.sullivan-county.com/deism.htm
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
automatic password protected web folder reset rgrant Web Design Forum 0 30-Aug-2006 08:25
Bloodshed Dev C++ Project Options JdS C++ Forum 6 11-Nov-2005 17:23
"HTML Help Workshop" to creating help files shinyhui MS Visual C++ / MFC Forum 0 08-Aug-2005 03:52
Can't view pages from another machine on the Intranet aevans Apache Web Server Forum 9 14-May-2004 02:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:58.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.