Friday, December 16, 2011

XOR ed value handling

In case encryption is done using xor, if you print/store these value in xml using %s then you may get wrong value; since there might be having non-printable chars,
to handle this kind of issue use character by character value to convert into equal integers using %d.
 enc_data[i] = src[i]^src[j];        // ENCRYPTING USING XOR

void xor_rev(char *src, int *enc_data)
{
  int i = 0, j, len;

  len = strlen(src);
  j = len - 1;

  for(i, j; i < len; ++i,--j)
    {
    enc_data[i] = src[i]^src[j];        // ENCRYPTING USING XOR
    }
  enc_data[i] = '\0';
}

void xor_test(int *enc_data, char *result)
{
  int x, y, len;

  len = strlen(src);

  printf("\nResult is \n");             //REMOVE LATER
  for(y = len-1, x = 0; y >=0 ; --y, ++x)
  {
    result[x]=src[y]^enc_data[x];
    printf("%c", result[x]);
  }
  printf("\n");
}


No comments:

Post a Comment