Results 1 to 5 of 5

Thread: XOR Encryption Attempt

  1. #1
    Senior Member
    Join Date
    Jan 2004
    Location
    Hawaii
    Posts
    350

    Angry XOR Encryption Attempt

    Okay, I've been following a thread on here where an AO user explained XOR to me (I forget who, sorry). But anyways, I wrote a simple C++ app that takes an input file, and XOR's it against the corresponding bytes of another file, and outputs it to an encrypted file...but my problem is, why doesn't it decrypt correctly...when I encrypted a TXT file with "hello", i got some weird ASCII characters...expected. But when I decrypted it, I got "helloou". I used the key "this is garbage.".

    Code:
    #include <iostream.h>
    #include <fstream.h>
    
    char inpath[50], keypath[50], outpath[50];
    char buf1, buf2, buf3;
    
    main()
    {
        cout << "Input file: ";
        cin >> inpath;
        cout << "File for key: ";
        cin >> keypath;
        cout << "Output file: ";
        cin >> outpath;
        
        ifstream infile(inpath);
        ifstream keyfile(keypath);
        ofstream outfile(outpath);
        
        while(!infile.eof())
        {
            infile.get(buf1);
            keyfile.get(buf2);
            buf3 = buf1 ^ buf2;
            outfile.put(buf3);
        }
        
        cout << "Process Complete!" << endl;
        
        infile.close();
        keyfile.close();
        outfile.close();
        system("pause");
    }
    Geek isn't just a four-letter word; it's a six-figure income.

  2. #2
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Hi

    Zeroth remark: It was [SirDice/]Tim_axe...
    First remark: Assign the main-function with a return type.
    Second remark: Avoid any call to [f]eof. I always have trouble with it.
    Here in this case, it spoils the length of the input by 1. Hence, your
    decrypted file has a length +2 in the end.
    Third remark: Avoid to read in byte by byte. The performance is bad.
    Read the whole thing in in one operation. Here is a working code:

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    
    
    char inpath[50], keypath[50], outpath[50];
    char buf1[30], buf2[30], buf3[30];
    int i;
    
    int main()
    {
        cout << "Input file: ";
        cin >> inpath;
        cout << "File for key: ";
        cin >> keypath;
        cout << "Output file: ";
        cin >> outpath;
        
    
    	ifstream infile(inpath, ios::in | ios::binary);
    	infile.read((char *)(&buf1), sizeof(buf1));
    	infile.close();
    
    	ifstream keyfile(keypath, ios::in | ios::binary);
    	keyfile.read((char *)(&buf2), sizeof(buf2));
    	keyfile.close();
    
    	i=0;
        while(buf1[i]!=0){
                buf3[i]=buf1[i]^buf2[i];
    			i++;
        }
     
        cout << "Process Complete!" << endl;
        
     	ofstream outfile(outpath, ios::out | ios::binary);
    	outfile.write((char *)(&buf3), i);
    	outfile.close();
    	return 0;
    }
    Cheers.
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  3. #3
    Senior Member
    Join Date
    Jan 2004
    Location
    Hawaii
    Posts
    350
    I notice that the buffer sizes are only 30...without using eof(), how would I read a file larger than 30 bytes without changing the buffer's size.

    Also, how can I dynamically allocate memory to be the size of a particular file? I've messed with malloc(), but I'm not fully aware of how to use it correctly.


    A_T

    PS - I have not attempted to compile this code yet....I'm on a fresh Windows install on another PC.
    Geek isn't just a four-letter word; it's a six-figure income.

  4. #4
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Hi A_T

    You really should do some reading. Solving a little problem by yourself
    helps you to keep that stuff in mind - hence, you will make progress.
    Copy and pasting prepared code, even when you "look" at it, does not
    help to improve your skills. Be independent!

    Anyway, two ways to get the filesize, after that a little malloc

    Code:
    #include <io.h>
    #include <fcntl.h>
    #include <sys/stat.h>
    (...)
    int fh;
    struct _stat buf;
    
    fh = _open( filename, _O_RDONLY ); // no error handling
    
    _fstat( fh, &buf );
    printf("_fstat: %ld\n",buf.st_size);
    
    printf ("_filelength: %ld\n", _filelength(fh));	
    
    malloc_buf = (char *)malloc( _filelength(fh) ); // convert the type of memory to char* rather than void*
    printf("Bytes _read: %ld\n", _read( fh, malloc_buf, _filelength(fh) ) );
    free(malloc_buf);
    _close( fh );
    Note, that the number of bytes read can be smaller than the filelength.
    This is due to a reinterpretation of 0x10h and 0x13h. You can use the
    filelength, determined by the low-level io routines, as an input of the
    istream of course.

    Cheers.
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  5. #5
    Senior Member
    Join Date
    Jan 2004
    Location
    Hawaii
    Posts
    350
    I prefer not to read from a book when I can get more competent questions here, ones that I can inquire more if I need to.
    But thanks for the answer.
    A_T
    Geek isn't just a four-letter word; it's a six-figure income.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •