Thread: XOR encryption is null-terminating my strings!

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    16

    XOR encryption is null-terminating my strings!

    I'm dealing with two strings, one being the string that I want to encrypt using XOR encryption and one being my "key" that I XOR the original string against.

    for (a simplified) example:
    Code:
    while ( szOriginal[i] != '\0' && szKey[i] != '\0' )
    {
    	szOriginal[i] = szOriginal[i] ^ szKey[i];
    	i++;
    }
    HOWEVER, the problem is that if by chance szOriginal[i] == szKey[i], the XOR operation will result in 0! This will replace what was a valid character in the string with a null character.

    Then, when I go to print the encrypted string (or do other things with it), the printing ends prematurely because the new null character terminates it.

    How do most programmers deal with this?

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Write encrypted using length. don't use functions that expect null terminated string.
    Eg use something like
    Code:
    fwrite(encrypted_string,1,len,stdout);

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    16
    Quote Originally Posted by Bayint Naung View Post
    Write encrypted using length. don't use functions that expect null terminated string.
    Eg use something like
    Code:
    fwrite(encrypted_string,1,len,stdout);
    Ah, ok. I don't know why I didn't think to do this... I had been using fread() and such and yet for some reason used fputs() instead of just fwrite().

    Thank you very much!

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If you use printf to display it then yes that will stop at the first zero byte. But really, once you start XORing it's no longer text, it's binary data, so it doesn't make sense to use functions designed for printing text. It would be more appropriate to display each character in hexadecimal one at a time.
    Of course "most programmers" wouldn't be caught dead using such a weak form of "encryption".
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by iMalc
    Of course "most programmers" wouldn't be caught dead using such a weak form of "encryption".
    It depends on how the key actually is generated, but that could well be part of a one time pad implementation since each byte of the plaintext is matched with each byte of the key.

    ... oh, but not really. steez, your loop is problematic in that if the key is shorter than the plaintext, you're going to end up with a bunch of bytes that are left as plaintext.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Point taken.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    16
    Quote Originally Posted by iMalc View Post
    If you use printf to display it then yes that will stop at the first zero byte. But really, once you start XORing it's no longer text, it's binary data, so it doesn't make sense to use functions designed for printing text. It would be more appropriate to display each character in hexadecimal one at a time.
    Of course "most programmers" wouldn't be caught dead using such a weak form of "encryption".
    I'm not printing it to view as output, rather I'm writing to a file to encrypt the file (e.g. a .txt file or a .jpg). For this reason it makes much more sense that I should have just used fwrite() to begin with, but I made a mistake.

    The key and the source data (to be encrypted) are read from separate files (whose filenames are passed as args to the program).

    And thank you for your condescension, I'm only a week into writing my own programs and this is my first exposure to any sort of encryption. Have to start somewhere.


    Quote Originally Posted by laserlight View Post
    ... oh, but not really. steez, your loop is problematic in that if the key is shorter than the plaintext, you're going to end up with a bunch of bytes that are left as plaintext.
    I actually use different variables for each counter, and I check within my loop if it has reached the end of the key. Something like if k > keySize (which I find earlier with fseek). If it has, it goes back to the beginning of the key (k = 0).
    Last edited by steez; 05-31-2011 at 04:31 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Check terminating null character
    By Ducky in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2010, 03:07 AM
  2. null terminating a string
    By steve1_rm in forum C Programming
    Replies: 5
    Last Post: 03-01-2009, 05:55 PM
  3. Null terminating a character pointer?
    By INFERNO2K in forum C++ Programming
    Replies: 9
    Last Post: 11-28-2005, 08:52 AM
  4. terminating null
    By rodrigorules in forum C Programming
    Replies: 19
    Last Post: 09-09-2005, 01:43 AM
  5. NULL terminating a multi dim char array
    By NickESP in forum C++ Programming
    Replies: 2
    Last Post: 02-05-2003, 03:43 PM