-1

I have two strings (char *) that contain a hex representaton of some data. I need to convert this into binary strings and then xor the results together. After that I will convert that binary result to a hexadecimal representation.

char * convertHexToBinaryString(char *alphabetString)
{

   char* val= (char*)malloc(strlen(alphabetString)*sizeof(char));
    char *pos = alphabetString;
    size_t count = 0;

    /* WARNING: no sanitization or error-checking whatsoever */
    for(count = 0; count < strlen(alphabetString)/2; count++) {
        sscanf(pos, "%2hhx", &val[count]);
        pos += 2 * sizeof(char);
    }
    return val;
}


char hexDigitForNumber(char number)
{
    switch (number) {
        case  0:
            return '0';
            break;
        case 1:
            return '1';
            break;

        case 2:
            return '2';
            break;

        case 3:
            return '3';
            break;

        case 4:
            return '4';
            break;

        case 5:
            return '5';
            break;

        case 6:
            return '6';
            break;

        case 7:
            return '7';
            break;

        case 8:
            return '8';
            break;

        case 9:
            return '9';
            break;

        case 10:
            return 'a';
            break;

        case 11:
            return 'b';
            break;

        case 12:
            return 'c';
            break;

        case 13:
            return 'd';
            break;

        case 14:
            return 'e';
            break;

        case 15:
            return 'f';
            break;

        default:
            return 'X';
            break;
    }
    return 100;

}

char * convertBinaryToHexString(char *binData)
{
    char *result = (char *)malloc(strlen(binData)*2);
    int latestIndex = 0;
    int szBd = strlen(binData);
    for (int i = 0; i < strlen(binData); i++) {
        unsigned char crt = binData[i];
        result[latestIndex] = hexDigitForNumber(crt >> 4);
        result[latestIndex +1] = hexDigitForNumber(crt % 16);
        latestIndex +=2;
    }
    return result;
}


char * xorBinaryStrings(char *s1,char *s2)
{
    int endPoint = (int)MIN(strlen(s1),strlen(s2));
    char *result = (char *)malloc(endPoint * sizeof(char));
    for (int i = 0; i<=endPoint; i++) {
        result[i] = s1[i] ^ s2[i];
    }
    return result;
}

I try to use the functions like this:

char *s = "48656c6c6f20576f726c64"; // Hello world
char *k= "7375706572736563726574"; // super secret
char *hexPT = convertHexToBinaryString(s);
char *hexKey = convertHexToBinaryString(k);
char *result = xorBinaryStrings(hexKey, hexPT);
char *rs = convertBinaryToHexString(result);

The problem is the rs string is 3b101c091d53320c0 ... when it should be 3b101c091d53320c000910... How do I make it ignore that null string terminator???

user1028028
  • 6,323
  • 9
  • 34
  • 59

1 Answers1

1

The problem is that you're storing arbitrary binary data in a region of memory, and then asking the C library to treat it as strings. A better solution is to use the uint8_t type for arbitrary byte-sized data, and to keep track of the number of bytes contained in the array separately, e.g. using a size_t variable.

Magnus Reftel
  • 967
  • 6
  • 19