LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-10-2005, 08:14 PM   #1
J_K9
Member
 
Registered: Nov 2004
Distribution: Slackware 11, Ubuntu 6.06 LTS
Posts: 700

Rep: Reputation: 30
C++ password-protected entry into program


Hey,

I've just started programming in C++, and after reading (quite a lot) today, I managed to put together an extremely crude login for a terminal based application. My code is as follows:
Code:
#include <iostream>

//An attempt at password protection
//By J_K9   -- 10/07/2005

int main()
{
	const int passwd = 13579;
	int input_passwd;
	std::cout << "Please enter your password: ";
	std::cin >> input_passwd;
	if (input_passwd == passwd)
		std::cout << "\nEnter!\n";
	else
		std::cout << "\nSorry! Wrong password!\n";
	return 0;
}
I have compiled this and I know it works, but I had an odd complaint from g++ when I tried replacing the "int" values with "char", and changing the passwd to "lycrolite" (including speech marks). The error I received was:
Code:
jk9@ubuntu:~/CPP/000my_stuff/pass_prot2$ g++ passprot2.cpp
passprot2.cpp: In function `int main()':
passprot2.cpp:8: error: invalid conversion from `const char*' to `char'
I don't really understand this error...invalid conversion? I declared my constant and my variable, only initialized my constant, but the compiler is telling me that there is an invalid conversion?

Anyway, I know that this is a very crude way of password protection, and I'm guessing it's insecure(?), but I was wondering what other ways there are of password protecting something in C++ code. Thanks in advance,

J_K9

P.S Is it possible to decompile an a.out file to view the original C++ source?

Last edited by J_K9; 07-10-2005 at 08:15 PM.
 
Old 07-10-2005, 09:30 PM   #2
dugas
Member
 
Registered: Jul 2004
Location: louisiana
Distribution: fedora 4/kubuntu
Posts: 116

Rep: Reputation: 15
password protection

I altered your code:
[CODE
]#include <iostream.h>
#include <string.h>
using namespace std;
//An attempt at password protection
//By J_K9 -- 10/07/2005

int main()
{
const string passwd("password");
string input_passwd;
cout << "Please enter your password: ";
cin >> input_passwd;
if (input_passwd == passwd)
cout << "\nPassword matches!\n";
else
cout << "\nSorry! Wrong password!\n";
return 0;
}
[/CODE]
Notice the change, I included the string class. If you do not use this class, you must use character arrays. Read up on it. Hope this helps.
 
Old 07-10-2005, 09:52 PM   #3
J_K9
Member
 
Registered: Nov 2004
Distribution: Slackware 11, Ubuntu 6.06 LTS
Posts: 700

Original Poster
Rep: Reputation: 30
Okay now you've got me interested... I don't usually tend to use "using namespace std;", as I prefer to declare std::cout, std::cin etc...If I did it my way (using std::xxx) would the "string" values be std::string or not? (I don't think so but just checking).

I will definitely read up on string classes and character arrays (:O), but can I just quickly ask what that did, and why it works whereas mine didn't? In my book (SANS Teach Yourself C++ in 24 Hours) it didn't mention the "string" value, which I actually used to use a lot with BASIC variables. I thought that from the information provided in the book, the only values allowed for strings were "char" and "float", but obviously "string" also exists....Yet, why do you need to "#include string"? Does this mean that "string" is not a variable type? Please teach me what's going on ! Thank you very much for your help so far!

edit: Oh yeah, just remembered, is it possible to decompile an a.out file so that you can see the original source code? I don't think so but I just want to check, because if not that would really defeat the purpose of my code!

J_K9

P.S I've been told to buy Accelerated C++: Practical Programming by Example and read that instead of my current book....

Last edited by J_K9; 07-10-2005 at 09:58 PM.
 
Old 07-12-2005, 01:11 AM   #4
dugas
Member
 
Registered: Jul 2004
Location: louisiana
Distribution: fedora 4/kubuntu
Posts: 116

Rep: Reputation: 15
why include <string>

When you include <string>, you are linking the sting class. The string class is a pre-defined class. You may also create user defined classes. This is using an object oriented approach to programming in C++. Read up on classes. They are used in many different languages.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Password Protected Folder's vezril Linux - Security 2 06-06-2005 11:26 PM
Password protected Apache ne21 Linux - Software 5 07-30-2004 01:08 AM
Password Protected Folder? crab_2004 Linux - Software 2 02-22-2004 10:50 PM
password protected directories dsgdevil Linux - General 1 02-17-2003 12:02 AM
Password Protected Directories TheSockMonster Linux - Security 2 05-31-2002 04:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration