XOR Encryption

This is a discussion on XOR Encryption within the C++ Programming forums, part of the General Programming Boards category; First off I appreciate any help that you all may provide. Also I made this post for two reasons. One ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    673

    XOR Encryption

    First off I appreciate any help that you all may provide. Also I made this post for two reasons. One of which being to give anyone out there wanting a XOR encryption function the chance to use this one, and also I have a question between the 2 code snippets at the bottom of the post.

    The working function ( I removed the key I used for privacy, but it is easy to add your own, if you cant your hopeless)
    //crypt.h
    Code:
    #include <string>
    struct CRYPT_
    {
        char byte;
        std::string key;
    };
    
    bool Crypt(const std::string&);
    //crypt.cpp
    Code:
    #include "crypt.h"
    #include <fstream>
    using namespace std;
    CRYPT_ data;
    bool Crypt(const std::string& file)
    {
        char savebyte;
        int value = 0;
        data.key = "type new key here";
        ifstream load (file.c_str(), ios::binary);
        if (!load)
        {
            return false;
        }
        ofstream save ("encrypted.xtc", ios::binary);
        while (load.get(data.byte))
        {
            savebyte = data.byte ^ data.key[value];
            save << savebyte;
            if ( value < 92 )
            {
                ++value;
            }
            else {
                value = 0;
            }
        }
        save.close();
        load.close();
        remove(file.c_str());
        rename("encrypted.xtc", file.c_str());
        return true;
    }
    That is for anyone that may have been looking for something like this.

    Now my question is why will this work
    Code:
    while (load.get(data.byte))
        {
            savebyte = data.byte ^ data.key[value];
            save << savebyte;
            if ( value < 92 )
            {
                ++value;
            }
            else {
                value = 0;
            }
        }
    but on the other hand this looks pretty much the same, but I get a lot of errors trying this...
    Code:
    while (load.get(data.byte))
        {
            save << data.byte ^ data.key[value];
            if ( value < 92 )
            {
                ++value;
            }
            else {
                value = 0;
            }
        }
    Thank you very much for taking your time to assist me. Good day.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Which has more precedence, operator^ or operator<<?

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    I thought bitwise took precedence, but I guess I was wrong?

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    Quote Originally Posted by Raigne View Post
    I thought bitwise took precedence, but I guess I was wrong?
    << is bitwise as well. There is no precedence level called "bitwise," the bitwise operators are spread across a few levels. Get a C precedence chart, print it, tape it on the wall.

  5. #5
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Yay, precedence chart!

    http://www.cs.uml.edu/~dm/course/91....ence_chart.htm

    [offtopic] Anyone like my sig?
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    so placing parenthesis would have fixed this. Got it, thanks for the help. Hopefully this helps someone.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Doubtful, most care to look at operator precedence. At least those who like the rules of maths :s

Popular pages Recent additions subscribe to a feed

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