I'm working on a basic encryption program (New in programming). At this very moment, I'm trying to XOR 2 hex values to create a cipher text. The problem is, my output is giving me odd data.
For example 0x61 XOR 0x2a should give me 4b according to an online xor calculator, but in my program I get odd outputs. Can some one explain why my output is not 4b and etc and how to properly fix it?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
char my_string[5]="0x61, 0x6e, 0x74, 0x69, 0x64";
char my_key[5]= "0x2a, 0x44, 0x23, 0x01, 0x46";
char cText;
int main()
{
int i;
for( i = 0; i<5; i++)
{
cText = my_string[i]^my_key[i];
printf(" %i . The Cipher Text is %0x \n", i + 1, cText);
}
return;
}
Your string initializations are invalid — you are initializing the strings with the literal text "0x61, 0x6e…"
, not with the hexadecimal values 0x61, 0x6e, etc.
To initialize a character array with numeric values, use curly braces instead of quotation marks:
char my_string[5]= { 0x61, 0x6e, 0x74, 0x69, 0x64 };