How can I bitwise XOR two C char arrays?

冷暖自知 提交于 2019-11-30 02:45:42

问题


I feel silly for not being able to figure this out, but I am lost. I am trying to XOR two C strings.

#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 output is:

$ ./a.out 
PlainText One: PlainOne
PlainText Two: PlainTwo

one^two: 

Why doesn't the xor array read as anything?


回答1:


Once you are dealing with XOR, you are dealing with binary bytes that might not be printable ASCII characters.

And when you XOR the same characters with each other, you get a 0. So 'P' ^ 'P' will be 0. That's a NUL byte and it terminates the string. If you try to print with printf() you get nothing; printf() considers the string to be a terminated length-0 string.

Also, you should simply assign the XOR result into your target buffer with = rather than using ^= as your program did.

Here's my version of your program, and my output:

#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

Notice how the first five bytes are all 00 because Plain is XORed with Plain.




回答2:


Well "Plain" xor "Plain" == 00000, were 0 is the terminator char. C strings print up to the terminator, which means it prints nothing.



来源:https://stackoverflow.com/questions/27914271/how-can-i-bitwise-xor-two-c-char-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!