XOR Encryption

This is a discussion on XOR Encryption within the C++ Programming forums, part of the General Programming Boards category; [QUOTE=matsp;663024] Also, for future use, you may want to allow more than one encrypted text to exist, so maybe you ...

  1. #46
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    [QUOTE=matsp;663024]
    Also, for future use, you may want to allow more than one encrypted text to exist, so maybe you should just merge the two source-codes to one application, and just decrypt/encrypt all in one thing - that's the beuty of a XOR encryption - it's exactly the same thing to encrypt and decrypt - the only difference is what you've got as an input - clear text or encrypted text.
    /QUOTE]

    The reason I did that is because for some reason, if the file is truncated, it will only write one letter to it, so I have to append to it. I was forgetting to clear it when I was done, so I ended up with more then one text file encrypted in it.

  2. #47
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by matsp View Post
    If you really want to delete files, perhaps you should figure out how to delete files within the application rather than calling system()
    http://www.cplusplus.com/reference/c...io/remove.html

  3. #48
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
            // do something with the block
            for(int i = 0; i < noBytes; i++)
            {
                out = buffer[i];
                string output = XOR(out, key);
                ofstream os("Encrypted_Text.txt", ios::app);
                os << output;
            }
    you open the file with every iteration of the loop. Open it once instead.
    Code:
            ofstream os("Encrypted_Text.txt", ios::app);
    
            // do something with the block
            for(int i = 0; i < noBytes; i++)
            {
                out = buffer[i];
                string output = XOR(out, key);
                os << output;
            }
            os.close();

  4. #49
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by robwhit View Post
    Code:
            ofstream os("Encrypted_Text.txt", ios::app);
    
            // do something with the block
            for(int i = 0; i < noBytes; i++)
            {
                out = buffer[i];
                string output = XOR(out, key);
                os << output;
            }
            os.close();
    And with this, there's no need to append to the existing file, just let the normal functionality of "emptying the file" take place. That way, the removal of the file can be removed, and it's a simple program for it. If possible, keep things simple - it's not always possible, but when it is, that's the best way.

    --
    Mats

  5. #50
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    matsp, (I already said this), for some reason, if I don't append to the file, it will only write the one character to it- that's why I have it append it.

  6. #51
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by mikeman118 View Post
    matsp, (I already said this), for some reason, if I don't append to the file, it will only write the one character to it- that's why I have it append it.
    That's because, as someone ALREADY pointed out, you re-open the file every time through the loop.

  7. #52
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Quote:
    Originally Posted by mikeman118 View Post
    matsp, (I already said this), for some reason, if I don't append to the file, it will only write the one character to it- that's why I have it append it.
    That's because, as someone ALREADY pointed out, you re-open the file every time through the loop.
    Oh, right. Hey if this helps anyone if you use "ABCDEFGHIJKLM" as your key, or even just "ABC", it will work just fine. I guess I could just use that as a key, but it's not exactly practical. Hope that helps.

  8. #53
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Also, the suggestions about changing how it handled the key seemed to help a little, but it still picked a random letter to do something to (i.e. color it changed to dcolor, for every instance of the letter c).

  9. #54
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, take a simple text file (say one that contains the alphabet in lower and upper case repeated a few times to make your key move to different places), then check out what particular letter is causing the problem. Put a if-statement in your XOR function and check the input value and output value (print them as hex or decimal by casting to an int).

    --
    Mats

  10. #55
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    (oops, nvm, didn't realize there was 4 pages.)

  11. #56
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Well, it appears that the problem letter is different for each key that you use, and sometimes there is no error. However, if the key starts with "m", the output is:
    Code:
    ABCDEFGHIJKLMNOPQRSTUVWXYZ
    abcdef`ghijklmnopqrstuvwxyz
    ABCDEFGHIJKLMNOPQRSTUVWXYZ
    abcdef`ghijklmnopqrstuvwxyz
    ABCDEFGHIJKLMNOPQRSTUVWXYZ
    abcdef`ghijklmnopqrstuvwxyz
    There are a few other letters that do such things, maybe I should just use a key that doesn't use those letters...

    Edit: The letters that do not work as the beginning of the key (i.e. they make a ~ appear after certain characters) are:
    k
    l
    m
    n
    o
    p
    r
    s
    Anyone have an idea why only those characters don't work? Thanks.

    Edit: I guess that was with only the first letter, if you use a longer key, with some of those characters combined (e.g. eggs, which starts with "e" which should be fine, but it ends in "s"). So that's what's wrong now, I think I'm giving up on it, unless someone else has an idea.
    Last edited by mikeman118; 08-09-2007 at 07:45 PM.

  12. #57
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    Here is a xor encryption algo. You seem to be having trouble with your xor routine.
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    //For simplicity sake
    const std::string key = "mxythpo";
    
    std::string xorString(const std::string &in)
    {
    	std::string retString = in;
    
    	size_t inLength = in.length();
    	size_t keyLength = key.length();
    
    	//Loop through string and xor each charcter
    	for(size_t i = 0, k = 0; i < inLength; i++){
    		retString[i] = retString[i] ^ key[k];
    
    		//Make sure the key index is in range
    		k = k + 1 < keyLength ? k + 1 : 0;
    	}//for
    
    	return retString;
    }
    
    int main()
    {	
    	std::string unencryptedString = "Hello I am an human readable string";
    
    	//Initial encrypt of the string
    	std::string encyptedString = xorString(unencryptedString);
    
    	std::cout<<"Encrypted String = "<<encyptedString<<std::endl<<std::endl;
    
    	//Decrypt the encrypted string
    	std::string decryptedString = xorString(encyptedString);
    
    	std::cout<<"Decrypted String = "<<decryptedString<<std::endl;
    
    	std::cin.get();
    
    	return 0;
    }

  13. #58
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Ok well I've just decided to do something else, I may come and try again when I know more, but for now I'm giving up. Thank you everyone who gave me advise, it is greatly appreciated.

Page 4 of 4 FirstFirst 1234
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