Thread: XOR Encryption???

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    208

    Question XOR Encryption???

    Hi,
    I am relitivly new at programing and I have been reading all the tutorials at this site. I read the XOR encyption tutroial and have implemented it into my own program but I was wondering about one thing? Decryption? Whats the deal? Can it be done? If so how can I do it? I have been fiddling around with some codes and stuff but It always ends up encrypting it further. Thanx for your help in advance.

    Kas

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Whatever you used to encrypt the data into its cyphertext counterpart, use the same key and you will get the plain text back out.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    208

    Question Yea???

    OK
    exactly how would I go about doing that??
    this is the code I tried.

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <fstream.h>
    
    int main()
    { char first[502];
      char out[502];
      char string[502];
      char key[502]= "DgFGH51ja8Md-klagf1";   
      ifstream b_file("encrypt1.txt");
      b_file>>first;
      cout<<"\nYour Encrypted Text\n\n";
      cout<<first;
      cout<<"\n\nYour Text Unencrypted\n\n";
     for(int x=0; x<502; x++)
      {
        string[x]=key[x]^string[x];
        cout<<string[x];
        
      }
     b_file.close();   
     cout<<"\n\nThere You Go!!";
          system("PAUSE");
          return 0;
    }

  4. #4
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    The principle behind XOR encryption has to do with a curious property of XOR operation (exclusive OR),

    As you know OR only produces false when both operands are false... 6 is bigger than 7 or smaller than 5. Both are false, so the overal statement is false.

    With XOR (EOR, as some like to call it) when both operands are true, it also produces false. That is, It's only true when one and only one of the operands is false.

    When doing a bitwise comparison you get this:
    Code:
    A     B   C
    1 XOR 0 = 1
    1 XOR 1 = 0
    0 XOR 1 = 1
    0 XOR 0 = 0
    Now, check this out. You see the end result there? 1010? Now, do again a bitwise XOR against the 2n operand:
    Code:
    C     B   A
    1 XOR 0 = 1
    0 XOR 1 = 1
    1 XOR 1 = 0
    0 XOR 0 = 0
    See how by xoring the end result (C) again by the second operand(B) you go back to the initial value.

    That's how you XOR encrytpion works.
    So basically:

    A XOR B encrypts to C
    C XOR B decrypts back to A

    Also note that the operation doesn't care for where the operand is in relation to the operator. I don't know how it is called in English. But it's the same property as with addition and multiplication... 1 + 2 = 2 + 1
    In portuguese it's called commutative property ("propriedade comutativa")
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    208

    Angry Frustration

    k,
    I understand what you said but It stil won't work for me???
    Here is my decrypt code.

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <fstream.h>
    
    int main()
    { char a[502];
      char b[502];
      char c[502];
      ofstream a_file("unencrypt.txt");
      ifstream b_file("encrypt1.txt");
      cout<<"\n\t\t*******Your Encrypted Text***********";
      b_file>>c;
      cout<<c;
         for(int x=0; x<502; x++)
         {
           a[x]=b[x]^c[x];
           cout<<a;
           a_file<<a;
         }
         a_file.close();
         b_file.close();
         cout<<"\n\n Well Thats Your Files. The Unencrypted\ntext will be in a txt file\named unencrypt.txt";
          system("PAUSE");
          return 0;
    }

    And it still gives me a bunch of gibbity gork text (which i know is encrypted) I want it to output the text that was input into another program and encrypted to a text file??????
    here is my encrytion source????


    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <fstream>
    
    
    int main()
    { char a[502];
      char out[502];
      char c[502];
      char b[20]="DgFGH51ja8Md-klagf1";
      ofstream a_file("encrypt1.txt");
      cout<<"\n\n\t\t********Please Input Less Then 500 Chars*********\n\n";
      cin>>a;
      cout<<"\n\n\t\t ***********This Is Your Encoded Text*************** \n\n";
      for(int x=0; x<502; x++)
      {
        c[x]=a[x]^b[x];
        cout<<c[x];
        a_file<<c[x];
      }
      
      cout<<"\n\nWell Thats It. Encrypted Text Will Be Written In A 'encrypt.txt' file\n\n";
        a_file.close();
        system("PAUSE");
        return 0;
    }
    Please can you help me???????

    Thanx


    Kas

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    66
    for(int x=0; x<502; x++)
    {
    c[x]=a[x]^b[x];
    cout<<c[x];
    a_file<<c[x];
    }

    when b is a char[20]?? Looks like you are way ou of bound of memory XORing with random memory data....


    Maybe b[ x % 20 ] in both encrypt and decrypt functions would help.

  7. #7
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    ok i understand your problem...

    Your XOE encryption program will have to handel things like

    a XOR a where the result is 0.. So here is where the problem arrises......


    Why dont you have a look at my vigenere algorithm which i had posted earlier.. it is better for encrypting text.....

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Well, like I said, you use the same thing to decode the text that you use to encode the text. That means you don't need two different pieces of code to make this work, a single program will do it for you. One problem is that you were outputting your encrypted text using cout when you should have been using a file so that the data could be saved somewhere. A second problem was that your input filename was fixed. It is much better to use two files, one for reading data from and a one to write data to. Also, let the user specify the filenames by passing them into the program from the command-line. With some modifications to your original code, I got this to work:

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <fstream.h>
    #include <string.h>    // For the strlen function
    
    int main( int argc, char* argv[] )
    {
        char cValue;
        int iIndex = 0;
        char* key = "DgFGH51ja8Md-klagf1";   
        ifstream in_file;
        ofstream out_file;
    
        // Test for proper number of command line arguments.
    
        if( argc != 3 )
        {
            cout << "Improper number of command line arguments." << endl;
            return 1;
        }
    
        // We have the proper number of arguments, open the files
    
        else
        {
            // Try to open the two files.
    
            in_file.open( argv[1] );
            out_file.open( argv[2] );
    
            // Make sure the input/output files are open.
    
            if( !in_file || !out_file )
            {
                cout << "Could not open files." << endl;
                return 1;
            }
        }
    
        // Encrypt the text from the input file and send to the output file.
    
        while( in_file.get( cValue ) )
        {
            out_file.put((char)(cValue^key[iIndex]));
            iIndex++;
            iIndex %= strlen(key);
        }
    
        // Clean up.
    
        in_file.close();
        out_file.close();   
        return 0;
    }
    The program is run from the command-line and passed two parameters, the name of the input file, and the name of the output. I called the program encrypt and tested it like so:

    encrypt file1.txt file2.txt

    This encrypted the plain text data in file1.txt and created a file named file2.txt that contained the encrypted text. Then I ran the program a second time like so:

    encrypt file2.txt file3.txt

    This took the encypted text data in file2.txt and converted it back into plain text in a file named file3.txt. If you try this yourself, you can then print out the contents of all the files and see what they look like. The contents of file1.txt and file3.txt in my case were identical indicating that everything worked. file2.txt contained what appeared to be garbage meaning the encyrption worked as well. As you can see there is no separate decrypt program, just a single program that does both encryption and decryption.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    By the way, you can easily modify the above code to accept a third command-line argument that is the key to be used to encrypt/decrypt the data, then you won't have to always use the same key.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed