Well i am a starter in programming and need ur help. M using older version of borland c++ for programming. M trying to make a program which adds users login and password information and also verifies the correct login and password by accessing the contents of those files. but there is some problem in coding and m not able to get it so plzzz help me out. Also tell me a method if possible to encrypt the password and hide it from showing on user screen instead of password it shud show "*" eg password is asdf then while prompting for password it shud show ****.

Here is ma code. Need ur help.............

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<string.h>
int n=0;
class a
{
char u_name[20],u_pass[12],uname[20],upass[12];
public:
s()
{
	 int iNum1,iNum2,i,j;
	 iNum1=strcmp(uname,u_name);
	 iNum2=strcmp(upass,u_pass);
	 if(iNum1==iNum2==0)
	     {
	       n=1;
	       cout<<"\n\n\n\n\n\n\n\n\n\t\t\t ------ USER AUTHENTICATED ------";
	       return n;
	      }
	 else
		{
		 n=2;
		 cout<<"\n\n\n\n\n\n\n\n\n\t\t\t ------ WRONG PASSWORD ------";
		 return n;
		}
}
void getdata()
{
		cout<<"\n\n\n\n\n\n\t\t\t\t USERNAME -  ";
		gets(u_name);
		cout<<"\n\n\t\t PASSWORD (12 digit max.) -  ";
		gets(u_pass);
}
void getpass()
{
	cout<<"\n\n\n\n\n\n\t\t\t\t USERNAME -  ";
		gets(uname);
		cout<<"\n\n\t\t PASSWORD (12 digit max.) -  ";
		gets(upass);
}
void outdata()
	{
		cout<<"\n\n\n\n\n\t\t\t UserName:- ";puts(u_name);
		cout<<"\n\n\t\t\t Password :- ";puts(u_pass);
	}
};
void main()
{
	a obj;
	ifstream ifile;
	ofstream ofile;

			int c;
			clrscr();
			cout<<"1.Enter\n2.Check\n3.Display";
			cin>>c;
			switch(c)
			{
			case 1:
			ofile.open("pass.dat",ios::app|ios::binary);
			int n1;

		cout<<"\nENTER THE LIST OF RECORDS TO INPUT : ";
		cin>>n1;
		clrscr();
		for(int i=0;i<n1;i++)
		{
			clrscr();
			obj.getdata();
			ofile.write((char*) &obj,sizeof(obj));
		}
		ofile.close();
		break;

		case 2:
		clrscr();
		obj.getpass();
		ifile.open("pass.dat",ios::in|ios::binary);
		while(!ifile.eof())
		{
			ifile.read((char*) &obj,sizeof(a));
			if(ifile.eof())
			break;
			obj.s();
		}
	      
		ifile.close();

		switch(n)
		 {
		  case 1:
			cout<<"\n\n\t\t\t------ PRESS ENTER TO PROCEED ------";
			getch();
			break;


		  case 2:
			cout<<"\n\n\t\t\t	------ WRONG PASSWORD ------";
			cout<<"\n\t\t\t------ PRESS ENTER TO TRY AGAIN ------";
			getch();
			break;
		  }     break;

		  case 3:
			ifile.open("pass.dat",ios::in|ios::binary);
			while(!ifile.eof())
			{
				ifile.read((char*) &obj,sizeof(a));
				if(ifile.eof())
				break;
				clrscr();
				obj.outdata();
				getch();
			}
			ifile.close();
			break;
}
}

I just looked over the code quick and i don't know whats wrong. Can you tell what is not working? And you can use md5 or something if you want to hash/encrypt the passwords. Using md5 shouldn't be too hard. But it might be cracked though. Using a wordlist. and for hiding the password and showing *. Take a look at curses/ncurses.

1) Don't use void main
2) Don't use Turbo C++
3) If it is a must to use Turbo C++, try getpass() or write one on your one to simulate it. Its ain't difficult.

I just looked over the code quick and i don't know whats wrong. Can you tell what is not working? And you can use md5 or something if you want to hash/encrypt the passwords. Using md5 shouldn't be too hard. But it might be cracked though. Using a wordlist. and for hiding the password and showing "*". Take a look at curses/ncurses.

Hi thanx for visiting. M highliting the area of the code where m getting problem.
Plz tell me more about md5.

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<string.h>
int n=0;
class a
{
char u_name[20],u_pass[12],uname[20],upass[12];
public:
s()
{
int iNum1,iNum2,i,j;
iNum1=strcmp(uname,u_name);
iNum2=strcmp(upass,u_pass);
if(iNum1==iNum2==0)
{
n=1;
cout<<"\n\n\n\n\n\n\n\n\n\t\t\t ------ USER AUTHENTICATED ------";
return n;
}
else
{
n=2;
cout<<"\n\n\n\n\n\n\n\n\n\t\t\t ------ WRONG PASSWORD ------";
return n;
}
}
void getdata()
{
cout<<"\n\n\n\n\n\n\t\t\t\t USERNAME - ";
gets(u_name);
cout<<"\n\n\t\t PASSWORD (12 digit max.) - ";
gets(u_pass);
}
void getpass()
{
cout<<"\n\n\n\n\n\n\t\t\t\t USERNAME - ";
gets(uname);
cout<<"\n\n\t\t PASSWORD (12 digit max.) - ";
gets(upass);
}
void outdata()
{
cout<<"\n\n\n\n\n\t\t\t UserName:- ";puts(u_name);
cout<<"\n\n\t\t\t Password :- ";puts(u_pass);
}
};
void main()
{
a obj;
ifstream ifile;
ofstream ofile;

int c;
clrscr();
cout<<"1.Enter\n2.Check\n3.Display";
cin>>c;
switch(c)
{
case 1:
ofile.open("pass.dat",ios::app|ios::binary);
int n1;

cout<<"\nENTER THE LIST OF RECORDS TO INPUT : ";
cin>>n1;
clrscr();
for(int i=0;i<n1;i++)
{
clrscr();
obj.getdata();
ofile.write((char*) &obj,sizeof(obj));
}
ofile.close();
break;

case 2:
clrscr();
obj.getpass();
ifile.open("pass.dat",ios::in|ios::binary);
while(!ifile.eof())
{
ifile.read((char*) &obj,sizeof(a));
if(ifile.eof())
break;
obj.s();
}

ifile.close();

switch(n)
{
case 1:
cout<<"\n\n\t\t\t------ PRESS ENTER TO PROCEED ------";
getch();
break;


case 2:
cout<<"\n\n\t\t\t ------ WRONG PASSWORD ------";
cout<<"\n\t\t\t------ PRESS ENTER TO TRY AGAIN ------";
getch();
break;
} break;

case 3:
ifile.open("pass.dat",ios::in|ios::binary);
while(!ifile.eof())
{
ifile.read((char*) &obj,sizeof(a));
if(ifile.eof())
break;
clrscr();
obj.outdata();
getch();
}
ifile.close();
break;
}
}

1) Don't use void main
2) Don't use Turbo C++
3) If it is a must to use Turbo C++, try getpass() or write one on your one to simulate it. Its ain't difficult.

Thanx prabhakar 4 ur sugestion but using getpass doesnt displays the txt entered at all. I want somthing on screen to be shown. Well m having visual studio 08 but dont know how to use it do ya knw a link or sumthing so that i cud get started with.

Do you want to encrypt your file. Or just the * when the password is typed?

If you want to hide the password when user types check my post else

Google for MD5 or SHA or any such algorithm to encrypt the file so that the contents will be confidential.

Do you want to encrypt your file. Or just the * when the password is typed?

If you want to hide the password when user types check my post else

Google for MD5 or SHA or any such algorithm to encrypt the file so that the contents will be confidential.

I wish to use both things simultaneously. Actually m working on a project in c++ in which i have to put all these things to make it secure.
And if u know how to access the datbase and handle it plz tell me.

I am pretty confident that experts in this forum wont like talks about turbo c++. well, I shall give you a clue.

getch() does not echo the char typed yet reads it.
putch() can be used to put a char of your liking.

Need you know anything more than this to solve the problem?

Coming to compilers. My academics has limited me to turbo c++ for some time untill I joined this fourm, where as you can start with the right compilers. Code::Blocks, Dev C++ & Visual C++ (express edition) are 32 - bit free compilers and provides you both win32 & console application. You can start with them on the first hand.
My suggestion, Uninstall Turbo C++, Like i did

yes i will do that 4 sure bcoz m also not satisfied with it but dont know how to use visuall c++. So need help.......
thanx for ur suggestion i havnt thought of dat.
once again thanx.
and if u cud gimme some more info then i will be grateful....

for data encryption,

If you use visual c++ You can have your data in MSAcess database & all those password security becomes easier. I suggested MS Access because thats what I know. But Oracle, I heard, has many more features.

Else you want to encrypt the file with your program then There are many cryptographic techniques

The most easiest and weakest algorithm, I suppose, is Caesar cipher.

Then Instead of using complex algorithms like MD5 or all those SHAs which makes my head swing while programing. I would choose a combination of transposition & substitution algorithm. Its comparatively easier.

Google these for more details

Hope this helps:)

EDIT: The more complex the algorithm, the more security you can provide.

Just saw one of your question. You want to know how to access databases. Then turbo c just cant help you. I have accessed database with C++ not long before but I need some time to reply properly.

Use code tags :), don't just colour it in.

Thanx to all who gave comments but yet tell me how to cross check the entered login and password with that already on file. note:- login and password information stored on other file cud b more than 1.

Oh God!. Think of a way on the first hand

You have the passwords encrypted in the file Don't you.

Oh God!. Think of a way on the first hand

You have the passwords encrypted in the file Don't you.

No i dont have passwords encrypted on file but yes i have solved the problem for displayin "*" on the password screen and hiding the original text but putch() was not of much use.
Well if u can provide me some hints on how to save passwords safely in a file containing list of many usernames and passwords and crosscheck them before granting a user to access his\her accnt.
Thanx for ur post.

#include <iostream>
#include <string>
using namespace std;

string encrypt(string data, const string& key);

int main(int argc, char **argv) {
	const string key = "lolz";
	string test = "I'm C++ string";
	cout << "before encryption: ";
	cout << test << endl;
	test = encrypt(test, key);
	cout << "after encryption: ";
	cout << test << endl;
	test = encrypt(test, key);
	cout << "same as input string: ";
	cout << test << endl;
	return 0;
}

string encrypt(string data, const string& key){
	for (size_t i = 0; i < data.size(); ++i){
		data[i] ^= key[i%key.size()];
	}
	return data;
}

Well, I guess you must thank IVAILOSP, for writing the code for you. Since passwords are small, Its difficult to decipher this poly alphabetic substitution algorithm, unless the crypt analyzer has the key "lolz".

Hints:

1) You may use this algorithm to encrypt the password & store in file
2) retrieve & decrypt the string using this algorithm again.
( In this case encryption is same as decryption )
3) compare with what you got from user.

4) Use a different algorithm for added safety for the data to be stored.

oncve again thanx prabhakar and ivailosp......
now cud u guyz help me out with databases....

didn't I give a link to learn ODBC database connectivity

#include <iostream>
#include <string>
using namespace std;

string encrypt(string data, const string& key);

int main(int argc, char **argv) {
	const string key = "lolz";
	string test = "I'm C++ string";
	cout << "before encryption: ";
	cout << test << endl;
	test = encrypt(test, key);
	cout << "after encryption: ";
	cout << test << endl;
	test = encrypt(test, key);
	cout << "same as input string: ";
	cout << test << endl;
	return 0;
}

string encrypt(string data, const string& key){
	for (size_t i = 0; i < data.size(); ++i){
		data[i] ^= key[i%key.size()];
	}
	return data;
}

thanx for helping but this code isnt workin and m not able to solve the errors showing in visual c++ 08, as well as in turbo c++. Please verify this out.......

>this code isnt workin
This is a waste of keystrokes. Do you go to the doctor, say "I don't feel good", and expect an accurate diagnosis? Do you go to the mechanic, say "My car is broken", and expect the actual problem to get fixed? If you answered no to either of those, why would you say to us "This code isn't working", and expect any response other than "Be more specific!". Seriously, it doesn't take much brain power to figure out that we need more information to help you, and if you can't understand that much, you have no business writing code.

>m not able to solve the errors showing
The code compiles and runs fine for me on Visual Studio. Maybe if you told us what errors you're getting... :icon_rolleyes:

Works great in Dev C++ too!

Little tips. May be they are useful.

While compiling in turbo c++ change #include<iostream> to #include<iostream.h> same goes for "string" I believe you know this already

I remember you saying that you have never worked with Visual C++. While selecting the type of project did you select console or Win32?

>While compiling in turbo c++ change
> #include<iostream> to #include<iostream.h> >same goes for "string" I believe you know this already
First, when compiling in Turbo C++...don't[1]. Second, changing <string> to <string.h> won't fix anything because those two headers are completely different. <string.h> holds the C string library functions and types while <string> hold the C++ string class. The two headers are not interchangeable.

[1] Unless it's Turbo C++ Explorer, which is a modern compiler in the same product line.

Oh my god! Guess I have to throw my book. I am learning everything wrong.

well thanx for quotes naure and prabhakar. Cud u guyz prefer me a book on visual c++ so that i cud start with it.

Hello evryone. well i have installed visual studio 2008 professional few days ago and m willing to create a database application. So in order to get started with ODBC database can any one give me some tips......:icon_question:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.