It`s good idea, but I still need to pass just 2 strings to the parameters. I came up with new code. But, I am missing 2 char-s somewhere. It appeared that XOR - CAN XOR to '\0' and return unexpected null terminated buffer. Here is the fix and check:
Code:
static inline char* XOR_buffer(const char* d,const char* k) {
const int stringl = strlen(d);
const int klen = strlen(k);
char* privateXorBuff = (char*) malloc(sizeof(char)* stringl);
char* begin = privateXorBuff;
char* ret = begin;
int j=0;
int h=0;
for(j=0,h=j+1; h < stringl; j++,h++) {
char* c = Utils.getStringFromTo(d,j,h);
c[0] ^= k[j%klen];
if ( c[0] == '\0') {
c[0] ^= k[j%klen];
}
privateXorBuff[j] = c[0];
}
return ret;
}
Actually if XOR makes a null terminator - dexor it. But, in my code:
Code:
char *buf = "Now is the time for all good men to came and aid their party\nThe quick brown fox jumpped iver the lazy dog";
char* bufxor = Utils.XOR_buffer(buf, "abc123");
char* dexor = Utils.XOR_buffer(bufxor, "abc123");
printf("\n\nXOR:%s\n\n", bufxor);
printf("\n\nDEXOR:%s\n\n", dexor);
The last printf prints "Now is the time for all good men to came and aid their party\nThe quick brown fox jumpped iver the lazy d", the DOG last 2 chars are missing. There must be some thing I am missing.