Putting a password on the prog

This is a discussion on Putting a password on the prog within the C++ Programming forums, part of the General Programming Boards category; Hey all, How would go about putting a 2-digit password or w/e into the code so the person who is ...

  1. #1
    Registered User Inferno's Avatar
    Join Date
    Nov 2003
    Posts
    24

    Putting a password on the prog

    Hey all, How would go about putting a 2-digit password or w/e into the code so the person who is running the program would have to know the password to gain access to the info the program holds? Also would I make case for when somebuddy types in the password or the wrong password. For example it would say "Correct password Welcome" or "wrong password please try again."

  2. #2
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Just basic stuff.

    Code:
    #include <iostream>
    #include <string>
    
    int main ( void )
    {
    	
    	std::string Password;
    	
    	std::cin >> Password;
    	
    	if ( Password == "Pw" ) std::cout << "Correct";
    	else std::cout << "Error";
    	
    	return 0;
    	
    }
    [edit]
    Fixed code..
    Last edited by Vicious; 09-13-2004 at 08:24 PM.
    What is C++?

  3. #3
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Code:
    #include <iostream>
    #include <cstring>
    
    int main(char argc, char **argv)
    {
        if (argc < 2)
        {
            std::cout << "The correct usage is <programname> <password>";
            return(-1);
        }
    
        if (strcmp(**argv, "Your_Password_Here") != 0)
        {
            std::cout << "Invalid password.";
            return(-2);
        }
    
        // Your program code.
    
        return(0);
    }
    I believe this will work. Not sure on the strcmp() though.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    close lithorien. you are actually dereferencing the double pointer back to its char value though.

    Code:
    #include <iostream>
    #include <cstring>
    
    int main(char argc, char **argv)
    {
        if (argc < 2)
        {
            std::cout << "The correct usage is <programname> <password>";
            return(-1);
        }
    
        if (strcmp(argv[1], "pass") != 0)
        {
            std::cout << "Invalid password.";
            return(-2);
        }
    	else
    	{
    		std::cout << "Password is OK";
    	}
    
        // Your program code.
    
        return(0);
    }

  5. #5
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Eee-whoops. I knew something didn't look right about it. Thanks!

  6. #6
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Inferno,

    I think I should explain the differences in the two examples here.

    My example is asking the user for the password after he has executed the program.
    bithub/lithoriens example is done from the command line

    e.g.
    Code:
     C:\Prog.exe Pw
    Pw being the password

    Either one works, its up to you to choose.

    You have 10secs before this post self-destructs.
    What is C++?

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    >if ( Password == "Pw" )
    Of course, the next problem is how to stop the inquisitive types with a hex editor from simply opening up your executable and seeing the password as clear as day.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User Inferno's Avatar
    Join Date
    Nov 2003
    Posts
    24
    thank you very much everyone

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    35
    Quote Originally Posted by Salem
    >if ( Password == "Pw" )
    Of course, the next problem is how to stop the inquisitive types with a hex editor from simply opening up your executable and seeing the password as clear as day.
    Maybe you could ask for the pw and then convert the user input to binary.

    >if ( Password == *some binary number* )

    He'd have to convert it to ascii first

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You can make it hard to find the password in your exe, but you can't make it impossible.

    What you could do is encrypt part of the program code so that the password is required for correct decrypting.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Write your password string characters inverted to a file, open the file and copy the inverted string back to your source file.

    Suppose you want "ab" as password, do this in another program:

    Code:
    FILE *fp = fopen("c:\\test.txt", "w");
    if (fp)
    {
      fprintf(fp, "%c%c", (char)~'a', (char)~'b');
      fclose(fp);
    }
    This program will write "" to file and then you can copy that to your source file. This way it gets a little harder to decipher the binary.

  12. #12
    Registered User Inferno's Avatar
    Join Date
    Nov 2003
    Posts
    24
    so if i were to use salems example it be like this


    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        char Password[100]
        
        cout << " blah blah 2 digit password\n";
        cin >>name<<\n;
        if ( Password == "69" )
        cout >> "blah\n";
    
      system("PAUSE");	
      return 0;
    }

  13. #13
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    One interesting method for the password I just thought of would be to have the password set up so that the first half when processed a certain way was equal to the second half.

    Like the password might be "ab". Then it would reverse the odd characters and if they were equal to the even characters then the password would be correct.

    The good part is the password would not be stored internally and should be more difficult to break. The bad part is, unless coded to have something specific for each person, that any correct password would pass.
    Last edited by Frobozz; 09-14-2004 at 03:05 PM.

  14. #14
    Registered User Inferno's Avatar
    Join Date
    Nov 2003
    Posts
    24
    how exatctly would i go about encrypting the code would i seperate into two files and then use a file encrypter to lock just one ?????

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    It really doesn't matter how complicated you make it
    At some point, cracking the password becomes too hard
    Code:
    if ( something_complicated(user,secret) == 0 ) {
      // the good stuff
    }
    Your attacker simply changes the code to be
    Code:
    if ( something_complicated(user,secret) != 0 ) {
      // the good stuff
    }
    Which is usually a simple matter of editing one instruction in the machine code.

    So instead of getting the password right, you just have to get the password wrong
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem reading a password from a file.
    By medeshago in forum C Programming
    Replies: 15
    Last Post: 12-21-2008, 06:20 AM
  2. [Q]Hide Password
    By Yuri in forum C++ Programming
    Replies: 14
    Last Post: 03-02-2006, 02:42 AM
  3. written command line password generator
    By lepricaun in forum C Programming
    Replies: 15
    Last Post: 08-17-2004, 08:42 PM
  4. password prog
    By ihsir in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-06-2002, 05:39 AM
  5. password
    By hammers6 in forum C Programming
    Replies: 1
    Last Post: 10-10-2001, 12:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21