Thread: XOR and File I/O Problems...

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

    Exclamation XOR and File I/O Problems...

    Hey guys, Im writing a login type program that requires a certain drive to have a certain file on it in order to allow you to enter a username and password. The drive part is done but the password part isn't working. I wrote one program to setup the username and password, XOR them together and save it ina file.
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
         char username[20];
         char password[20];
         cout<<"Set Username: ";
         cin.getline(username, 20);
         cout<<"Set Password: ";
         cin.getline(password, 20);
         char encrypted[20];
         for(int i=0; i<20; i++)
         {
               encrypted[i]=username[i]^password[i];
         }
         ofstream fout("L:/Login/Log-On.txt");
         fout<<encrypted;
         fout.close();
         return 0;
    }
    Then in the Lgoin pro gram it asks you for you username and password, then XOR's them together the EXACT same way it does in the setup program. Then it STRCMP()'s them together to check. Problem is it never works. I had the program cout<< the different XOR's out (the one from the file and the one from the login program) so i could compare them. They're different, dunno why though. Any ideas? Thanks!

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Then in the Lgoin pro gram it asks you for you username and password, then XOR's them together the EXACT same way it does in the setup program.
    Prove it.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > ofstream fout("L:/Login/Log-On.txt");
    You should open the file in binary mode:
    Code:
         ofstream fout("L:/Login/Log-On.txt", ios::binary);
    > fout<<encrypted;
    You should use write() or put() to output the encypted value:
    Code:
    fout.write(encrypted, strlen(username));
    Last edited by swoopy; 02-12-2005 at 11:57 PM.

  4. #4
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Quote Originally Posted by 7stud
    Prove it.
    Ok. They use the exact same process to encode the data in both programs, like i said.
    Code:
    ...
    for(int i=0; i<20; i++)
    {
         encrypt[i]=username[i]^password[i];
    }
    ...
    Im not gonna write the same thing twice. I think the problem is that it doesn't save certain characters to the file correctly so when the second program loads the characters from the txt file it loads em different.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    a few fundamental problems here:
    1. never assume the username OR password have a certain number of characters
    2. NEVER assume they both have the SAME AMOUNT of characters
    3. you should probably let the user enter less than 20 characters
    I don't know about any of the other people on this board, but I don't think I would appreciate anything requiring me to add to my username or password. for example, my username is Major_Small, not Major_Small123456789, and my password is password, not passwordwithextrachr.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Consider
    Username: Fred
    Password: Fred
    What is your encrypted text?
    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.

Popular pages Recent additions subscribe to a feed