Once you deal with XOR, you are dealing with binary bytes, which can be non-printable ASCII characters.
And when you XOR the same characters with each other, you get 0. So, 'P' ^ 'P' will be 0. This is the NUL byte, and it ends the line. If you try to print using printf() , you will not get anything; printf() considers the string to be the string length-0.
In addition, you should simply assign the XOR result in the destination buffer = , rather than using ^= , as your program did.
Here is my version of your program and my conclusion:
#include <stdio.h> #include <memory.h> #include <stdlib.h> #define LENGTH 16 int main() { char const plainone[LENGTH] = "PlainOne"; char const plaintwo[LENGTH] = "PlainTwo"; char xor[LENGTH]; int i; for(i=0; i<LENGTH; ++i) xor[i] = (char)(plainone[i] ^ plaintwo[i]); printf("PlainText One: %s\nPlainText Two: %s\n\none^two: ", plainone, plaintwo); for(i=0; i<LENGTH; ++i) printf("%02X ", xor[i]); printf("\n"); return 0; }
Output:
PlainText One: PlainOne PlainText Two: PlainTwo one^two: 00 00 00 00 00 1B 19 0A 00 00 00 00 00 00 00 00
Note that the first five bytes are all 00 , because Plain has XORed with Plain .
source share