Thread: XOR "Encryption"

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    2

    XOR "Encryption"

    I have what seems to be a fairly simple problem, but I've no clue how to fix it. Basically I am learning about encryption, etc.. and made a basic encryption program which works fairly well for a first try except when you decrypt the ciphertext you end up with a few little bits left over. here is the code and before/after:

    Code:
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <fstream>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
            if(argc < 4)
            {
                    cout << "Usage: xorcrypt \"inputfile\" \"outputfile\" \"keyfile\"" << endl;
                    exit(-1);
            }
    
            ifstream input, key;
            ofstream output;
    
            input.open(argv[1],ios::binary);
            output.open(argv[2],ios::binary);
            key.open(argv[3],ios::binary);
    
            key >> skipws;
            input >> skipws;
    
            if(input==NULL)
            {
                    cout << "Error opening inputfile." << endl;
                    exit(1);
            }
    
            if(output==NULL)
            {
                    cout << "Error opening outputfile." << endl;
                    exit(1);
             }
    
            if(key==NULL)
             {
                    cout << "Error opening keyfile." << endl;
                    exit(1);
             }
    
            while(input.eof() != true)
            {
                    output.put(input.get() ^ key.get());
                    if(key.eof() && input.eof() == false)
                     key.seekg(ios::beg);
    
            }
    
            input.close();
            output.close();
            key.close();
    
            return 0;
    }
    That is my program in its entirety, it reads in a file to be encrypted, an output file, and a file to be used as a key.

    Now when I encrypt and decrypt the actual source (XOR.cpp) I end up with this:

    Code:
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <fstream>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
            if(argc < 4)
            {
                    cout << "Usage: xorcrypt \"inputfile\" \"outputfile\" \"keyfile\"" << endl;
                    exit(-1);
            }
    
            ifstream input, key;
            ofstream output;
    
            input.open(argv[1],ios::binary);
            output.open(argv[2],ios::binary);
            key.open(argv[3],ios::binary);
    
            key >> skipws;
            input >> skipws;
    
            if(input==NULL)
            {
                    cout << "Error opening inputfile." << endl;
                    exit(1);
            }
    
            if(output==NULL)
            {
                    cout << "Error opening outputfile." << endl;
                    exit(1);
             }
    
            if(key==NULL)
             {
                    cout << "Error opening keyfile." << endl;
                    exit(1);
             }
    
            while(input.eof() != true)
            {
                    output.put(input.get() ^ key.get());
                    if(key.eof() && input.eof() == false)
                     key.seekg(ios::beg);
    
            }
    
            input.close();
            output.close();
            key.close();
    
            return 0;
    }
    ÿ
    Notice the "ÿ" right at the end. I am getting this problem regaurdless of platform. I compiled with g++ on a linux box and in VS2008/Win7. While I really am just doing this for fun, any further need for encryption could probably get skewed pretty bad in a large scope with little things like this. After all, we have all spent several hours searching for the one semicolon we forgot. Any ideas on how to fix this would be appreciated.

    EDIT: Seems a bit vague as I read it, just to clarify, I compiled the source and then used the XOR.cpp file as an actual argument, e.g. I entered xorcrypt XOR.cpp of.txt key.txt at the command line
    Last edited by svenstrikesback; 06-30-2011 at 11:22 PM.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I think eof isn't true until after you try reading past the end. As such the FAQ about not using feof to control a loop is probably relevant here.
    Last edited by iMalc; 07-01-2011 at 12:16 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2
    Quote Originally Posted by iMalc View Post
    I think eof isn't true until after you try reading past the end. As such the FAQ about not using feof to control a loop is probably relevant here.
    It would seem you are exactly correct, after changing the condition of the loop, it works like a charm. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-31-2009, 04:23 PM
  2. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  3. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM