Having problems with a login script...

This is a discussion on Having problems with a login script... within the C++ Programming forums, part of the General Programming Boards category; Hi guys, i have been working on this database program for a while now and everything works perfect, except the ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434

    Having problems with a login script...

    Hi guys, i have been working on this database program for a while now and everything works perfect, except the login. The login basically takes the users password and username, and (its been a while im trying to remember) ok heres what it does:

    (1) Gets information -- Appears to be working fine
    (2) Adds (via the + operator) each character of the two arrays together (the password's first letter with the usernames first letter, and so on)
    (3) Then it splits the new array in half (if the array has an odd number of elements it adds an x on the end) and XOR's one half with the other.
    (4) Then character by character it compares the stored encrypted info (saved in a text file on the computer) with the newly encrypted information.
    (5) If they match then the logged in bool is set to true and it then breaks the while loop and moves on to the rest of the program.

    The problem is that it doesnt match every time, depending on what programs are running in the background the encrypted information changes. I suspect a variable is not being initialized or something along those lines, but i just checked and i couldnt find anything. I even went back and initialized all the char arrays. Still wont work. So heres the code for the login. If anyone can spot the problem i would appreciate it. Thanks!

    Code:
    ...
    //infinite loop
        while(1)
        {
            //title
            cout<<"**************************\n";
            cout<<"*     Black Squadron     *\n";
            cout<<"*        Database        *\n";
            cout<<"**************************\n\n";
            //login
            //init all vars
            char username[25];
            char password[25];
            char encinfo[25];
            char tempenc[25];
            char tempencI[25];
            char tempencII[25];
            //initialize all strings;
            username[0]='\0';
            password[0]='\0';
            encinfo[0]='\0';
            tempenc[0]='\0';
            tempencI[0]='\0';
            tempencII[0]='\0';
            int xleng=0;
            int i=0;
            //get information
            cout<<"Username: ";
            cin.getline(username, 25);
            cout<<"Password: ";
            cin.getline(password, 25);
            cout<<"\nPress enter to begin encryption...\n";
            cin.ignore();
            //encrypt
            for(i=0; i<25; i++)
            {
                tempenc[i]=username[i]+password[i];
            }
            xleng = strlen(tempenc);
            if((xleng%2)!=0)
            {
                tempenc[xleng+1]='x';
                xleng = strlen(tempenc);
            }
            for(int j=0; j<(xleng/2); j++)
            {
                tempencI[j]=tempenc[j];
            }
            i=0;
            for(int k=(xleng/2); k<xleng; k++)
            {
                tempenc[i]=tempenc[k];
                i++;
            }        
            for(int l=0; l<xleng; l++)
            {
                encinfo[l]=tempencI[l]^tempencII[l];
            }
            //convert non-standard characters to standard
            for(int m=0; m<(strlen(encinfo)); m++)
            {
                encinfo[m]=char(((int(encinfo[m]))%26)+65);
            }
            //END ENCRYPTION
            //check result with known result
            //get import variables
            char ch;
            char importloc[115]="C:/Documents and Settings/Douglas/My Documents/BlackSquadron/Department of Intelligence/Program Stuff/known.cmc";
            char importcode[16];
            importcode[0]='\0';
            i=0;
            //import
            ifstream fin(importloc);
            while(fin.get(ch))
            {
                importcode[i]=ch;
                i++;
            }
            fin.close(); 
            //evaluate
            //temp
            cout<<importcode<<"\t"<<encinfo;
            cin.ignore();
            //end temp.
            //eval. vars
            int encleng=0;
            int evalnum=0;
            cout<<flush;
            //temp must term. in final
            ofstream fout(importloc);
            fout<<encinfo;
            fout.close();
            //end temp.
            //evaluate both known and new info
            if(strlen(importcode)==strlen(encinfo))
            {
                encleng=(strlen(encinfo));
            }
            for(i=0; i<encleng; i++)
            {
                if(encinfo[i]==importcode[i])
                {
                    evalnum++;
                }
            }            
            //temp
            cout<<evalnum<<"\t"<<encleng<<endl;
            cin.ignore();
            //end temp    
            if(evalnum==encleng)
            {
                cout<<"Successful Login!\n";
                cin.ignore();
                loggedin=0;
                break;
            }else{
                cout<<"Incorrect Login Information...\n";
                cin.ignore();
                break;
            }                        
        }
    ...
    Thanks for any help you can provide, i appreciate it!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > username[0]='\0';
    This only initialises the first char to 0, not the whole array

    > cin.getline(username, 25);
    Too many magic numbers here - use a constant, or use sizeof
    cin.getline(username, sizeof username);

    > //encrypt
    > ... lots of code
    > //END ENCRYPTION
    This should be a separate function.
    De-bloat your main loop a little.

    > encinfo[l]=tempencI[l]^tempencII[l];
    If your username and password contain the same character in a matching place, the result of this expression is 0.
    This really messes up your later strlen attempts.
    You should be treating this as binary data at this point, not a character string.

    > char importloc[115]="C:/Docu
    Get the compiler to count for you with
    char importloc[]="C:/Docu
    Better yet, since it's just a const string, do
    char *importloc="C:/Docu

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Thanks alot Salem that was really helpful. At the time i started this code i was having problems with functions (i was stupid at the time and could never get them to work but now i can ) Once again thanks! As for binary... im not to familiar with how to do that Thanks!

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Treating it as binary simply means not using the C string handling functions on it. Instead, you need to store the length of it in a separate variable and only work with that length. The string handling functions assume that there is exactly one byte with the value zero in the string, and that one terminates it. With your encryption, you cannot guarantee this any longer.
    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

  5. #5
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    ok thanks, does anyone have a link or something to how i can implement this. I think i understand what you guys are saying (if the result of the XOR is 0 then its putting a '\0' in the string and thus terminating it) How do i change/look at the string in binary? Thanks!

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Basically (in this case), you say the length of the encrypted data is 24 bytes (excluding the \0 for strings), and you use memcpy / memcmp for moving and comparing.

  7. #7

    Join Date
    Jan 2006
    Posts
    28
    \0 turns up as a heart shape, if I'm not mistaken.

    EDIT: Oh, that's \003, sorry!
    Last edited by Dark~Cloud; 01-06-2006 at 11:10 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. My script slows down how do I fix this??
    By JordanCason in forum C++ Programming
    Replies: 10
    Last Post: 11-25-2007, 10:43 PM
  3. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 05:35 AM
  4. "Premature end of Script Headers"
    By tetradtech in forum Linux Programming
    Replies: 2
    Last Post: 10-09-2002, 10:45 AM
  5. Replies: 7
    Last Post: 07-19-2002, 11:49 AM

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