3

I've been trying to make a basic XOR header file for use in some future programs. So far I've gotten almost everything to work, but I can't seem to use the same function twice. If I call the function to encrypt the string it works, but then if I call it again it crashes. I don't know if I'm doing something wrong memory-wise or am missing something obvious. Hope someone can point out a flaw in this because I can't seem to find anything wrong.

Edit: If posting this much is too much, feel free to trim the code. I already took out quite a bit, so I'm not just pasting my project and hoping someone fixes it.

// Main.c
#define MAX_LENGTH 255
#define KEY_SIZE 8
int main(int argc, char *argv[]) {
    //Get String to XOR
    char *input = malloc (MAX_LENGTH);
    printf("Enter a string to encrypt: ");
    fgets(input, MAX_LENGTH, stdin);

    if(input[strlen (input) - 1] == '\n') {
        input[strlen (input) - 1] = '\0';
    }

    //Create a random key
    char *pass = _create_key(KEY_SIZE);
    int len = strlen (input);
    printf("Length of key is %d\n", KEY_SIZE);
    printf("Entered String: %s - Password: %s\n", input, pass);

    //Encrypt works fine
    char *encrypted = malloc (sizeof (input));
    _xor_str_s(input, pass, len, encrypted);
    printf("Encrypted String: %s\n", encrypted);

    char *decrypted = malloc (sizeof (input));
    //Crashes here
    _xor_str_s(encrypted, pass, len, decrypted);
    printf("Decrypted String: %s\n", decrypted);
    return 0;
}

//Header File Function
void _xor_str_s(char *str, char *pass, int len, char *out) {
    int i = 0;
    for(i = 0; i < len; i++) {
        *(out + i) = str[i] ^ pass[i % strlen (pass)];
    }
    *(out + i) = 0;
}

char * _create_key(int len) {
    len = !len ? 16 : len;
    char *ret = (char *)malloc (len);
    unsigned int _GLOBAL_SEED_ = (unsigned int)time(NULL);
    srand (_GLOBAL_SEED_);
    int i = 0;
    for(i = 0; i < len; i++) {
        ret[i] = (char)(rand() + 1); //+1 avoids NULL
    }
    ret[i] = '\0';
    return ret;
}
6
  • What is the error message you receive? Mar 13, 2012 at 4:04
  • There is no compile error, it just crashes during runtime without a message. Another thing I should have added is that it works with 4 letter words and some other lengths, but always crashes on anything divisible by 8 and some other random lengths. Just another thing that makes me wonder what's happening. Mar 13, 2012 at 4:06
  • You are missing a NULL reference check in _xor_str_s. Always check for NULL after a malloc call. Mar 13, 2012 at 4:07
  • @torrentialcoding, I don't think that would be the problem I'm having currently since the function will be setting the data anyway. Regardless, I did a check and none of the variables passed the second time are NULL. Mar 13, 2012 at 4:14
  • @ozdrgnaDiies I am not saying that's the problem. I just meant to point out that after a malloc you need to check for NULL to make sure that allocation was successful. Mar 13, 2012 at 4:19

1 Answer 1

11
char *encrypted = malloc (sizeof (input));

is probably the problem as this will always be sizeof(char *). I think you want

char *encrypted = malloc (strlen (input) + 1);
1
  • This is a prime example of how easy it is to gloss over an error when you're sure it's right. This instantly fixed the problem. Always good to have a fresh pair of eyes to spot these types of things. Thanks for the answer! Mar 13, 2012 at 4:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.