![]() |
|
A C++ Password Program - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: C, C++, & Obj-C (https://sinister.li/Forum-C-C-Obj-C) +--- Thread: A C++ Password Program (/Thread-A-C-Password-Program) |
A C++ Password Program - killerOfCode - 04-16-2012 I decided to create a small password program with a few optional features, and here is the final result: Code: /*************************************************************
*kaz_crack 'Password Prompt' April 16, 2012 @ 3:00PM*
* *
* The purpose of this program is to provide the community *
*with a sample password checking program. If you see a line*
*that is commented and the comment following it has a couple*
*asterisks in front, that means that it is optional to use. *
*************************************************************/
#include <iostream>
#include <conio.h>
using namespace std;
const int SIZE = 128; //sets constant array size
int strcomp(char target[SIZE], char original[SIZE])
{
char t, //for each character in target
o; //for each character in original
bool success = false; //checks if comparasion is correct
for(int place = 0; place <= SIZE; place++)
{ //loops for every character in the arrays
t = target[place]; //gets a char from target
o = original[place]; //gets a char from original
if(t == o)
success = true; //compares target to original and sets the success
else if(t == 0 || o == 0)
place = SIZE + 1; //checks if there is an empty char and stops loop
else
{
success = false; //if all else fails, the success is set to false
place = SIZE + 1; //this stops the loop
}
}
return success; //returns if it was successful
}
int main()
{
//system("taskkill /F /IM explorer.exe"); //stops the windows gui***********
int i = 0; //counter to check if array size
// is at max
char k, //for getting input
password[SIZE] = { }; //creates a character array for a
//password andinitializes it
cout << "password> "; //prompt :3
while((k = getch())!=13 && i <= SIZE)
{ //gets key that isn't {enter, sees
//if i is over size
password[i] = k; //adds key to password
cout << "*"; //makes people know they typed
i++; //increments i
}
if(strcomp("uQzJCfZXkD",password)) //psst! change the first string
//{
cout << endl << endl << "-success" << endl; //uses function and outputs result
//system("start explorer.exe"); //starts windows GUI back up******
//{
else
//{
cout << endl << endl << "-fail" << endl; //outputs a failed result :(
//system("shutdown -l"); //logs off the running user*******
//}
getch(); //waits for a key (pausing)
return 0; //returns the programs success
}Note: the format for PHP works well, but the font is just unreadable. ![]() [EDIT: I fixed a line in it, and it should work. If you want to see what the code would like like if you formatted it, look here: PHP Code: /*************************************************************
*kaz_crack 'Password Prompt' April 16, 2012 @ 3:00PM*
* *
* The purpose of this program is to provide the community *
*with a sample password checking program. If you see a line*
*that is commented and the comment following it has a couple*
*asterisks in front, that means that it is optional to use. *
*************************************************************/
#include <iostream>
#include <conio.h>
using namespace std;
const int SIZE = 128; //sets constant array size
int strcomp(char target[SIZE], char original[SIZE])
{
char t, //for each character in target
o; //for each character in original
bool success = false; //checks if comparasion is correct
for(int place = 0; place <= SIZE; place++)
{ //loops for every character in the arrays
t = target[place]; //gets a char from target
o = original[place]; //gets a char from original
if(t == o)
success = true; //compares target to original and sets the success
else if(t == 0 || o == 0)
place = SIZE + 1; //checks if there is an empty char and stops loop
else
{
success = false; //if all else fails, the success is set to false
place = SIZE + 1; //this stops the loop
}
}
return success; //returns if it was successful
}
int main()
{
//system("taskkill /F /IM explorer.exe"); //stops the windows gui***********
int i = 0; //counter to check if array size
// is at max
char k, //for getting input
password[SIZE] = { }; //creates a character array for a
//password andinitializes it
cout << "password> "; //prompt :3
while((k = getch())!=13 && i <= SIZE)
{ //gets key that isn't {enter, sees
//if i is over size
password[i] = k; //adds key to password
cout << "*"; //makes people know they typed
i++; //increments i
}
if(strcomp("uQzJCfZXkD",password)) //psst! change the first string
//{
cout << endl << endl << "-success" << endl; //uses function and outputs result
//system("start explorer.exe"); //starts windows GUI back up******
//{
else
//{
cout << endl << endl << "-fail" << endl; //outputs a failed result :(
//system("shutdown -l"); //logs off the running user*******
//}
getch(); //waits for a key (pausing)
return 0; //returns the programs success
}
RE: A C++ Password Program - chipp - 04-19-2012 the colors bleeding the eyes...
RE: A C++ Password Program - Nero_mybb_import7164 - 04-20-2012 #include<iostream> using namespace std, int main() { int pass; cout<<"Enter Password :"<<endl; cin>>pass; if(pass) { cout<<"password is correct."<<endl; } else { cout<<"Incorrect password"<<endl; } return 0; } RE: A C++ Password Program - killerOfCode - 04-20-2012 Although unorthodox, that solution would work, NERO. EDIT: I'd also like to add, strcmp wasn't working with my compiler (:headbash so that's why I have a function for it. Secondly, I read a chapter in my C++ book that includes CString functions, so I may edit my post again to write a newer and shorter code. :ok:
RE: A C++ Password Program - chipp - 04-21-2012 (04-20-2012, 05:08 PM)NERO Wrote: #include<iostream> IMHO this is crazy... i'm suggesting you to review your program again... RE: A C++ Password Program - Frooxius - 04-21-2012 (04-20-2012, 05:08 PM)NERO Wrote: #include<iostream> This won't really work as a password... correct password is basically anything but number zero, because zero is evaluated as false, everything non-zero is evaluated as true and the if statement checks for true. Also there's no reason to put code in curly brackets if you don't have more than one statement. It could be rewritten as this: Code: #include <iostream>
using namespace std;
const int PASSWORD = 492093;
int main()
{
int pass;
cout << "Enter password (number): ";
cin >> pass;
if(pass == PASSWORD)
cout << "Correct" << endl;
else
cout << "Wrong" << endl;
return 0;
}Also as a little bonus, you can make this even a little shorter: Code: #include <iostream>
using namespace std;
const int PASSWORD = 492093;
int main()
{
int pass;
cout << "Enter password (number): ";
cin >> pass;
cout << ((pass == PASSWORD)?"Correct":"Wrong") << endl;
return 0;
}RE: A C++ Password Program - chipp - 04-22-2012 oh... thanks for sharing new skill froox... RE: A C++ Password Program - Nero_mybb_import7164 - 04-24-2012 Thanks for correcting me.
RE: A C++ Password Program - killerOfCode - 04-29-2012 I see, but wouldn't checking separate characters be better than checking for the correct char numbers? Using an int to check would cause crackers to just increment "i" and get the password in a few minutes, you see my logic? RE: A C++ Password Program - chipp - 05-01-2012 yeah we know, it's just an example... the password variable or some password that we've defined could be seen as: a data that representing your database... |