How can I beat XOR two C char arrays?

It seems silly to me that I cannot understand this, but I am lost. I am trying XOR two lines of C.

#include <stdio.h> #include <memory.h> #include <stdlib.h> int main() { char plainone[16]; char plaintwo[16]; char xor[17]; strcpy(plainone, "PlainOne"); strcpy(plaintwo, "PlainTwo"); int i=0; for(i=0; i<strlen(plainone);i++) xor[i] ^= (char)(plainone[i] ^ plaintwo[i]); printf("PlainText One: %s\nPlainText Two: %s\n\none^two: %s\n", plainone, plaintwo, xor); return 0; } 

My conclusion:

 $ ./a.out PlainText One: PlainOne PlainText Two: PlainTwo one^two: 

Why is the xor array not readable?

+6
source share
2 answers

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 .

+11
source

Well, "Normal" xor "Normal" == 00000, were 0, is a char terminator. Lines C are printed before the terminator, which means that it does not print anything.

+1
source

Source: https://habr.com/ru/post/980901/


All Articles