Thread: Xoring text with binary file

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    Xoring text with binary file

    Hi,

    Why can't we xor text with binary file?

    When I xor it back half of them are not the same, garbage characters.

    Code:
    #include<fstream>
    #include<iostream>
    #include<string>
    using namespace std;
    
    string XOR(string value,string key);
    void OpenKeyFile();
    
    string value ("The quick brown fox jumps over the lazy dog");
    const char * key_file = "C:\\Windows\\System32\\appinfo.dll";
    int length_of_command = 200;
    char secret_key[520] = "";
    
    int main()
    {
        OpenKeyFile();
    
        string key(secret_key);
    
        cout<<"Plain text: "<<value<<"\n\n";
        value=XOR(value,key);
        cout<<"Cipher text: "<<value<<"\n\n";
        value=XOR(value,key);
        cout<<"Decrypted text: \n"<<value<<endl;
    
        return 0;
    }
    
    void OpenKeyFile()
    {
        fstream ifs(key_file, fstream::in | fstream::binary);
        char a;
        if(!ifs.is_open())
        {
            cout << "Couldnt open input file "<<key_file<<endl;
            return;
        }
    
        //
        ifs.seekp(0);
        // write to ifs
        ifs.read(secret_key, length_of_command);
        // cout << vIn << endl;
        ifs.close();
    
    }
    
    string XOR(string value,string key)
    {
        string retval(value);
    
        short unsigned int klen=key.length();
        short unsigned int vlen=value.length();
        short unsigned int k=0;
        short unsigned int v=0;
    
        for(v;v<vlen;v++)
        {
            cout << value[v] << " - " << key[k] << "\n";
            retval[v]=value[v]^key[k];
            k++;
        }
    
        return retval;
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    string key(secret_key);
    This will only copy up to the first \0.
    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.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Ah, of course, I should use a vector I guess. Thank you.
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary/Text file
    By jakemott in forum C Programming
    Replies: 3
    Last Post: 11-23-2009, 01:36 PM
  2. The text file is a binary ?
    By abh!shek in forum C Programming
    Replies: 7
    Last Post: 05-23-2008, 09:29 AM
  3. binary text file
    By spanker in forum C Programming
    Replies: 7
    Last Post: 12-28-2007, 02:10 AM
  4. Text to Binary file
    By tomas.ra in forum C Programming
    Replies: 2
    Last Post: 09-23-2005, 05:52 AM

Tags for this Thread