I am stupid:
How do I make a password protected file?
Printable View
I am stupid:
How do I make a password protected file?
>How do I make a password protected file?
There are many ways, but we need to know what type of protection your looking for.....
It's just too vague, are you looking for code that the user would have to enter in a password to continue in the program?
Or are you looking for a utility that actually encrypts your program and decrypts it when the right password is input'd(this the right word?), something along those lines?
Your question is too vague sorry, can't exactly answer that without more information.
Umm...the first one, where you have to enter a password to continue and see the other stuff.
If you're talking about writing a password-protected program, it's as easy as:
Code:std::string password; //A string to hold the input
std::cout << "Enter password: "; //Ask for the password
std::getline(std::cin, password); //Let them type a password
if(password != "my_password") //Check if they got it right
{
std::cout << "You got the wrong password!";
return 0; //If they got the password wrong, quit.
}
Try this
Code:#include <iostream>
#include <string>
int main()
{
cout<<"Enter password: ";
cin>>password;
cin.ignore();
if ( password == "mypassword" )
{
cout<<"Password correct!";
cin.get();
}
else
{
cout<<"Sorry password incorrect, Quitting";
}
}
A better method would be:
1) Get password
2) Apply encryption scheme (MD5 works)
3) Check it against password that already has been encrypted
themethod suffers one huge problem. I can open your executable with pretty much any text editor and see the password.Code:if (password == "mypassword")
>Try this
Too bad it won't work and has nothing to do with password protecting a file.
Well sorry, i'm just trying to help. You don't need to get so uptight at me!
>You don't need to get so uptight at me!
You need to chill out a bit. I wasn't trying to slam you down.
>Well sorry, i'm just trying to help.
If you don't post working code, you're not helping. Start by compiling and testing everything you post until you're confident that you can write correct code off the top of your head. It saves you the headache of people constantly correcting you, and it also saves you the headache of confusing the very people you're trying to help.
I'm sorry :(
Judging from "I am stupid:"Quote:
A better method would be:
1) Get password
2) Apply encryption scheme (MD5 works)
3) Check it against password that already has been encrypted
It sounds like that might be a little over his head :)
I know but I'm in one of those moods :)
I made this with correct syntax, but for some reason DEV gave me trouble when trying to copy / paste so I typed this. I was careful, but if it does not work like I typed it above I have made a typing mistake. This is what I would do, but for the record, I have only started learning to code 2 days ago. Although people could just pop it open with a program editor, most people have no idea about that. Here's my work. If you don't like it, don't use it. It works for me.Code:#include <iostream>
using namespace std;
int main()
{
float a_a, b_b;
b_b = 123 //replace 123 with password
cout<<"Please enter your password: \n";
cin>> a_a;
cin.ignore();
if ( a_a == b_b ) {
cout<<"Correct.\n";
cin.get();
}
else if ( a_a != b_b ) {
cout<<"Incorrect.\n";
cin.get();
}
}
The problem with that password is that you can only have numbers
Sorry....as I said, I just starting learning C++ 2 days ago. I tried for a very long time, but couldn't get anything else to work :( .Sorry, that's the best I can do as a newbie programmer.
>Sorry, that's the best I can do as a newbie programmer.
No biggie, just don't take the following review the wrong way.
>float a_a, b_b;
It's best to choose names that are meaningful to other people and yourself in the future. It saves you all kinds of heartache later on. Also, for multiple reasons but mostly because double is the default floating-point type, you should always use double unless you have a compelling reason not to.
>b_b = 123
Don't forget the trailing semicolon.
>if ( a_a == b_b ) {
I highly recommend not using floating-point data types until you know how to use them correctly. This isn't a flame or anything, it's just that floating-point is incredibly subtle and difficult to work with for just about everyone. If you aren't familiar with the pitfalls then you'll have weird problems that you won't know how to deal with.
In this case, comparing floating-point values for equality is inaccurate because the binary representation of floating-point is inherently imprecise. If you have a_a=1.1 and b_b=1.1 then they still might not be equal if a_a is 1.100001 and b_b is 1.100000 internally.
The solution is to do a "close enough" test that would be dreadfully confusing at this point in your studies, which brings me back to the initial recommendation. :)
>else if ( a_a != b_b ) {
else nothing is equivalent to else a_a != b_b.
>cin.get();
Good idea, but because you do this for every path taken, you can place it outside of the if statement.
Ignoring the floating-point issues, here's your updated code:
Code:#include <iostream>
using namespace std;
int main()
{
double pass, input;
pass = 123.0;
cout<<"Please enter your password: ";
cin>> input;
cin.ignore();
if (input == pass)
cout<<"Correct!"<<endl;
else
cout<<"Incorrect"<<endl;
cin.get();
}
Yeah...Could you explain the whole doublepass thing. Also, you may beleive the whole thing is ugly, but if you take out the spaces(which are put there automatically), it looks a lot neater. Thanks for the cin.get(); outside the if statement suggestion, it helps me learn how c++ works. :confused: How could I take this the wrong way? I'm happy that after 2 days I can offer an answer to a question answered by someone else! And yes, it may not be too pretty, but I am still happy that the syntax works! :p
>Could you explain the whole doublepass thing.
pass and input are just more informative names than b_b and a_a, respectively.
>How could I take this the wrong way?
You would be surprised at the ways my posts have been misunderstood in the past. ;)
so what does the command 'double' do?
>so what does the command 'double' do?
Basically, the same thing as float. The only difference is that double might be able to hold bigger or more precise values.
Ok...The coding is simpler, but the main obstacle to get over now is that the password can still only be numbers...Got any ideas?
You want string data. The easiest way to do what you want in C++ is to use the string class, and getline:
Compare this program with the one I posted earlier. Spot the differences and try to figure out the purpose of each of them. :)Code:#include <iostream>
#include <string>
using namespace std;
int main()
{
string pass, input;
pass = "x04G9Ytm";
cout<<"Enter your password: ";
getline(cin, input);
if (input == pass)
cout<<"Correct!"<<endl;
else
cout<<"Incorrect"<<endl;
cin.get();
}
Thantos:
MD5 is a hash scheme, not an encryption scheme. There is a very important difference between the two.
Yes but you can use it to encrypt a password since only one way is needed.
Isee what you mean now.
That's what I thought would be needed. However, I am still learning basic concepts. After trying to figure out the whole string situation for half an hour, I decided to give up and ask for someone else's input. Thanks for the code, it helps explain concepts of c++. Very valuable. :)
If you're really looking for an MD5 scheme, then here would be a good reference:
http://www.codeguru.com/code/legacy/...ms/md5_src.zip
If you don't have visual C++, you can still open the source files in notepad and go from there. You might not want to start out with this if you're a beginning coder and are just learning still, but regaurdless this will do what you want to do if you read/modify the code correctly :)
If you're just trying to get past notepad readers, then just implement a 1-1 crypto scheme, it wouldn't be that hard really, say your password is a char array of size 11 including the null terminator, here's what it would look like:
-The main holes in this are successCounter, buffer overflow when getting input for plainCipher because of no bounds checking, and the simple scheme.Code:#include <iostream>
int main(int argc, char **argv)
{
/* Program: 1-1crypto.cpp
Author: Tronic
Description: Detailing a simple 1-1 crypto algo.
Compiled In: Visual C++ 7
/*
bool access = false;/*mainly use this if you want to change/expand this code*/
int successCounter = 0;
char matchCipher[11] = "abcdefghij";
for(int i = 0; i < 10; i++)
matchCipher[i]++;
char plainCipher[11] = { 0 };
std::cout << "> ";
std::cin >> plainCipher;
for(int i = 0; i < 10; i++)
plainCipher[i]++;
for(int i = 0; i < 10; i++)
{
if(!(plainCipher[i] == matchCipher[i]))
{
access = false; //Just in case something goes wacko.
std::cout << "Access Denied";
}
else
successCounter++;
}
if(successCounter == 10)
{
bool access = true;//Might need this for later use.
std::cout << "Access Granted";
}
return 0;
}
Prelude, what makes != more precise with floating points than == ? I haven't ran into this problem yet lol, good that I find out about it now from someone.
>>what makes != more precise with floating points than ==
I don't think it is. Otherwise, you could just do:
if(!(x != y))
;)
Well, from what Prelude says it is, but that confuses me since I don't exactly know the steps behind != and ==. I would assume that == and != have a different precisity, but that's just an assumption.
It has nothing to do with != being more accurate than ==, what Prelude was addressing was the fact that a floating point number is inherently inaccurate, more of a "guess". A double is more accurate of a guess than a float, but neither is 100% exact.Quote:
Originally Posted by Tronic
For either of them, the computer may store the number 1.1 as 1.10001, or it may store 1.1 as 1.10000. It may even store two float variables defined at the same time with these two different values. So, while you print out 1.1000 for both numbers, because of the possible inaccuracy, testing for equality of those two floats may turn out false (because 1.10001 is NOT equal to 1.10000).
This means that testing for equality ( == ) is most likely to be false for any two given float variables, while testing for inequality ( != ) is most likely to be true for any two given float variables.
I have played with the way floating point variables are stored internally with gcc and it's fascinating (in a morbidly geeky sort of way).
Haha, well I suppose morbidly geeky would fascinate me as well.
Thank you for the response ^_^, I'm wondering though..how/why floats store values like this? I would think an innaccuracy like this wouldn't go un-fixed if it was fixable.
It's not an error, it's a way for the computer to handle high precision decimal numbers. Internally the number is stored in such a way that it can be used for either very large numbers (many places to the left of the decimal) or very small numbers (many places to the right of the decimal).
Basically, the machine represents the numbers in exponential format for floats (one bit for the sign, 7-11 bits (depending on float or double) for another part, and the remaining bits for the mantissa) versus using the binary nature of storage for ints.
Whereas ints are represented like such:
00000000 = zero
00000010 = two
00000100 = four
00000101 = five
etc.
A float is stored with a very different method (rather complex and can vary from compiler to compiler) in such a way that while it is not exact, it holds a much broader range. Internally it looks more like
0 0100101 010010100101010010101000
if you were to break up the bits into the different parts. (some compilers store them in different orders, but the result is the same)
Most of this is academic, unless you want to get into compiler design or very advanced tricks and stuff. The main thing to remember is this: == with floats or doubles is most likely to be false, no matter what the values of the numbers, while != is most likely to be true with floats and doubles, no matter what the numbers.
Like any tool, you use them when appropriate and remember how to use them properly. floats and doubles are useful for representing money, temperature, and anything else that might have a fractional part, ints are useful for most other things.
>I'm wondering though..how/why floats store values like this?
Good stuff
Why?Quote:
for representing money, temperature,
Due to the inaccuracy of floating-point types, isn't it better to simply use a larger integer type and divide by a set amount to get the real answer?
i.e.
Code:float fNumber = 150.25;//normal, but possibly inaccurate
long long lNumber = 150250000;//just divide this by 6 and you can get the real value. you can use == on the
long longs and just cast them to float and divide by 6 for output
Fixed-point math is indeed popular, but 6 is a weird radix. You usually use powers of 2 or 10.
Thank you Jessycat and Prelude for your help :) I'm taking a look at that pdf right now infact.
What do you mean by radix?
radix == base, from what I remember of the docs on atoi() or itoa() (both nonstandard apparently.. *sigh*) or whatever ;)
It was just an example; you can make it whatever you want (as long as it will fit in the variable).
atoi is standard, itoa is not. And yes, radix is the base of the number system. Decimal has radix 10, binary radix 2.
>>atoi is standard, itoa is not
Oh. Well, good to know at least one of them is usable :)
I use itoa a lot, and I really don't like all the compiler warnings I get.
My solution is just to rewrite it and call it my_itoa :)
i Just thought i'd take a shot at the initial problem:
i think the (strcmp) thing is right, im new to this too.Code:#include <iostream>
using namespace std;
int main()
{
char password[20];
cout<<"Enter Password: ";
cin.getline(password, 20);
if(strcmp(password, "PASSWORD")==0) // your password in ""
{
cout<<"Correct!";
.......Whatever you want it to do.....
}
else{
cout<<"Wrong!";
}
return 0;
}
As explained before, this is terrible protection because you can view it in a hex editor or disassembler (hell, even notepad!)
Encryption is a very important aspect of computer programming. As prospective computer scientists we should strive to become encryption experts. Go here to learn more.
>My solution is just to rewrite it and call it my_itoa
Or you could use sprintf, or better yet, stringstreams. That would save you the trouble of potentially introducing unnecessary bugs by reinventing an existing solution.
Unless of course you wanted to write the number out in base 5 :)
I must have missed something. Was that a requirement, or do you just work with base 5 a lot? ;)
Yeah its for when I work with pennies, nickles, and quarters. One of my professors just loves base conversions. I had to do it in pascal and in assembly. The "Read in a base 9 number and output it as a base 7 number." Fun stuff
Oh, I had to do that in Haskell. Any base between 2 and 36, and then base -2 for good measure. (Now THAT's a weird number system.)
How do you have a negative base? The base is suppose to represent the number of different symbols that are used.
In pascal I once did a base (26*2 + 10) 62 system :)
No, in the definition my professor used, a number system is done like so:
number = SUM[i = 0 to digit-count](base^i * digit-value)
So base can be any integer except -1, 0 and 1, as long as you have enough digit signs.
In base -2, with four digits you get these numbers:
0000 = 0
0001 = 1
0010 = -2
0011 = -1
0100 = 4
0101 = 5
0110 = 2
0111 = 3
1000 = -8
1001 = -7
1010 = -10
1011 = -9
1100 = -4
1101 = -3
1110 = -6
1111 = -5
etc. etc.
I think your professor had to dig deep in his arse to find that defination :)
Well...
The "negabinary" system is listed at Wikipedia as a "positional-like system with non-standard base". The German Wikipedia states in a paragraph that as a generalization of numbering systems, the base need not be a natural number as longas the absolute value of the base is greater than 1.
http://en.wikipedia.org/wiki/Negabinary
I even had to implement the addition.
Hmm interesting. Though something doesn't seem rightbut it still takes 1 extra bit.Quote:
unusual property that negative and positive numbers can be represented without a sign bit
This has gone way too off-topic.
Sportsmaniac, if you're still stuck, start a new thread with your latest code and question(s).